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:

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:

  1. 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.
  2. 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: