Ammo.js Contact Threshold vs Resting Distance

This article explains the differences between the contact processing threshold and resting contact distances in the ammo.js physics engine. We will explore how these two distinct parameters affect collision detection, simulation performance, and physical stability so you can eliminate jitter and optimize your 3D web applications.

What is the Contact Processing Threshold?

The contact processing threshold (contactProcessingThreshold) is a performance-focused parameter in ammo.js (and the underlying Bullet Physics engine). It determines the maximum distance at which the physics engine will keep and process existing contact points between two rigid bodies.

When two objects collide, ammo.js creates a “contact manifold” containing the points where the objects touch. If the objects begin to separate, ammo.js does not instantly destroy these contact points. Instead, it keeps them active as long as the distance between the objects remains below the contact processing threshold.

What is the Resting Contact Distance?

The resting contact distance (often influenced by the collision margin and solver settings in ammo.js) defines the ideal physical separation or allowable penetration depth when two rigid bodies are at rest.

In real-time physics engines, perfect rigid-body contact is mathematically difficult to sustain without objects sinking into each other or vibrating. To solve this, ammo.js allows a tiny, almost invisible amount of penetration between resting objects. The resting contact distance acts as a target value for the constraint solver:

Key Differences

While both parameters deal with the distance between colliding rigid bodies, they serve entirely different roles in the simulation pipeline:

Feature Contact Processing Threshold Resting Contact Distance
Primary Role Performance optimization and contact caching. Physics stability and collision resolution.
Underlying Mechanism Decides when to discard stale contact point data. Decides how much overlap is tolerated before applying separation force.
Typical Value Slightly larger than the resting distance (e.g., 0.02 units). A very small fraction of a unit (e.g., 0.005 or based on collision margin).
Direct Visual Effect Reduces jitter during sliding or rolling. Prevents objects from sinking into floors or bouncing continuously when stacked.

How to Configure Them in Ammo.js

In ammo.js, you can adjust these properties directly on individual rigid bodies depending on your simulation scale.

// Example: Configuring a rigid body in ammo.js
const rbInfo = new Ammo.btRigidBodyConstructionInfo(...);
const body = new Ammo.btRigidBody(rbInfo);

// Set the contact processing threshold
body.setContactProcessingThreshold(0.02); // Distance in meters/units

// Adjust collision margin to manage resting contact distance
const shape = body.getCollisionShape();
shape.setMargin(0.04); // Default is often 0.04; smaller margins allow closer resting contact

By balancing these two values, you can achieve both high performance and visually stable simulations. Keep your contact processing threshold slightly larger than your resting contact distances to ensure that contacts are maintained smoothly until the objects have clearly separated.