Hex grid Help

Hi I have a hex grid. I’m finding it hard to expand on this Cartesian coordinate grid for pathing so I did some research.

If found this http://3dmdesign.com/development/hexmap-coordinates-the-easy-way

I’m trying to do move from Cartesian to a slanted approach like described but I having issues. Hex grids are confusing me. The Cartesian coordinates are easy to layout for me but hard to expand on but having one axis slanted diagonal is easy to expand on but I’m having a hard time laying it out.

Thanks for your time.

Here is my grid working laid out using the Cartesian coordinates.

    public void Intialize()
    {
        pathNodes = new PathNode[width, depth];

        //Create our Grid
        for (int x = 0; x < width; x++)
        {
            for (int z = 0; z < depth; z++)
            {
                PathNode pathNode = new PathNode();
                pathNode.gridNodeIndex = new Vector2(x, z);
                pathNode.isWalkable = true;
              
               //POSITION OUR NODE
                float offset = 0;
                if (z % 2 != 0)
                    offset = nodeSize * 0.5f;
                pathNode.position = new Vector3(offset + x * nodeSize, 0, z * nodeSize * 0.75f);
      
                pathNodes[x, z] = pathNode;
            }
        }
    }

Edit : Never mind, read post below.
Ok I got it to work. I’m still using Cartesian coordinates and I just worked on the expansion. All I had to do was take a break and the answer hit me. I just needed to check if the tile is offseted during expansion(Stupid Past me). Anyway here is code.

Creating our grid node positions

 public void Intialize()
    {
        pathNodes = new PathNode[width, depth];
        //Create our Grid
        for (int x = 0; x < width; x++)
        {
            for (int z = 0; z < depth; z++)
            {
                PathNode pathNode = new PathNode();
                pathNode.gridNodeIndex = new Vector2(x, z);
                pathNode.isWalkable = true;
          
               //POSITION OUR NODE
                float offset = 0;
                if (x % 2 != 0)
                    offset = nodeSize * 0.5f;
                pathNode.position = new Vector3(offset + y * nodeSize, 0, x * nodeSize);
  
                pathNodes[x, z] = pathNode;
            }
        }
    }

Expanding

 PathNode pnNorth = current.current;
        pnNorth.gridNodeIndex.y -= 1;
        if (inBounds(pnNorth))
            AddToExpansion(GridGraph.instance.pathNodes[(int)pnNorth.gridNodeIndex.x, (int)pnNorth.gridNodeIndex.y], currentDistance);
     
        PathNode pnSouth= current.current;
        pnSouth.gridNodeIndex.y += 1;
        if (inBounds(pnSouth))
            AddToExpansion(GridGraph.instance.pathNodes[(int)pnSouth.gridNodeIndex.x, (int)pnSouth.gridNodeIndex.y], currentDistance);
     
        PathNode pnNorthEast = current.current;
        pnNorthEast.gridNodeIndex.x += 1;
        pnNorthEast.gridNodeIndex.y -= 1;
        if (pnNorthEast.gridNodeIndex.x % 2 == 0)
            pnNorthEast.gridNodeIndex.y += 1;
        if (inBounds(pnNorthEast))
            AddToExpansion(GridGraph.instance.pathNodes[(int)pnNorthEast.gridNodeIndex.x, (int)pnNorthEast.gridNodeIndex.y], currentDistance);
     

        PathNode pnNorthWest = current.current;
        pnNorthWest.gridNodeIndex.x -= 1;
        pnNorthWest.gridNodeIndex.y -= 1;
        if (pnNorthWest.gridNodeIndex.x % 2 == 0)
            pnNorthWest.gridNodeIndex.y += 1;
        if (inBounds(pnNorthWest))
            AddToExpansion(GridGraph.instance.pathNodes[(int)pnNorthWest.gridNodeIndex.x, (int)pnNorthWest.gridNodeIndex.y], currentDistance);

        PathNode pnSouthWest = current.current;
        pnSouthWest.gridNodeIndex.x -= 1;
        pnSouthWest.gridNodeIndex.y += 1;
        if (pnSouthWest.gridNodeIndex.x % 2 != 0)
            pnSouthWest.gridNodeIndex.y -= 1;
        if (inBounds(pnSouthWest))
            AddToExpansion(GridGraph.instance.pathNodes[(int)pnSouthWest.gridNodeIndex.x, (int)pnSouthWest.gridNodeIndex.y], currentDistance);

        PathNode pnSouthEast = current.current;
        pnSouthEast.gridNodeIndex.x += 1;
        pnSouthEast.gridNodeIndex.y += 1;
        if (pnSouthEast.gridNodeIndex.x % 2 != 0)
            pnSouthEast.gridNodeIndex.y -= 1;
        if (inBounds(pnSouthEast))
            AddToExpansion(GridGraph.instance.pathNodes[(int)pnSouthEast.gridNodeIndex.x, (int)pnSouthEast.gridNodeIndex.y], currentDistance);

Hi everything is working fine but path finding is slow. I tested it and it is because i’m doing this every 4 out of 6 expansions.

 if (pnNorthWest.gridNodeIndex.x % 2 == 0)

With the slanted approach I wont have to check that and thus speeding up my A*.
I’m still having trouble laying out the grid like that.

Simplifies, and converts the hexadecimal matrix in a two dimensional array.

Then you can use an ordinary algorithm pathfinding.

Colums = Width and Files = Height * 2 - 1.
In even-numbered columns, the first position is repeated twice, and the latter once.
In odd-numbered columns, the first position is not repeated, and the last repeats.

Hmm, this is a cool format. I’ve never seen this before. Nice and simple! Well, I’ve already got three formats used, I don’t think I need another. Anyways, good work from a fellow hexagon nerd.