I’m porting a pinball engine to DOTS where the colliders are all static (apart from ball-ball collisions of course). So my broadphase uses a quadtree stored as a BlobAssetReference
. It’s computed when the game starts and doesn’t change. The ball then checks against that quadtree while moving in order to detect collisions.
Now I’m wondering how hard it would be to just replace my broadphase by Unity Physic’s BVH in order to make it dynamic? Basically:
-
Throw my (now static) AABBs at Unity Physics somehow
-
Update them when entities move
-
Find a way of identifying the overlapping AABBs in the BVH with a given AABB
All the other parts (nearphase, collision, etc) would still be managed by my code.
I have looked a bit at the Physics source but didn’t find any APIs that would allow that. But it’s a huge beast and I probably missed a lot.
Does anyone know if that’s possible? And if so, any hints about the APIs to use for the three points above?
BuildPhysicsWorld system actually does all the things you need. You get a PhysicsWorld which contains a CollisionWorld. CollisionWorld stores a broadphase (also a quad tree) which only gets rebuilt when static layer changes (assuming you don’t have any dynamic bodies).
You should disable all the other systems (StepPhysicsWorld, ExportPhysicsWorld and similar).
Code you could use (assuming you have BuildPhysicsWorld m_BuildPhysicsWorld somewhere):
var physicsWorld = m_BuildPhysicsWorld.PhysicsWorld;
var collisionWorld = physicsWorld.CollisionWorld;
Once you have CollisionWorld it’s up to you to do queries:
- CastRay (cast a ray agains the world)
- CastCollider (cast a collider, maybe a sphere in your case, against the world)
- CalculateDistance (calculate distance between a point or a collider to the closest object in the world)
- OverlapAABB (the one you asked for, overlap an AABB with the world)
And you get all this without needing to step anything, plus BuildPhysicsWorld automatically manages changes in static entities for you.
Don’t forget to put the CollisionWorld into a job and get the great performance of Burst compiled jobs.
I hope this answers your question and helps you get started. I’m here for additional help. 
2 Likes
@petarmHavok Awesome, thanks a lot! Two follow-up questions before I dive into this:
- By “dynamic” bodies you mean bodies that change shape, right? Not bodies that move.
- Which brings me to the next point, my collision world has its own shapes / colliders. How would BuildPhysicsWorld interact with my colliders? Like, how can I make it pull in my AABBs to do its thing? And how do I notify it when they change?
Thanks again, you guys are brilliant.