Does updating btMotionState wake up ammo.js rigid body

This article explores whether updating a btMotionState in ammo.js automatically wakes up a sleeping rigid body. It explains the relationship between motion states and body activation, and provides the necessary steps to ensure your physics bodies react correctly after being moved.

No, updating a btMotionState does not automatically wake up a sleeping rigid body in ammo.js.

In Bullet Physics (upon which ammo.js is based), a rigid body enters a “sleeping” state (deactivation) to save CPU cycles when it stops moving. When a body is sleeping, the physics engine stops simulating it and ignores changes made solely to its btMotionState. The motion state is primarily designed as a one-way bridge to synchronize the physics simulation’s transform with your graphics engine. It does not actively push updates back into the physics simulation loop unless the body is already active.

If you manually update the position or orientation of a rigid body via its motion state, the physics engine will remain unaware of this change if the body is asleep. To ensure the body wakes up and the simulation registers the new transform, you must explicitly activate the rigid body.

To properly update and wake up a sleeping rigid body, use the following steps in your code:

  1. Update the World Transform Directly: Instead of relying solely on the motion state, set the transform directly on the rigid body using rigidBody.setWorldTransform(transform).
  2. Activate the Body: Call rigidBody.activate(true) (or rigidBody.activate()) to force the physics engine to wake the body up. This moves its activation state out of the sleeping phase.
  3. Clear Forces (Optional): If you are teleporting the body, you may also want to clear its linear and angular velocities using rigidBody.setLinearVelocity and rigidBody.setAngularVelocity with zero vectors to prevent erratic movement upon waking.

By combining setWorldTransform with activate(), you guarantee that the physics engine registers the new position and immediately includes the rigid body in active collision detection and physics calculations.