I can’t seem to find a simple formula to calculate this.
This is the closest I can get buy there are 2 paths depending if the row is odd or even on the X axis that calculate half the numbers 1 higher.
public static int HexDistance(int sx, int sy, int ex, int ey) {
int xd = Math.Abs(sx - ex);
int yd = (int)Math.Abs(sy - ey);
int d = xd + Math.Max(0, yd - (xd / 2));
return d;
}
This answer is going to be completely dependent on how you order your cell addresses. I’ve not mucked with the Unity hex-mode tilemaps so I can’t speak to how they are laid out, but I do keep these handy-dandy hex links:
I have tried every single formula even the double width and all of them come out with errors.
I have 5 different methods doing this and none work correctly. Of course I have the one model has none of the articles I saw is using which is the 0,0 being lower left and (1,1) is 1 right, 1 up being offset UP half a hex.
This is what it looks like with 0, 0 being the lower left of the map, 1, 1 is offest up from there.
You did not read the resources Kurt provided carefully. Especially in the first link the paragraph about Distances introduces cube coordinates (using 3 axis instead of two) which you should definitely use when you deal with hexagons. I also found this a usefull resource explaining the concepts and thoughts. But unfortunately the site is down and it’s not available in web archive. But author seems to provide his email adress so maybe you could contact him directly.
You really should consider trying the ones I linked in my first response. It’s like @exig says: it literally tells you ALL the ways you can handle these coordinate systems.
You are on extremely well trodden ground: put away the machete and flame thrower and stop chopping / burning a fresh path through virgin jungle wilderness, unless you’re into that and don’t mind the malaria and splinters.
I asked Amit from Red Blob games. I had a Homer SImpson moment. While I have a custom made mesh for my wargames in which I odd-q shift it. I forgot to add it to my distance formula. As soon as I read the first line of his email… DOH!..
For those having the same issues this is the formula written in rq.
// convert odd-q to axial
int q1 = x1;
int r1 = y1 - (x1 - (x1 & 1)) / 2;
// convert odd-q to axial
int q2 = x2;
int r2 = y2 - (x2 - (x2 & 1)) / 2;
int qd = q1 - q2;
int rd = r1 - r2;
int dist = (Mathf.Abs(qd) + Mathf.Abs(qd + rd) + Mathf.Abs(rd)) / 2;
And this is the formula simplified using xy only. 1,0 is shifted up.
FLAT top 2nd column shifted up… odd-q
public static int HexDistance(int x1, int y1, int x2, int y2) {
int xd = x1 - x2;
int yd = y1 - ((x1 - (x1 & 1)) / 2) - (y2 - (x2 - (x2 & 1)) / 2);
int dist = (Mathf.Abs(xd) + Mathf.Abs(xd + yd) + Mathf.Abs(yd)) / 2;
return dist;
}
FLAT top 2nd column shifted down… even-q
public static int HexDistance(int x1, int y1, int x2, int y2) {
int xd = x1 - x2;
int yd = y1 - ((x1 + (x1 & 1)) / 2) - (y2 - (x2 + (x2 & 1)) / 2);
int dist = (Mathf.Abs(xd) + Mathf.Abs(xd + yd) + Mathf.Abs(yd)) / 2;
return dist;
}
Thanks Fuzzypop, this saved my bacon tonight, I can confirm it works perfectly, I appreciate you taking the time to put them up for us to cut and paste in.