Still very new to ECS. But it’s starting to slowly click.
I’m spawning a bunch of obstacles in an entity command buffer, since I’ve read that that’s the correct way to handle entity creation/destruction.
I wanted to figure out how to ensure that the obstacles don’t spawn within each other’s collision bounds. It’s pretty trivial to check to see if a point is colliding with an object once that object has already been instantiated, but how can I do the same thing if that entities instantiation is still queued up in an ECB?
I know I can calculate the bounds of he object based on the size and shape of the prefab, is that really the way to do it?
NativeList<DistanceHit> outHits = new NativeList<DistanceHit>(Allocator.Temp);
//Set on Entity physic custom physics shape on objects you want to check against set the Shapes setting Belongs to 1.
CollisionFilter overlapSphereCollisionFilter = new CollisionFilter()
{
BelongsTo = 1,
CollidesWith = 2,
GroupIndex = 0
};
if (_collisionWorld.OverlapSphere(localToWorld.Position, 2, ref outHits, overlapSphereCollisionFilter))
{
//Check if any objects exist in ouHits. If there is don't spawn the object. or delete any objects in the way
}
Something like this in a Job should do the trick. Sphere collisions are a fast easy way. I’m sure there’s a lot of ways. I’m unsure of the way your spawning things. might be easier to do a spawn in a double for loop and then do a random offset within limits of your spacing. If you want to get a little fancier look at Poisson disc sampling.