Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Fr0stbite · May 04, 2015 at 05:20 PM · mathdistancegridvectormathf

Hexagon-Grid Distance

How is it possible to calculate distance between two tiles? I want to get the number of tiles of the distance;

Here is my grid: alt text

i tried a lot of solution till this the best was this

 var distanceVector : Vector2 = Vector2(Mathf.Abs(a.pos.x-b.pos.x),Mathf.Abs(a.pos.y-b.pos.y));
 distance = Mathf.Ceil(Mathf.Sqrt((distanceVector.x*distanceVector.x)+(distanceVector.y*distanceVector.y)+(distanceVector.x*distanceVector.y)));
grid.png (30.0 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Bunny83 · May 04, 2015 at 08:28 PM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fr0stbite · May 05, 2015 at 06:21 AM 0
Share

It works! Thank You! You saved me. I was trying to solve it for 2 days :D Thank you again.

avatar image
2

Answer by maccabbe · May 04, 2015 at 07:39 PM

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fr0stbite · May 04, 2015 at 07:57 PM 0
Share

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.

avatar image Cherno · May 04, 2015 at 08:11 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I calculate the Vector3 b using Vector 3 a pos with an angle? 1 Answer

Finding Nearest Object Both Positive And Negative? 1 Answer

How can I have a distance between 2 points = 0-1 (min = 0, max = 1) and in between is a decimal? [C#] 2 Answers

Mathf repeat at the end? help! 1 Answer

finding point on the vector 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges