Ammo.js Soft Body Solver Iterations and Cloth Intersection

This article explains how the soft body solver iterations setting in ammo.js prevents cloth self-intersection and clipping during web-based physics simulations. It details the relationship between solver passes, physics accuracy, and CPU performance, offering practical insights on optimizing your WebGL physics engine.

In ammo.js (a JavaScript port of the Bullet Physics engine), cloth is simulated as a soft body comprised of vertices, links, and faces governed by constraints. When cloth collides with itself or other 3D meshes, the physics engine must resolve these contacts to prevent the geometry from passing through itself. This resolution is controlled by the solver iterations setting, primarily configured via btSoftBody::Config::piterations (position solver iterations).

Understanding Solver Iterations

The solver iterations parameter defines how many times the physics engine calculates and enforces constraints per simulation step. When cloth vertices collide with an obstacle or another part of the cloth, they generate contact constraints. The solver runs iteratively to satisfy these constraints by pushing intersecting vertices back to the correct side of the colliding surface.

Preventing Tunneling and Intersections

If the solver iterations value is set too low, the physics engine will stop calculating before all constraints are resolved. This results in “tunneling,” where vertices pass through collision boundaries, causing the cloth to clip through itself or solid objects.

By increasing the solver iterations, you allow ammo.js to perform more refinement passes per frame. This ensures that: * Self-collisions are resolved: Vertices folded against other parts of the same mesh are pushed apart successfully. * Stretching constraints are maintained: The cloth retains its structural integrity rather than stretching infinitely or collapsing under gravity. * High-speed collisions remain stable: Fast-moving cloth does not bypass thin collision barriers.

Balancing Performance and Quality

While higher iterations drastically reduce cloth intersection, they come at a heavy computational cost. Because ammo.js runs in the browser CPU via WebAssembly or JavaScript, high iteration counts can quickly bottleneck the main thread, lowering the frame rate.

To achieve optimal results, developers should: 1. Increase Position Iterations (piterations): Values between 4 and 10 are typically sufficient for standard cloth, while complex self-colliding robes may require 10 to 20. 2. Adjust the Substepping/Time Step: Often, pairing a moderate iteration count (e.g., 5) with smaller physics substeps (e.g., 1/120s or 1/240s instead of 1/60s) provides better stability and fewer intersections than simply maxing out the solver iterations. 3. Enable Self-Collision Flags: Ensure that fIterations and self-collision flags (like fcollisions with SBF_SELF_COLLISIONS) are explicitly enabled in the soft body configuration to allow the solver to calculate vertex-to-face and vertex-to-vertex limits.