btDiscreteDynamicsWorld vs btSoftRigidDynamicsWorld in Ammo.js
When developing 3D physics simulations for the web using ammo.js (the
Emscripten port of the Bullet physics engine), choosing the correct
physics world is crucial for both functionality and performance. This
article compares btDiscreteDynamicsWorld and
btSoftRigidDynamicsWorld, highlighting their core
differences, feature support, and performance characteristics to help
you select the right world for your project.
Core Definitions
btDiscreteDynamicsWorld: This is the standard, go-to physics world in ammo.js. It is designed purely for rigid body simulation. It handles non-deformable solid objects (like boxes, spheres, and custom collision meshes), gravity, collisions, and constraints (joints) between rigid bodies.btSoftRigidDynamicsWorld: This is an extension ofbtDiscreteDynamicsWorld. In addition to everything the discrete world does, it adds support for soft bodies. Soft bodies are deformable objects such as cloth, ropes, flags, and squishy volumes.
Key Differences
1. Support for Deformable Physics (Soft Bodies)
The primary differentiator is the ability to simulate soft bodies. *
In btDiscreteDynamicsWorld, you cannot add
soft bodies. If you attempt to do so, they will not render or simulate.
* In btSoftRigidDynamicsWorld, you can
seamlessly simulate interactions between rigid bodies and soft bodies.
For example, you can drape a cloth over a rigid sphere, or anchor a
swinging rope to a solid ceiling.
2. Performance and CPU Overhead
Physics calculation is often the main bottleneck in web-based 3D
applications. * btDiscreteDynamicsWorld is
highly optimized and lightweight. It uses efficient algorithms for
collision detection and constraint solving, making it the best choice
for achieving a smooth 60 FPS in standard browser environments. *
btSoftRigidDynamicsWorld carries
significantly higher CPU overhead. Simulating the vertex-by-vertex
deformation of cloth or soft volumes requires solving complex
mass-spring systems. Even if you do not have active soft bodies in the
scene, the underlying solver structures in a soft-rigid world can
introduce slight overhead compared to the pure discrete world.
3. Setup and Boilerplate Code
Setting up the two worlds in ammo.js requires different initialization steps.
A btDiscreteDynamicsWorld requires a
basic configuration: * Collision configuration
(btDefaultCollisionConfiguration) * Collision dispatcher
(btCollisionDispatcher) * Broadphase interface
(btDbvtBroadphase) * Constraint solver
(btSequentialImpulseConstraintSolver)
A btSoftRigidDynamicsWorld requires
extra components to handle soft body mathematics: * A soft body solver
(typically btDefaultSoftBodySolver) * A specialized soft
body collision configuration
(btSoftBodyRigidBodyCollisionConfiguration) * The world
initialization helper must explicitly register the soft body solver
during creation.
Summary: Which One Should You Use?
- Use
btDiscreteDynamicsWorldif you are building a standard game, a vehicle simulation, a platformer, or any application where objects do not need to bend, stretch, or tear. It offers the best performance and is easier to configure. - Use
btSoftRigidDynamicsWorldif your project absolutely requires realistic cloth simulation, ropes, sails, or squishy, jelly-like objects that must deform upon colliding with the environment.