Detect when all entities at sleep

  1. 7 months ago

    Hello all,

    I am starting out using Polycode, and I am trying to expand upon the "2DPhysics_Basic" example to learn more about the framework. Specifically, I want to determine when all entities added to the PhysicsScene2D are asleep. In the example program, this would be when all of the blocks have come to rest. The end goal of all this is to develop a genetic algorithm that evolves the tallest "tower" out of semi-randomly placed blocks, so I need to check the position of the highest block when the simulation has come to a rest.

    Is there a flag set in the physics engine for when the entire scene has come to rest? Or would I need to loop through each block to check their sleep status? If so, what function would I use? In the documentation, I saw a function to wake up an entity but not one to check whether or not it was currently asleep.

    Thank you

  2. fodinabor

    17 Oct 2016 Administrator

    yeah it looks like you have to go to each block and check their sleep status by looking at:
    scene:getVelocity(block) if that's not 0 then that block still moves..

  3. Thanks for the reply! I kept fiddling with this and I found another method that works as well.

    You can access the Box2D class "b2Body" of the entity with the following:

    //Reference to sceneEntity given when adding as physics child to scene.  
    PhysicsScene2DEntity * sceneEntity = scene->addPhysicsChild(shape, PhysicsScene2DEntity::ENTITY_RECT, false);
    b2Body * body = sceneEntity->getFixture()->GetBody();

    Then later on, check the awake status of the body with the following:


    This solution seems a bit messy, though it does work. Luckily, the scene won't contain more than 100 blocks at a time, so it won't take long to check the status of each block on each render loop.

  4. fodinabor

    18 Oct 2016 Administrator

    well it's definitely a cleaner solution than mine :)


or Sign Up to reply!