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

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?

This might help!

http://docs.unity3d.com/Documentation/ScriptReference/Physics.OverlapSphere.html

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?

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.

Vector3.Distance