Ammo.js Soft Body Volume Preservation Configuration
This article provides a direct guide on configuring volume preservation for closed soft bodies in the ammo.js physics engine. You will learn the specific properties, coefficients, and methods required to make 3D soft meshes maintain their shape and prevent deflation during physical simulations.
To enable volume preservation in an ammo.js soft body, the mesh must first be completely closed (manifold). Once you have initialized the soft body helper with a closed mesh, you must configure the soft body’s pose, volume conservation coefficient, pressure, and solver iterations.
1. Enable Pose-Based Volume Tracking
The soft body needs to capture its default state to understand its
original volume. You must call the setPose method on your
soft body instance.
// Arguments: setPose(bool bvolume, bool bframe)
softBody.setPose(true, false);Passing true as the first argument tells ammo.js to
track and preserve the volume of the soft body based on its initial
shape.
2. Configure the Volume Conservation Coefficient (kVC)
You must access the soft body’s configuration object
(m_cfg) and set the volume conservation coefficient
(kVC). This coefficient determines how strongly the body
resists volume changes.
const config = softBody.get_m_cfg();
config.set_kVC(20.0); // Adjust this value based on desired stiffness- Low values (e.g., 1.0 - 5.0) allow the body to compress more easily.
- High values (e.g., 20.0 - 100.0) make the body rigid and highly resistant to volume loss.
3. Apply Internal Pressure (kPR) (Optional)
If you are simulating an inflated object like a balloon or a ball,
you can apply internal pressure using the pressure coefficient
(kPR). This works in tandem with volume preservation.
config.set_kPR(1.5); // Sets the internal pressure of the soft body4. Increase Position Solver Iterations
Volume preservation constraints require more computational
calculations to remain stable. If your soft body collapses or behaves
erratically under stress, increase the position solver iterations
(piterations).
config.set_piterations(10); // Default is typically 1; increase for stabilityComplete Configuration Example
Below is the consolidated code block required to set up a volume-preserving soft body in ammo.js:
// Assuming 'softBody' is your btSoftBody instance
softBody.setPose(true, false);
const config = softBody.get_m_cfg();
// Enable volume conservation and pressure
config.set_kVC(20.0); // Volume conservation coefficient
config.set_kPR(2.0); // Pressure coefficient (optional)
// Increase solver iterations to maintain physics stability
config.set_piterations(10);