How to Adjust Restitution in Ammo.js
This article explains how to configure and adjust the restitution—commonly referred to as bounciness—of rigid bodies in the ammo.js physics engine. You will learn how to set the restitution coefficient on a physical body, how ammo.js calculates bounces during collisions, and how to ensure both interacting objects are configured correctly to produce the desired physical reaction in your 3D application.
Understanding Restitution in Ammo.js
In physics engines, restitution determines how much kinetic energy is retained after two objects collide. * A restitution value of 0.0 represents a completely inelastic collision (the object will not bounce at all and will stop upon impact). * A restitution value of 1.0 represents a perfectly elastic collision (the object will bounce back with the same amount of energy it had before the impact).
In ammo.js, restitution is set directly on the rigid body
(btRigidBody) rather than on a separate material
object.
Setting Restitution on a Rigid Body
To adjust the bounciness of an object, you must call the
setRestitution method on its btRigidBody
instance.
Here is the basic code implementation:
// Assuming 'rigidBody' is an already created instance of Ammo.btRigidBody
const bounciness = 0.8; // High bounce
// Set the restitution value
rigidBody.setRestitution(bounciness);To retrieve the current restitution value of a rigid body, use the corresponding getter method:
const currentRestitution = rigidBody.getRestitution();How Ammo.js Calculates Collision Bounciness
A common issue when working with ammo.js is setting a high restitution on a falling object (like a ball) but seeing it fail to bounce off the ground.
By default, ammo.js calculates the combined restitution of a collision by multiplying the restitution values of the two colliding bodies:
\[\text{Combined Restitution} = \text{Restitution}_A \times \text{Restitution}_B\]
Because of this multiplication, if either of the colliding objects
has a restitution of 0.0, the resulting bounce will be
0.0.
Solution for Getting Objects to Bounce
To make an object bounce off a surface, you must set a non-zero restitution value on both the moving rigid body and the ground/obstacle rigid body.
// 1. Configure the falling ball
const ballBody = new Ammo.btRigidBody(ballConstructionInfo);
ballBody.setRestitution(0.8); // 80% bounciness
physicsWorld.addRigidBody(ballBody);
// 2. Configure the ground surface
const groundBody = new Ammo.btRigidBody(groundConstructionInfo);
groundBody.setRestitution(0.6); // Ground must also have restitution > 0
physicsWorld.addRigidBody(groundBody);With these settings, the combined bounce factor during impact will be \(0.8 \times 0.6 = 0.48\), resulting in a realistic, diminishing bounce effect.