Configure Spinning Friction in Ammo.js
Simulating realistic spinning objects like tops in ammo.js requires a
specific combination of physical properties to prevent them from
spinning infinitely or sliding unrealistically. This article
demonstrates how to configure spinning friction, rolling friction, and
angular damping on an Ammo.btRigidBody to achieve natural
spinning behavior in your physics simulation.
To make a rigid body behave like a spinning top, standard lateral friction is not enough. Ammo.js (which is a port of the Bullet physics engine) provides dedicated methods to handle torque resistance at contact points and general rotational air resistance.
Key Physics Properties for Spinning Objects
- Spinning Friction
(
setSpinningFriction): This is the most critical parameter for a top. It simulates torsional friction at the point of contact, opposing the spinning motion around the contact normal. Without this, a top spinning on its tip would spin indefinitely when touching a surface. - Rolling Friction (
setRollingFriction): This resists the rolling motion of the object when it tilts. It prevents the top from endlessly rolling across the floor like a marble when it begins to lose balance. - Angular Damping (
setDamping): This simulates air resistance against rotation. It gradually slows down the spin even when the object is in mid-air.
Implementation Code
Apply these properties directly to your btRigidBody
after construction:
// Create your rigid body construction info and body as usual
const rbInfo = new Ammo.btRigidBodyConstructionInfo(mass, motionState, shape, localInertia);
const body = new Ammo.btRigidBody(rbInfo);
// 1. Set standard sliding friction (prevents the top from sliding like ice)
body.setFriction(0.5);
// 2. Set rolling friction (prevents perpetual rolling when tilted)
body.setRollingFriction(0.1);
// 3. Set spinning friction (resists the axial spin on contact)
body.setSpinningFriction(0.05);
// 4. Set damping (linear damping, angular damping)
// The second parameter (0.05) acts as air resistance for the spin
body.setDamping(0.01, 0.05);
// Add the body to your physics world
physicsWorld.addRigidBody(body);Fine-Tuning the Behavior
- If the top stops spinning too quickly: Decrease the
value of
setSpinningFriction(e.g., to0.01) or lower the angular damping insetDamping. - If the top spins forever on the floor: Increase the
setSpinningFrictionvalue. - If the top skates across the ground too much:
Increase the standard friction using
setFriction. - If the top wobbles and rolls away instead of staying in
place: Increase the
setRollingFrictionvalue.
By balancing these four parameters, you can accurately simulate different materials, ranging from a highly polished metal top spinning on glass to a heavy wooden top spinning on a rough table.