physijs

Physijs stops updating?


I'm trying to make a small project using Three.js & the physics plugin physijs; just a little dice roller. My approach is to use setGravity to move the dice around, modelling gravity to move the dice around. The issue I'm running into is that once the dice come to a rest, they no longer respond to gravity. Has anyone run into this before?


Solution

  • Whats happening: Ammo.js, on which Physijs is based, puts resting or very slow moving objects in a sleep state to save performance. So when you change the worlds gravity the sleeping objects dont care, because Physijs doesnt tell them gravity has changed.

    You have the ability to modify the sleeping thresholds, set activation states or just quickly activate the rigid bodys before changing gravity.

    Please note this code applys to native Ammo.js, I am not sure how to do this when using physijs but you get the idea.

    Solution 1: Loop over your Bodys and activate them, then change gravity:

    // dice is an array with your rigid bodys
    for ( var i = 0; i < dice.length; i ++ ) {
        // hey wake up
        dice[ i ].activate();
    }
    physicsWorld.setGravity( new Ammo.btVector3( 0, -9.81, 0 ) );
    

    Solution 2: Thou shall get no sleep, do this after creating your dice:

    var DISABLE_DEACTIVATION = 4;
    for ( var i = 0; i < dice.length; i ++ )
        // no sleep for you... ever
        dice[ i ].setActivationState( DISABLE_DEACTIVATION );
    }