Ammo.js Soft Body and Rigid Body Collisions
This article provides an overview of how the ammo.js physics engine calculates and resolves collisions between soft bodies and rigid bodies. It covers the underlying mechanics of node-based collision detection, impulse-based resolution, anchoring, and the critical configuration settings required to achieve realistic interaction between deformable meshes and solid objects in web-based 3D environments.
The Simulation World Context
To enable interactions between soft bodies (such as cloth, ropes, or
deformable volumes) and rigid bodies, ammo.js uses a specialized physics
world called btSoftRigidDynamicsWorld. This class inherits
from the standard rigid-body dynamics world but includes a dedicated
solver capable of processing both rigid constraints and soft body node
dynamics in a unified simulation loop.
During each simulation step, the engine performs collision detection, generates contact constraints, and updates the positions and velocities of both types of bodies.
Collision Detection: Nodes and Faces
Soft bodies in ammo.js are represented as a network of masses (nodes/vertices) connected by springs (links). Because soft bodies do not have a single, rigid transform, collision detection relies on analyzing these individual elements against the boundary shapes of rigid bodies:
- Vertex-to-Rigid Body: The engine checks if individual soft body nodes have penetrated the collision shape of a rigid body. If a node passes inside a rigid body, a collision is registered.
- Face-to-Rigid Body: For soft bodies with defined surface faces (like cloth), the engine can check if the rigid body has penetrated the triangles of the soft body mesh, preventing thin rigid objects from passing through the soft body.
These checks are governed by the soft body’s configuration
parameters, specifically the collision flags
(m_cfg.collisions). Setting this flag determines whether
vertex-to-rigid, face-to-rigid, or both types of detection are
active.
Impulse-Based Collision Response
Once a penetration is detected, ammo.js resolves the collision using an impulse-based constraint solver. The response must satisfy Newton’s third law of motion—exerting equal and opposite forces on both objects:
- For the Soft Body: The engine calculates the penetration depth and normal at the contact point. It then applies a corrective impulse directly to the affected soft body nodes, pushing them out of the rigid body’s volume along the collision normal.
- For the Rigid Body: The sum of the impulses applied to the soft body nodes is calculated. An equal and opposite impulse is applied to the rigid body at the exact point of contact. This contact force alters the linear and angular velocity of the rigid body, causing it to react to the soft body (e.g., a falling heavy ball being slowed down by hitting a trampoline).
Anchors and Joint Connections
Beyond passive collisions, ammo.js allows you to physically link soft bodies to rigid bodies using anchors.
Using the appendAnchor method, you can bind specific
nodes of a soft body to a local position on a rigid body. When anchored,
the engine treats the connection as a hard constraint. The anchored soft
body nodes will move dynamically with the rigid body as it translates
and rotates, while the rest of the soft body behaves fluidly based on
its internal physics. This is commonly used to attach ropes to rigid
anchors or flags to moving poles.
Material Properties and Tuning
The behavior of the collision response is heavily influenced by the physical material properties defined in both bodies:
- Friction: The friction coefficients of both the soft body and the rigid body determine how much sliding resistance occurs when they rub against each other. High friction allows cloth to drape and stick to rigid surfaces.
- Restitution (Bounciness): This determines how much kinetic energy is preserved during an impact.
- Deformability and Stiffness: Properties within the soft body’s material (like structural, shear, and bending coefficients) dictate how easily the soft body deforms upon impact with a rigid object, and how quickly it attempts to return to its original shape.