Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by PacRat · Oct 25, 2021 at 11:52 PM · terraingridtilesradiusaoe

How to find a circle (radius) in grid tiles around another tile

Hey everyone,

I have made a grid system to store some data, and im wondering about the best/most performant approach to find a radius of tiles around a certain tile, or potentially multiple tiles. This radius would need to be scalable, and needs to operate every frame or close to if possible.

I have a few ideas on how to do it but they seem either super heavy or involve manually creating a circle shape for each size.

Some examples would be radius of influence in city builder games, or area of effect for explosions etc in games like xcom. If you have done something similar or have any advice, i'd really appreaciate the help!

Thanks :)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Eno-Khaon · Oct 26, 2021 at 12:22 AM

If all you need to know is whether objects are within a radius, rather than needing to worry about their specific distance relative to it, you can cut out the Square Root element of Vector Magnitude calculations and stop short at their Square Magnitude instead:

 // Example, radius is already cached from (radius * radius) as applicable
 if((testedTile.position - middleTile.position).sqrMagnitude < radiusSquared)
 {
     // etc.
 }
Comment
Add comment · Show 3 · 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 PacRat · Oct 26, 2021 at 02:10 AM 0
Share

Hmm ok, so do you think I should sample a square area with the dimensions of my radius, and then use this to sample each tile inside of that area, then remove the ones that aren't in range?

This could potentially be pretty heavy if I am sampling areas with a radius of 50 or more right?

avatar image Eno-Khaon PacRat · Oct 26, 2021 at 04:46 AM 1
Share

If you have the information already available, it shouldn't be so bad, really. For example, if you have your tile information in a 1-or-2-dimensional array already (or similar structure, at least), then it shouldn't be too tough to cycle through them:

 // assu$$anonymous$$g 1-dimensional array to illustrate the
 // slightly-more-complex way of preparing data
 // Example: explosion centered on (2, 315), radius 50.5
 int tileX = 2;
 int tileY = 315;
 Vector2Int tileXY = new Vector2Int(tilex, tileY);
 float radius = 50.5f;
 float sqrRadius = radius * radius;
 int radiusCeil = Mathf.CeilToInt(radius);
 int searchMinX = Mathf.Max(tileX - radiusCeil, 0);
 int searchMaxX = Mathf.Min(tileX + radiusCeil, gridWidth);
 int searchMinY = Mathf.Max(tileY - radiusCeil, 0);
 int searchMaxY = Mathf.Min(tileY + radiusCeil, gridHeight);
 
 Vector2Int currentPos; // maximize speed by declaring it only once
 for(int y = searchMinY; y < searchMaxY; y++)
 {
     currentPos.y = y; // replace value in-place without creating a new Vector2Int
     for(int x = searchMinX; x < searchMaxX; x++)
     {
         currentPos.x = x;
         if((currentPos - tileXY).sqrMagnitude <= sqrRadius)
         {
             // Tile is in range.
             grid[y * gridWidth + x].isInRange... // (etc.)
         }
     }
 }



There's a bit of redundancy in this (i.e. tileX, tileY, tileXY), but it's entirely for the sake of maximizing speed by only using struct types when optimal (as opposed to using values directly). It shouldn't be necessary for a small size like 50x50, but the more you do with each tile in the process, the more every bit will make a difference.

Namely, the example of grid[y * gridWidth + x] works under the assumption that the grid tiles are all accounted for in full, so that you aren't using GetComponent<>() or anything woefully inefficient like that. For example, even if they're all GameObjects with a "GridTile" script attached to them, those "GridTile" scripts would have all relevant references necessary on them, such as Material/Renderer/Sprite/whatever for immediate use.

Edit: Oh, right. 50 radius definitely means more than 50x50, but whatever. 2500 vs. 10000 iterations... Meh, I could substitute the 50's with 25's (i.e. diameter / 2), but I don't think the point's been lost here.

avatar image PacRat Eno-Khaon · Oct 26, 2021 at 05:46 AM 0
Share

this is awesome, way more than I expected! That should work well, my grid is all based in a 2 dimensional array so this should be pretty easy to apply. Thanks heaps I really appreciate the help @Eno-Khaon

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

127 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to draw a grid over parts of the terrain? 7 Answers

Texture grid displayed oddly when width =/= height 1 Answer

How to generate a grid for procedurally generated platforms 1 Answer

How to create a rotating tiles with walls on edges of tiles? 0 Answers

A* Can't Scan GridGraph 0 Answers


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