Comments and answers for "Hexagon-Grid Distance"
http://answers.unity.com/questions/960064/hexagon-grid-distance.html
The latest comments and answers for the question "Hexagon-Grid Distance"Comment by Fr0stbite on Fr0stbite's answer
http://answers.unity.com/comments/960370/view.html
It works! Thank You! You saved me. I was trying to solve it for 2 days :D Thank you again.Tue, 05 May 2015 06:21:33 GMTFr0stbiteAnswer by Bunny83
http://answers.unity.com/answers/960169/view.html
maccabbe posted an excelent link on that subject. However they seem to suggest to use a 3 coordinate system instead of 2. As you can read in the post it has some advantages. However if you want to stick with yours that's not a problem.
Just you look at your structure. You have layed out straight rows. That means if you only move on x axis the number of tiles equals the distance in that direction. Now if you move only along the y axis you also always need as many tiles as it is away in y. The point is if you move 2 times in y you can see that you either end up at the same x position or one x position shifted (left or right). That means for every 2 y moves you can save 1 x move
int GetTileDistance(int aX1, int aY1, int aX2, int aY2)
{
int dx = aX2 - aX1; // signed deltas
int dy = aY2 - aY1;
int x = Mathf.Abs(dx); // absolute deltas
int y = Mathf.Abs(dy);
// special case if we start on an odd row or if we move into negative x direction
if ((dx < 0)^((aY1&1)==1))
x = Mathf.Max(0, x - (y + 1) / 2);
else
x = Mathf.Max(0, x - (y) / 2);
return x + y;
}
Note: the aDelta/2 is an integer division. So it's rounded down. The Max function is needed since we can only save x moves. If we only move along the y axis there's nothing we can save.
***edit***
Forgot the "Abs" on each delta value ^^
***another edit***
Just realized that the "saving" in x moves is a bit different when you move to the left instead of right. I fixed the method and rearranged the code so it's more readable.
the final x and y values are actually the required moves in each direction. Keep in mind that the y moves can contain those "saved" x moves as you can switch direction every step. The method of course calculates the shortest path ^^
*** third edit ^^ ***
I just discovered that the distance also depends on if you start on an odd or even row ^^. I first thought that this is going to bee too complex, but finaly i thing i got a solution that works everytime. Now you don't have to pass the relative vector but the two points in the grid (as you need to know if we start on an odd or even row).
Just in case you're wondering (aY1&1)==1 does the odd even test. Also keep in mind that "^" is the xor operator. So either one or the other should be true but not both.Mon, 04 May 2015 20:28:19 GMTBunny83Comment by Cherno on Cherno's answer
http://answers.unity.com/comments/960161/view.html
Since all adjacent tiles to any given hex are equidistant, you could just dive the distance by the tile diameter and round to the nearest multiple of the diameter.Mon, 04 May 2015 20:11:55 GMTChernoComment by Fr0stbite on Fr0stbite's answer
http://answers.unity.com/comments/960148/view.html
Yeah i also found that blog but i dont realy understand that. Which function returns only the number of the tiles and not the distance vector.Mon, 04 May 2015 19:57:26 GMTFr0stbiteAnswer by maccabbe
http://answers.unity.com/answers/960137/view.html
Read through http://www.redblobgames.com/grids/hexagons/. It describes most hex grid representation and has a section in how to get distances for each of them, not to mention the other things you will find useful for hex grid games.Mon, 04 May 2015 19:39:22 GMTmaccabbe