SOLVED Issue with hex grid and distance

Geners

Member
I have a hex grid and I used http://3dmdesign.com/development/hexmap-coordinates-the-easy-way this guide to figure out how distance works on such a grid.


I made my grid, gave every hex a coordinate, and then checked the distance of each hex tile with the tile at 2,3


GML:
var xDif = coordinate[0] - 2;

var yDif = coordinate[1] - 3;

var twoDif = yDif - xDif;
var distance = max(xDif,yDif,twoDif);
draw_set_font(f_hex);
draw_text(x,y + 12,string(distance));

Most of the grid shows the correct distance but some show bizarre or impossible distances. Here is a screenshot. The top number on each tile is the coordinate, the bottom is the distance.
distance.PNG

What am I doing wrong here?
 

sylvain_l

Member
What am I doing wrong here?
Distance is equal to the greatest of the absolute values of: the difference along the x-axis, the difference along the y-axis, or the difference of these two differences.

you forget one important detail, you need to compare the absolute value. (else when you have negative value you are going to get the wrong value when you pick the max)
 

Geners

Member
Distance is equal to the greatest of the absolute values of: the difference along the x-axis, the difference along the y-axis, or the difference of these two differences.

you forget one important detail, you need to compare the absolute value. (else when you have negative value you are going to get the wrong value when you pick the max)
AH! Thank you, I totally forgot to make my results absolute. Thank you so much, you're a gold star user.
 

Alice

Darts addict
Forum Staff
Moderator
Aside from what @sylvain_l said, there is also the issue with coordinates.

According to the guide you linked, cells with same Y coordinates are along descending lines, like shown here:


Note how the bottom cells of the hexagon in that picture are (0,0); (1,0); (2,1).
That's because starting from column 1, the grid bottom line goes north-east instead of south-east. And when going north-east, you add 1 to both X and Y, not just X.

In your grid, the grid bottom line goes south-east starting from (0,0) along (1,0); (2,0); (3,0); (4,0). That much is correct, because going south-east only increases X by 1.

But starting from cell (4,0), the bottom line starts going north-east. So subsequent cell coordinates, instead of:
(5,0); (6,0); (7,0); (8,0)
should go through
(5,1); (6,2); (7,3); (8,4)

When you adjust the right-side cell coordinates, the distance should calculate correctly for all cells (once you apply absolute difference values, too).
 

Geners

Member
Distance is equal to the greatest of the absolute values of: the difference along the x-axis, the difference along the y-axis, or the difference of these two differences.
Aside from what @sylvain_l said, there is also the issue with coordinates.
Alright, guys, I made the fixes with your help and the distances are a lot more accurate, but there is still some that aren't correct. Anything else I did wrong?

Here is the code
GML:
var xDif = abs(coordinate[0] - 2);

var yDif = abs(coordinate[1] - 3);

var twoDif = abs(yDif - xDif);

var distance = max(xDif,yDif,twoDif);
draw_set_font(f_hex);
draw_text(x,y + 12,string(distance));

And here is a screenshot
grid2.PNG
 

Alice

Darts addict
Forum Staff
Moderator
It's twoDif: it should be calculated as a difference between signed differences, while it's a difference between aboslute differences instead.
For example, for cell 1,4, it should be:
- xDif: -1
- yDif: 1
- twoDif: 2 (1 - (-1))
- distance: 2 (max of abs(-1), abs(1) and abs(2))

While what your code does at the moment is:
- xDif: 1 (abs(-1))
- yDif: 1 (abs(1))
- twoDif: 0 (abs(1 - 1))
- distance: 1 (max of 1, 1, 0)

So the code should be:
GML:
var xDif = coordinate[0] - 2;
var yDif = coordinate[1] - 3;
var twoDif = yDif - xDif;
var distance = max(abs(xDif),abs(yDif),abs(twoDif));
 

Geners

Member
It's twoDif: it should be calculated as a difference between signed differences,
OH MY GOD. That's such an obvious mistake I feel like an idiot.

Thanks again. The game maker community is always excellent.

Edit: It works perfectly now.
 
Top