Hex node distance formula

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:

Hexagonal maps (hexmaps):

1 Like

Just need a simple distance formula. Not all this complicated work. Tried these solutions which didn’t work either.

        int xd = Math.Abs(sx - ex);
        int dx = (x2 - (int)Mathf.Floor(y2 / 2)) - (x1 - (int)Mathf.Floor(y1 / 2));
        int dy = y2 - y1;
        int dist = Mathf.Max(Mathf.Abs(dx), Mathf.Abs(dy), Mathf.Abs(dx + dy));
        return dist;

//OR

        int dx = x2 - x1;
        int dy = y2 - y1;
        int dist;
        if (Mathf.Sign(dx) == Mathf.Sign(dy)) {
            dist = Math.Max(Mathf.Abs(dx), Mathf.Abs(dy));
        } else {
            dist = Mathf.Abs(dx) + Mathf.Abs(dy);
        }

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.

I can’t figure out what I am doing wrong.

7634692--950581--upload_2021-11-6_14-18-16.png

7634692--950575--upload_2021-11-6_14-14-10.png

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.

You’re at the wrong place then and in the wrong industry.

1 Like

In the right industry. I have 2 of 2 successful games that have earned me more than in my previous profession.

There are always easier ways to do things.

The RedBlob games post is confusing and not clear on how to do it. I am finding it very difficult to understand what he is writing.

I converted to the 3 coordinate system following his formulas but it is still not right. The xyz aren’t matching correctly and the distances aren’t.

    struct Cube {
        public int x, y, z;
    }

    public static int HexDistance(int x1, int y1, int x2, int y2) {
        Cube cube = new Cube();
        cube.x = x1 - x2;
        cube.y = y1 - y2;
        cube.z = -cube.x - cube.y;
        return (Mathf.Abs(cube.x) + Mathf.Abs(cube.y) + Mathf.Abs(cube.z)) / 2;
    }

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;
    }
1 Like

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.