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