Physics updates in Unity 2019.3
Unity 2019.3 beta brings quite a few updates to our PhysX integration. This will make the current implementation more precise, faster, and more robust.
We’ve upgraded the PhysX library to PhysX 4.1 from PhysX 3.4. For more information, see the section “Migrating from PhysX SDK 3.4 to 4.0” in NVIDIA’s PhysX 4.1 SDK Guide.
New solver type
We exposed a new solver type in 2019.3, which is called Temporal Gauss-Seidel. It’s available under Edit > Project Settings, in the Physics category. With this new solver type, joints tend to be more resistant to overstretching and various glitches.
New broad-phase type
We also exposed a new broad-phase algorithm, which is called Automatic Box Pruning. It’s similar to the Multibox Pruning algorithm that’s been available for a while but it can compute the world boundaries and amount of subdivisions automatically. It will maintain the set of grid cells and use the regular Sweep And Prune approach to work out potentially overlapping pairs of colliders. This is intended to help with big scenes where a single Sweep And Prune would be producing lots of extra false positives. This mode is the default broad-phase in PhysX 4.1, but we chose to be conservative and keep it disabled by default. If you want to try it out, enable it in the Physics settings under Broadphase Type.
Mid-phase is a set of data structures as well as a set of algorithms. These algorithms pick a small set of potentially intersecting triangles of a mesh whenever you run a physics query or when a collider is close to the surface of a mesh. The idea is that a mesh may have a large number of triangles and you don’t want to be running precise checks for each of them, so we build an acceleration structure that allows the algorithm to pick only a small subset that is worth checking.
This acceleration structure is built during the mesh baking for physics, which normally happens when you instantiate the MeshCollider component and assign it its sharedMesh property. Building that structure can take quite a bit of time, and it makes sense to minimize it. The majority of the time spent during this process in older PhysX versions was in constructing the R-Trees.
PhysX 4.1 has a new algorithm in place that doesn’t require any R-Trees. However, the downside is that it can currently only run on Windows, Mac and Linux targets. It is automatically used on Desktop platforms but can be disabled per MeshCollider in the Cooking Options drop-down menu. On unsupported platforms, it will automatically fall back on the old mid-phase algorithm.
Delayed and multi-threaded mesh baking
As we mentioned before, instantiating a new MeshCollider can take time. Now you can pre-bake a Mesh instance for usage with the MeshCollider using the Physics.BakeMesh function. You can hide the computationally intense mesh-baking process behind a loading screen or transition scenes, like a dialog scene in an adventure game or a cut-scene.
An interesting side effect of this function is that you can actually call it from off the main thread, and thereby spread the baking load across all of the available cores. Check the docs for an example of how to use it with the C# Job System.
Exposing a thread-safe Physics.BakePhysicsMesh(meshInstanceId, isConvex) function to bake the data for MeshCollider on-demand from any thread. Usage example with C# job system (generate N meshes and bake off the main thread): https://t.co/vWIQ64yVqG pic.twitter.com/wsHHsD9Rsx
— Anthony Yakovlev (@AnthonyYakovlev) April 15, 2019
Removed TerrainCollider thickness and Unified Heightmaps setting
Back in Unity 2018.3, we switched to a more robust contact generation path for TerrainCollider, and changed the contacts generation algorithm. Instead of using a special path to handle the heightmap geometry when computing contacts, this newer algorithm treats a portion of the heightmap in contact as a mesh and utilizes the regular mesh contacts generation code. However, we kept the old code around, and it was still available in the physics settings as the Enable Unified Heightmaps option.
The update to PhysX 4.1 removes the Enable Unified Heightmaps option. On top of that, we’re also removing the thickness setting that was available in the Terrain component properties. Terrain thickness was intended to solve the tunneling effect, where fast moving objects would pass through the terrain surface undetected. Now that thickness is gone, we recommended that you use Continuous Collision Detection instead.
When a kinematic Rigidbody is turned into a non-kinematic one, the physics engine will now re-insert it into the broad-phase structures for the purposes of collision detection. This will cause an extra OnTriggerEnter event.
We’ve been focusing on polishing and stabilizing the PhysX integration in Unity, and we hope that this update will bring you improved precision, stability and performance. Get Unity 2019.3 beta to try it out and let us know what you think on the forum!