Honeycomb top-down hex grid setup?

How would I go about setting up the grid graph for a honeycomb top-down hex grid?

What I’m starting with:

)

Tile width is 120px, height is 140px, pixels per unit is 120, node size is 120/140, aspect ratio is 140/120. Am I doing something wrong here?

I tried messing with different isometric angle and rotation values but without much luck.

Thanks in advance! Much love for the project.

I’ve only just started using AStar, but I recall reading if you change the number of node connections to 6 on a grid graph, you gain the option to make the grid hexagonal.

Might be mistaken, but take a look while waiting for me to be corrected :slight_smile:

1 Like

Hi

@Alec_Stanley
That is correct.
This is also explained in the documentation: http://arongranberg.com/astar/docs/class_pathfinding_1_1_grid_graph.php

In the future I will likely introduce a separate hexagon graph type which inherits from grid graph to make it easier to discover.

yeah, I tried that, it’s the first thing I tried, and it didn’t work, possibly due to:


following the suggestions in the post about dropping 2D and rotating sprites I could use it, but the graph gets too imprecise the more in depth it goes

the solution that worked for me was rolling my own gridgraph
http://arongranberg.com/astar/docs/writing-graph-generators.php

Hi

The rotation that you need to use is maybe not that obvious (which is also a reason I want to create a separate hexagon graph type which can provide these settings preconfigured). That you need to rotate it in an odd way is because if you rotate it -90 on the x axis to be aligned with the 2D plane, that will hit a gimbal lock.

If you rotate it x=45, y=90, z=90, that will give you a nice 2D hexagon graph. Here are the settings that I used

Also. In the hexagon layout, the grid graph does not a node size that corresponds to what you usually want to use unfortunately. For example, to make the distance from the hexagon center to the edge of a hexagon be D world units, you would have to set the node size to Dsqrt(2) (approximately D1.4142), and to make the distance from the hexagon center to neighbouring nodes be D world units, you would have to set the node size to Dsqrt(3/2) (approximately D1.225). This is another reason I want to create a separate hexagon graph because it is not obvious that it needs to be scaled that way.

Hey, thanks for helping!

This is what I get with node size 1.225 and aspect ratio 1:

As you can see, the bottom and topmost centers on tiles don’t match. Horizontally they seem to match.

If I change the aspect ratio, I need to change the rotation, and I’m at a loss to what.

Hi

I checked the measurements in a scene I created, and it seems that while 1.225 does give some error, it’s very small, only a few millimeters at that distance. If I use a better approximation to sqrt(3/2) ≈ 1.224744871 the error is even smaller.

What texture are you using for your background? Do you think you could send it? It might be the texture that is slightly off.

Yeah, you are completely right, I was using the single tile texture dimensions as correct for hex tiles. Once I turned my calculations from 120x140 to 120x138.56 everything works perfectly.

Thanks a bunch! Sorry for making such a mess out of something so simple.

1 Like

Great that it works now :smile:

It is unfortunate that a hexagon graph never saw the light of day.

I’ve been trying to use APP for a hexagon graph, searching every thread in this forum to figure it out, and I’m stumped because APP’s hexagons are not the same size (dimensions X/Y) as my Sprites, and I find no way to customize the width/height of the hexagons (just the “size”).

I could change my hexagon sprite TILES to the correct dimensions assumed by A*PP, but I do not know what those are. It’s given me enough of a headache I’ve just decided to head over to www.redblobgames.com/grids/hexagons and just implement it myself, one section at a time, until I don’t need any third party asset. It’s less of a headache than sifting through documentation/source for a feature which doesn’t seem very popular. (I even bought Grid2D due to its claimed feature supporting Hexagons, but it is even more of a headache because it creates additional problems in addition to my mismatched Hexagon shape & imprecise math. Bleh.)

This is always what happens, with both A*PP & Grid2D assets. I am sure it’s my bad math, but I’ve spent a few hours and cant even find what dimensions/aspect ratio their hexagons have by default. So I can’t do the math without the numbers.

Hi @CarterG81

You are correct, I really should note the sizes of the hexagons in the documentation.
Here is an illustration I did that shows the dimensions of a hexagon:

In your case, you likely want to set the node size to some factor of sqrt(3/2) = 1.225 in order to get a hexagon “width” of 1 like in the image below:

Let me know if these illustrations are understandable or not. I am used to a lot of geometry, so it is sometimes hard to spot the things that need further clarification.

I’m posting an additional illustration here as it might be useful for some other people coming across this thread.
Setting the node size to some multiple of sqrt(2) = 1.4142 is useful if you want the horizontal distance between the hexagons to be an integer:

Thank you.

After failing hexagonal pathfinding with A*PP, I went ahead and read a lot of research, tried Grid2D & HexKit paid assets, and re-read said research probably 3-5 times in full & 10+ re-skimming afterwards. I think it is the math that is far above my head, or today is an off day. I have never in my entire life had such a difficult time learning something.

Either way, 12 hours later and I end up back here - thinking A*PP may be my only hope. So this helps a lot. Especially when I felt as if I learned so little after those 12 hours of google, redblob articles, tutorials, assets, & documentation. Idk how, but being unable to find the size of the hexagons & my inability to just customize the dimensions really set me down a bad path. (My own fault).

The most unfortunate part of all of my day is not that I spent 12 hours ending up totally failing, but that this was 12 hours in what was suppose to be a 48 hour game jam to test my ability & push myself forward. “Only do what you know how to do” as well. I thought I could handle this easily, because A*PP took me only a few hours to do a way more complicated grid graph & game. I was trying something simple this time, but the math & geometry is so far beyond me. I really regret sleeping my way through school & avoiding math even in college, heheheh. Today was so frustrating & such hard work, with so many walls, I felt like quiting gamedev permanently and pursuing a completely different career (heresy for me, as gamedev has been the dream I’ve been hardcore pursuing since 2009, haha).

Thanks again. This really is very helpful.

I am hoping that after today’s Crunch & total failure, I will be able to start fresh tomorrow, use some of this information, and finally get it a simple pathfinding system working with hexagonal shapes. I don’t know why going from squares to hexagons somehow complicates things so much for me that I chase the rabbit of frustration all the way into a black hole of frustration.

My biggest mistake was probably leaving APP to pursue self learning, and upon failing, not returning to APP and instead going to two other paid assets which claimed super-easy 2D hexagonal simplicity. A*PP is definitely the best, even when it isn’t perfect. It’s the closest I got.

I spent a few hours trying again, and I have some success. Acceptable!

I did the math & studied it more, and now I understand why everyone uses MeshRenderers instead of Pixel-Perfect SpriteRenderers. Pixel art hexagon tiles & hexagons + Unity navigation assets do not work perfectly together because pixels can’t be in fractions (and even when I tried that, it failed). I think. Maybe it’s bc I still didn’t get it, but that’s okay.

Anyway, I used what I read on the forms combined with the numbers you gave me and voila! I was able to set a node size that was accurate enough that it works well. As long as I never try to make my map any larger.

At the very very edges of my map, it’s misaligned by a few pixels, but that’s not a big deal at all. Those tiles will most likely be inaccessible anyway, so the grid graph matches my tiles with enough precision it works good!

Thanks again.

1 Like

Great that you got it working!