How to avoid objects overlapping in a procedurally generated world?

In my procedural project, I’m spawning thousands of objects, like trees, rocks, plants, and cliffs.
Now the thing is, I’d like these to not overlap each other, and I’m kinda struggling with that part.

What I do now is to generate a collection of all possible spawn points. Then I pick a spawn point randomly, based on some selection criteria, and then I check if there’s room to spawn the object. The room checking is fairly simple, and works like this:

protected bool IsRoomForSpawning(Vector3 position, float radius)
{
    var endPos = position + new Vector3(0, 128, 0);
    return Physics.CheckCapsule(position, endPos, radius, GameInfo.Instance.NoWorldGenSpawn);
}

So basically, I call this method for each spawn point. The radius passed in is based on the size of the object I’m trying to spawn.
Originally I did a CheckSphere, but in some cases, the sphere would be fully inside a previously spawned object, and thus would not return true. I then switched to a collider and artificially made it quite tall, to ensure it’s not entirely encompassed by a possible object (Cliffs, for instance, are quite large, and can easily encompass a tree or rock)

The thing is, the above does not seem to do the trick. I still see the exact same results, as if I did not do the above. I do, of course, remember to remove a spawn point from the collection once it’s been used, or deemed invalid.

My concern is that when something is spawned that the colliders are not yet active in the scene, as everything is done within the same flow, at startup- Could this be the case? By this, I mean, assume I’m spawning 1000 objects, in one loop. When placing object 500, and doing the detecting thing above, it will never find anything as the preceding 499 objects, are not yet fully initialized- Could this be the case?

An example of the overlap can be seen below- Notice the tree that is spawned inside of the round thing. Both of these are spawned at generation time, but, as mentioned, shouldn’t overlap.

Any hints or input would be greatly appreciated. Everything else in my spawning engine seems to work perfectly, except this particular thing. I’m trying to keep it as performant as I can, as the check will have to be done for every single object spawned (As mentioned, thousands of them).

Thanks in advance, and thanks for reading :slight_smile:

One way I handled this issue, was to determine what was more important. So in your picture I would assume that the well looking thing, is more important than the tree, or rock that may be underneath it. So within the setup phase I would allow the well to have a Collision check within it(assuming a box collider(disable after setup phase)) and you would also need a rigidbody so collision can occur(disable after setup phase). And in the code of the well it would basically set a list of all things it collided with, then destroy(or disable if object pooling) anything that isn’t more important.

Or you could go crazy with setting a List of positions, and as each object spawns from those iterations it deletes from that List to make sure the same spot isn’t used twice. This is very difficult to produce, but if there is a will, there is a way :wink:

I suggest just collision checking, and letting the battle of who destroys who play out :slight_smile: