Disable Ammo.js Rigid Body Deactivation

This article explains how to permanently prevent a critical rigid body from entering a sleep or deactivated state in the ammo.js physics engine. You will learn the specific API methods, state constants, and code implementation required to keep your physics objects active indefinitely for continuous simulation accuracy.

In ammo.js (a JavaScript port of the Bullet Physics engine), rigid bodies automatically “deactivate” or go to sleep when they remain stationary for a set period. This behavior optimizes CPU performance. However, for critical game elements—such as player characters, moving platforms, or objects that must constantly trigger collisions—this sleeping state can cause unexpected physics bugs.

To disable deactivation entirely for a specific rigid body, you must change its activation state using the setActivationState method.

Step-by-Step Implementation

  1. Obtain the Rigid Body Instance: Ensure you have a reference to your Ammo.btRigidBody.
  2. Apply the Activation State: Call setActivationState on the rigid body, passing the integer value 4. This value corresponds to the DISABLE_DEACTIVATION constant in the Bullet physics engine.

Here is the direct code implementation:

// Define the Bullet Physics constant for disabling deactivation
const DISABLE_DEACTIVATION = 4;

// Assuming 'rigidBody' is your instance of Ammo.btRigidBody
rigidBody.setActivationState(DISABLE_DEACTIVATION);

Forcing Immediate Activation

If the rigid body is already asleep when you apply this setting, you should also force it to wake up immediately. You can do this by calling the activate method:

rigidBody.activate();

By combining these two lines, the ammo.js physics solver will perpetually calculate physics forces and collisions for the specified rigid body, ignoring the default idle thresholds that normally trigger sleep mode.