Search code examples
javascriptbox2dgame-physicsphysics-engine

In Box2D, how to set the sleepTime of an object?


I'm working on a educational physics project and I've chosen Box2D as my main engine for a "Build your own rocket" like physics game.

Box2D looks very intuitive and the documentation seems pretty good IMMO, but I couldn't find how to set the sleeping time of a given body. Is it possible?

I've already tried the lazy approach:

var newB = new b2BodyDef();
    newB.m_sleepTime = 12000;

Unfortunately, the newB still starts to sleep almost instantaneously after it gets into a rest position.


Solution

  • You can SetSleepingAllowed(bool) on an individual body, or set the entire world to not sleep when you create it. Unless you have a huge number of bodies in your simulation, I would just use one of these and keep the body awake all the time. You can also SetAwake(bool) on individual bodies if you know when you'll need them to be awake.