Adjust ammo.js Soft Body Bending Constraints

This article explains how to adjust the bending constraints of a soft body cloth in ammo.js to control its stiffness and behavior. You will learn about the key properties involved, how to generate bending constraints, and how to configure the material stiffness coefficients to achieve realistic cloth physics in your web-based 3D applications.

In ammo.js (the JavaScript port of the Bullet Physics engine), cloth is represented as a soft body. By default, a soft body cloth only has structural links, which can make it feel extremely floppy or prone to folding like paper. To make the cloth behave more realistically, you must explicitly generate bending constraints and configure their stiffness.

Step 1: Create or Retrieve the Soft Body Material

Bending constraints are governed by the properties of the material assigned to them. You can access the default material of the soft body or append a new one specifically for bending.

// Access the default material (index 0) of your soft body
var material = softBody.get_m_materials().at(0);

Step 2: Set the Stiffness Coefficient

The primary property that controls bending stiffness is the linear stiffness coefficient (m_kLST). This value ranges from 0.0 (fully flexible/no resistance) to 1.0 (highly rigid).

To adjust the bending stiffness, use the set_m_kLST method on the material:

// Set stiffness: 0.1 for loose cloth, 0.8+ for stiff cardboard-like material
material.set_m_kLST(0.5); 

Step 3: Generate the Bending Constraints

After configuring the material, you must generate the bending links across the soft body’s node grid. This is done using the generateBendingConstraints method.

var connectionDistance = 2; // The distance between connected nodes
softBody.generateBendingConstraints(connectionDistance, material);

Step 4: Configure Soft Body Solver Iterations

For bending constraints to resolve properly, the physics solver needs enough iterations to calculate the forces. If your cloth still droops or bends too easily despite high stiffness values, increase the position solver iterations in the soft body configuration:

var config = softBody.get_m_cfg();
config.set_piterations(10); // Default is often 1; increase to 5-10 for stiffer cloth

By balancing the connection distance, the m_kLST stiffness coefficient, and the solver iterations, you can achieve any cloth behavior from silk to heavy canvas.