What would be the best way to calculate how many hexes to the designated spot?

I’m working on a tile-movement system and have encountered a roadblock. I’m unsure of how to calculate the amount of hexes from the NPC’s tile to the tile the player wants it to go to. I need a way to find out the amount of hexes to the target.

Hex image:
tiles
ba
The distance would be the distance in-between the target and npc, and including the target.
So, the distance shown in the image would be 1 tile to travel.

How would I calculate the number of tiles between any given target and the NPC’s location?

Can’t you divide total distance by length of the tile

me idiot

thanks for the idea though

If it’s only straight lines then the length of the tile would work, but if you have a grid of hexes and the player is 2 studs to the left then 2 studs up and left diagonally then you would have issues.

Would pathfinding with waypoints set at the centers of each tile be better? You should be able to calculate the number of waypoints required/allowed. I’m not good at pathfinding, so that’s the only help I can suggest.

1 Like

As @Scottifly has said, Unfortunately this won’t work exactly.
The centrepoints of hexagonal grids aren’t evenly spaced in every direction beyond the first adjacent hex.
The next ring is either twice the distance of the first ring or sqrt3. It only gets more complicated after that.

If you have a coordinate system for the hexes you can use vector math to calculate the number of steps between any 2 points. if you’re feeling brave

2 Likes

A coordinate system seems like it would work, I’ll try it out

1 Like

Since there are only 6 directions you can move in, you can get the unit vector for the direction to the target. You can then round that direction to point to the closest of the 6 directions. Repeat until you reach target and count how many times you had to do it.

1 Like