Hexagon Coordination System Rotation Problem

849 Views Asked by At

I am working on a hexagon tiles game and I have applied a coordination system to it.

There are two types and hexagon tiles pattern, and my one is look like this: Hexagon Tiles Coordination

*please noticed that for some reasons, the x and y axis here are swapped, x is vertical and y is horizontal, please feel free to swap it back if you want.

In this pattern, using (0,0) as center, you can see it actually have 6 directions. I set the right hand side ((0,1) side) is 0 degree, and we have 60, 120, 180, 240, 300, etc. (rotate clockwise or anti-clockwise are both ok, but i use anti-clockwise here)

Say, I have a unit on (0,1), and I want it to move anti-clockwise with (0,0) as the center, that is, move to (1,1). It is easy to view it graphically, but quite no idea how to calculate it by a formula.

This is getting more difficult when the case extended to d2 and d3 (means the distance from center)

Hexagon Coordination rotation - d1 to d3

So, is there any ways that can calculate what is the new coordinates after rotate anti-clockwise by 60 degrees? or for all degrees? Thanks!

2

There are 2 best solutions below

2
On

The problem you're running into is that you're only kind of forcing an orthogonal coordinate system on your hexagonal array.

The even $y$ rows are offset relative to the odd $y$ rows. When you rotate $\pi/3$, whether a hexagon ends up in an even or odd row depends on the evenness/oddness of $y$.

Maybe that observation is enough to lead you to a formula for rotations of some multiple of $\pi/3$.

But for arbitrary rotations (as well as the rotations just discussed), you'll be better served by using a skew coordinate system. One axis is your current $y$ axis; the other would be $\pi/3$ off from this axis. Your indices would change, but you wouldn't have the offset effect that you have now with the even and odd rows. The indexing would match the symmetry of your layout better.

0
On

I have found a solution of the rotation issues.

As John said, I have to applied another coordination system for the rotation. Instead of using a Skew Coordination System, after I have read the guidlines in Amit Patel's Home, I have decided to use the Cube coordination System.

I strongly recommend everybody that need a Hexagonal tile coordination system to take a look into this site, it just have everything you need!

To summarize, the final approach I choose is: 1. Use simple "offset coordination system" to create all the tile (just like what I did in the question, but I swapped the x and y back) 2. Generate "Cube Coordination System" for all the tiles 3. Use the cube coordinate to process to get the coordinate after rotation, then convert it back to simple "offset coordination system".

There is also an answer in here, which also answered by Amit Patel.