Please Help with Spawn Area Clearance Checking

Alright so I’m working on an a-life simulation in Unity. A really basic one to start with. But I’ve run into a problem with the reproduction of my first, generic plant.

The plant is a small 0.2 meter green cube at full growth. It’s got a rigidbody and a spherecollider so it’s fully physical, and the physics are set to continuous so that it doesn’t fall through the plane I’m using for the ground. The plant shrinks down 99% on spawn, and gradually grows to its full size based on some properties that can be changed.

When the plant reaches full size, it begins its reproductive cycle. It’s basically another percentage that starts at 0.0 and goes up to 1.0 at a rate set by a parameter. In this, most basic of plants, I’ve decided to simulate growth by shoots. In effect, the plant spawns a new plant prefab somewhere within a circular area defined by minimum and maximum reproduction range values. This is about where i start having problems.

I don’t want my plants to stack on top of eachother or spawn where there is already an object apart from the ground plane it spawns on top of. I also don’t want my plants to spawn outside of the ‘world’, which is currently a flat plane bounded on four sides with invisible flat planes.

So I’ve got a ring defined by two circles representing the area in which the plant can spawn, and I need to scan that area and eliminate regions where other colliders are, and regions that would place the new plant off the edge of the world, and then from the area that remains, randomly choose a location to spawn.

So my problem is, how do I do that?

Now, if you’ve got a good idea how to do that, go ahead and start writing your reply before I fill you in on the way I tried to do it before and had problems.

Here’s what I tried already. I figured that checking a circular area would be really difficult, as would checking the theoretically infinite area between zero and one, so I turned the area into squares or cubes in my imagination, and in the code I turned the circle into a bunch of points, each as far away from eachother as a full grown plant. My plan was to check each of these areas for colliders and eliminate them from a list of potential points to spawn into, and then when I had my final list I would do a simple random roll to pick a spot to spawn. If there were no spots to spawn in the reproduction area, the plant would hold its reproduction at 100% and try to do it again in a second.

So to accomplish this, I made a List<List> and set the Capacity of the List and all the nested Lists to 2 * reproductionMaxRange + 1. Early on I falsely believed that this would create null value list entries. It didn’t. So I made a square of Vector2 points instead of a circle, and set them to 0,0 since the corner of a square that contains a circle will never be included in that circle. I could thus throw out any square set to 0,0.

Once I had my square of points I used the midpoint circle algorithm to make a pair of squared circles in my List of Lists. I started working on a scanline based flood fill algorithm to fill in the space between the two circles when I realized something was wrong. I put in some debug code and found that my earlier code was putting the inner circle in the corner instead of the center of the outer circle. In my attempts to fix this I got a bug in my code and gave up for the night.

After I had my circle of possible spawn location points, I planned to use a series of OverlapBox calls to check for colliders or out-of-world invalidity, and then randomly choose from the list that remained to spawn my new plant in. Or hold the reproduction for a second and do the whole thing again until there was an open space.

Now that I’ve written it all out I think it might still work if I do it that way. I just got frustrated by a bug and needed to ‘zoom out’ and look at the whole thing from a different perspective. But I could still use your help, since I’m not sure this is the best way to do it.

How would you reccomend I do this?

here is how I would do it.
I suppose that your plant should always spawn close to the spawner, and not at a distance, so I would do something like…
take spawnerPos + Vector3(plantSize, 0f, 0f);
This is a point, an appropriate distance from the spawner where to spawn. Now we need to make sure it does not intersect the edge of the world (add colliders for that) or plants.
So using the above position, I would do a Physics.Spherecast, with the size of my plant, and no movement.
if we did collide with a world edge or plant, using a quaternion move the target position around my spawner, keeping the same distance. do so N steps, where N is a compromise between precision and execution time (a small N will result in available spaces being skipped). if none of the N positions worked, welp, there is no available space.