Configure Friction in Ammo.js Rigid Body
Configuring the friction coefficient of an individual rigid body in ammo.js is essential for controlling how physical objects slide and interact within your 3D physics simulation. This article provides a direct guide on how to set and adjust this property using the native Ammo.js API, enabling you to create realistic material interactions such as icy slopes or high-traction surfaces.
To configure the friction coefficient for an individual
Ammo.btRigidBody, you must call the
setFriction method directly on the rigid body instance.
This method takes a single floating-point number as an argument.
Setting the Friction Coefficient
The friction value typically ranges from 0.0 (completely
frictionless, causing objects to slide indefinitely) to 1.0
or higher (very rough, causing high resistance to sliding).
Here is the direct code implementation:
// Assuming 'rigidBody' is an already instantiated Ammo.btRigidBody
var frictionCoefficient = 0.5; // Default is usually 0.5
// Apply the friction coefficient to the rigid body
rigidBody.setFriction(frictionCoefficient);How Ammo.js Calculates Combined Friction
By default, when two rigid bodies collide, Ammo.js calculates the resulting friction by multiplying the friction coefficients of both interacting bodies:
\[\text{Combined Friction} = \text{Friction}_A \times \text{Friction}_B\]
If you want an object to be completely slippery, setting its friction
to 0.0 will guarantee that any collision involving this
object has zero friction, regardless of the other object’s friction
value.
Rolling and Spinning Friction
For spherical or cylindrical objects, standard friction might not stop them from rolling indefinitely. To configure resistance against rolling and spinning, Ammo.js provides dedicated methods:
// Set rolling friction to prevent infinite rolling
rigidBody.setRollingFriction(0.1);
// Set spinning friction to resist rotation on the contact axis
rigidBody.setSpinningFriction(0.1);Applying these coefficients ensures precise control over how individual rigid bodies behave during contact and movement within your physics world.