My project is built on the standard gameobjects framework and is not using DOTS/ECS at the moment.
For a certain system I need to perform spatial queries (Raycasts, Overlaps etc.) against a static and fairly large collection of colliders that is only initialised at scene load.
The current solution is to create the colliders on a separate layer and use the standard Physics.Raycast etc methods. This however creates an additional ~50K gameobjects in the scene and this overhead is very noticeable on level load.
The Unity Physics package looks like it should allow doing something like this, but it pulls the whole ECS system with it.
So I was wondering, is it feasible and practical to use the Unity Physics package to only handle static collision queries (i.e., no physics simulation, no other systems necessary – just the barebones physics world for spatial queries) or would it be more straightforward to interface with a native physics library like PhysX or Bullet for this?
You’re better off just taking the BVH it uses and implementing that. I’ve done it a couple of times and it’s not that hard as you have physics as reference and can copy/paste most of what you need.
You can use only the broadphase in Unity Physics by simply only creating static rigid body entities.
These will populate the bounding volume hierarchy within Unity Physics without causing any simulation of dynamic force responses, e.g., due to collisions. Then, you can use raycasting or other collision queries such as distance queries etc. on the resultant rigid bodies.
You can decide to use the lower level of Unity Physics for this purpose (Unity.Physics.PhysicsWorld which contains a CollisionWorld that you can populate directly in code) or go through ECS by creating Entities on which you add specific components, such as PhysicsCollider to represent collision shapes, and have Unity Physics convert it into a PhysicsWorld for you automatically.
Both work fine for your use case.
An example that could be interesting for you is the Pool demo in our PhysicsSamples project.
See here:
The demo scene is called “6b. Pool.unity”. It bridges the GameObject and Entities world. And it creates a second simulation under the hood in which it predicts the trajectoried of billiard balls using a manually updated Unity Physics simulation.