Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

[solved] what does allowSleep and enableBodySleeping do?


Recommended Posts

This is related to the physics engine p2.

If a body is allowed to sleep the physic engine can put it in a state where it gets excluded from some expensive physics calculations- for example the collision detection.


To determine if a body should sleep or wake up the engine checks if the body changed significantly during a calculation step.

FALL SLEEP: If the body is awake and it's activity level, determined by its velocity, is below a given threshold it will sleep.

WAKE UP: If the body's velocity is sleeping and its velocity is above a threshold it will wake up and becoming a fully functional member of the physics community again :)


A sleeping body cannot be fully excluded from the collision calculation list, as it needs to be seen by other bodies awake (so they can collide with the sleeping body).

I guess it heavily depends of the chosen broadphase/collision algorithm when a body wakes up or is allowed to participate in a collision.


This is the code I read to explain you this behaviour as I was curious about the current sleep implementation of p2.

//from p2.Body, the physics engine in Phaser/** * @method sleepTick * @param float time The world time in seconds * @brief Called every timestep to update internal sleep timer and change sleep state if needed. */Body.prototype.sleepTick = function(time){    if(!this.allowSleep)        return;    var sleepState = this.sleepState,        speedSquared = vec2.squaredLength(this.velocity) + Math.pow(this.angularVelocity,2),        speedLimitSquared = Math.pow(this.sleepSpeedLimit,2);    if(sleepState===Body.AWAKE && speedSquared < speedLimitSquared){        this.sleepState = Body.SLEEPY; // Sleepy        this.timeLastSleepy = time;        this.emit(Body.sleepyEvent);    } else if(sleepState===Body.SLEEPY && speedSquared > speedLimitSquared){        this.wakeUp(); // Wake up    } else if(sleepState===Body.SLEEPY && (time - this.timeLastSleepy ) > this.sleepTimeLimit){        this.sleep();    }};// from Broadphase.js/** * Check whether two bodies are allowed to collide at all. * @method  canCollide * @param  {Body} bodyA * @param  {Body} bodyB * @return {Boolean} */Broadphase.canCollide = function(bodyA, bodyB){    // Cannot collide static bodies    if(bodyA.motionState == Body.STATIC && bodyB.motionState == Body.STATIC)        return false;    // Cannot collide static vs kinematic bodies    if( (bodyA.motionState == Body.KINEMATIC && bodyB.motionState == Body.STATIC) ||        (bodyA.motionState == Body.STATIC    && bodyB.motionState == Body.KINEMATIC))        return false;    // Cannot collide kinematic vs kinematic    if(bodyA.motionState == Body.KINEMATIC && bodyB.motionState == Body.KINEMATIC)        return false;    // Cannot collide both sleeping bodies    if(bodyA.sleepState == Body.SLEEPING && bodyB.sleepState == Body.SLEEPING)        return false;    return true;};




Link to comment
Share on other sites

Yes, I would do so too.


In fact it defaults to true in the very famous and mature Box2D C++ physic library. 

How many objects are involved in your physics currently ? 50 ? Or even more ? It seems that on mobile any js physics engine is breaking due to the required performance. The only solution I see is using web workers which is itself highly difficult to manage a physics engine with. But there are other problems with web workers, of course :) See that post from Stefan, the author of p2, about web workers.



Here an excellent introduction to web workers



Performance of other physics engines:

1. Look at this Box2D JS port.

It's using the very recent Box2D version 2.3 and is compiled by using googles closure compiler. It seems to be pretty fast on the desktop.

Nobody seems to know or use that port, so I do not know about the performance in comparison. (Everybvody is only talking about the outdated Box2D Web (C->Flash->JS) Port based on 2.1 alpha).

BUT try the examples on your mobile or tablet. The are running pretty slick until you break some body count threshold. Run the 'stacked boxes' example vs. 'pyramid'. Pyramid is awful slow on my iphone 5.



2. Same here with chipmunk js.

Logo smash with those many particles nearly kills my mobile- well it's not performant on my laptop either :)



3. Physics JS

Again. Many objects, bad performance.




Mobile is simply not desktop, particularly in a non-native environment like a web browser

Link to comment
Share on other sites

Yes.. 50+ objects, all animated, 4 layers parallax in x and y axis..but I guess this should not be to much compared with whats happening on logosmash.. (3fps on my nexus) BUT I just found out that there isn't any notable Performance issue in my testgame with phaser when forcing Canvas instead of webgl.. With webgl on a nexus7 2013 I have.. Let's say 20 frames.. With canvas 60..

Interesting read about web workers btw. :)

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...