Exclusively use broadphase from Unity Physics?

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. :slight_smile:

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.

  1. By “dynamic” I mean bodies that have velocity and are not part of the landscape or scene (those are called “static” in our words :slight_smile: ). Our broadphase consists of 2 trees, one that holds the static geometry, and another that holds moving bodies… Now, from what I understand, you only need the static ones, before you are moving the ball on your own… Both trees can still change and get rebuilt, it’s just that static one gets rebuilt less often since it has fewer changes.
  2. If you had your own AABBs already and just want an AABB overlap, it’s probably best to only utilize our BoundingVolumeHierarchy. Now it may not be all public for you to start, but you can make some types public and let us know what you needed. I have it on my list already to make this public and create a demo, but it just hasn’t been high on the priority list until now.
3 Likes
  • Gotcha. We have indeed bodies marked as static, so working with two trees would be a good approach as well.
  • Cool. I’ll dive into this the next days and let you know how it goes.

Cheers!

1 Like