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

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?