Filed under: Programming · Date: Wed Oct 6 16:53:27 2004
Working with hexagonal maps is not much different to working with square grid maps. Look at the map to the right. It shows a hexagonal grid with labeled hexes. The labels tell the coordinates of each of the hex. Hexes with yellow background mark the X-coordinate; blue background marks Y-coordinate. You can rotate the image if it helps you understand the coordinate system.
Note that each hex on a row in alignment with the yellow row, have the same value for their X-coordinate. And likewise, each hex on a row in alignment with the blue row have the same value for their Y-coordinate.
Calculating distance on hexagonal map is not difficult. The following pseudocode describes how the calculation is done between to hexes A and B.
dX = B.x - A.x
dY = B.y - A.y
distance = (abs (dX) + abs (dY) + abs (dX - dY)) / 2And here's an example. A is 0:2 and B is -2:-2. Now lets put this to the equation: dX = -2 - 0 = -2; dY = -2 - 2 = -4; distance = (abs(-2) + abs(-4) + abs(-2 - -4)) / 2 = (2 + 4 + 2) / 2 = 8 / 2 = 4. From the map above you can see the distance of these two points is 4 hexes, which proves our example right.
For more math on hexagonal maps, and other interesting reads, go to Amit Patel's game programming page. This coordinate model was originally suggested by Lars Jensen.
..i’ve also tryed it with the hexspace transformation…
You have to re-orientate your axxis within the calculation, too:
distance = -(abs(dx) + abs(dy) + abs(dx + dy)) / 2
Hi,
This algo does not works (my hex is vertically aligned…