Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Calculate which cells are affected by a unit's radius?

Discussion in 'Scripting' started by Velo222, May 30, 2013.

  1. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    I have a regular square grid in which the grid cells are evenly distributed in terms of distance from each other. But I'm having a hard time figuring out how to tell which cells on the grid are currently influenced by a given unit's radius at any given time.

    For instance, say I have a given unit whose radius is 5 units. And say the grid cells are each 1 unit apart. How do I find the cells that fall within this unit's radius (dynamically)?

    So far, I have an algorithm that can calculate the closest cell in my grid to a given unit's location. But that's it. I'm also looking for speed here, as I'd like the algorithm to be as fast as possible of course. Could anyone give me an example of how to retrieve the cells, or articles explaining how to do this?
     
  2. Jtbentley_v2

    Jtbentley_v2

    Joined:
    Sep 5, 2012
    Posts:
    174
  3. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    Thanks for the reply Jtbentley.

    I thought about using that function, but my grid cells do not have colliders. They are simply arrays of classes with certain variables. I was also trying to avoid using physics, as I heavily use physics for other parts of my game. I'm not completely ruling out using it though, if it gives the kind of performance I'm looking for.

    If I have 200 units all needing cells within their respective radii, I think physics checks might become a performance problem. However, I haven't tested it yet, so I could be wrong.

    Are there any non-physics based ways to do it?
     
  4. KaBKa

    KaBKa

    Joined:
    May 20, 2013
    Posts:
    34
    you could index the locations of every ball and then do 200*200 distance checks to see if they are touching.

    Jtbentley is right though.. use the built in physics most work arounds you could come up with will have a larger performance impact then just doing it right.
     
  5. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Vector3.Distance