best way of determing if a body has stopped moving?

Hi

Does anyone know the best way to determine if a body has stopped moving; ie turned blue in the model view?

I actually want to know when all bodies have stopped moving and using the body list to test all bodies. I couldn't see any method on world such as isAllBodiesStationary().

Thanks
Graham
Graham,

It sounds like you are talking about whether bodies are sleeping. If you have created your World object and passed "true" in for allowSleep, then when bodies stop moving (after some specified time), the body will fall asleep. You can easily check to see if a body is sleeping with the following code.
Code:
BodyDef bd = new BodyDef();
Body b = world.createBody(bd);
...
boolean sleep = b.isSleeping();


Alternately, if a body has stopped moving, its speed is going to be zero (more likely very small). You can pick a suitably small threshold for speed and write a simple function that compares a body's speed to that number.

Nathan
Hi

Thanks for your reply. I'll try the is-sleeping method.

I got your 2nd suggestion working using the code below, with _Linear indicating that I could also test rotational components too.

Thanks again.

Graham

=======

public boolean isAllWorldBodiesStationary_Linear()
{
// empty world
int numberBodies = m_world.getBodyCount();
if (numberBodies == 0)
return true;

// traverse body list
Body body = m_world.getBodyList();
Body bodyNext = body;
boolean hasNext = true;
boolean isAllBodiesStatiionary = true;
do
{
Vec2 bodyNextVel = bodyNext.getLinearVelocity();
float len2 = (float)bodyNextVel.lengthSquared();

if (len2 > 0.01f)
{
isAllBodiesStatiionary = false;
break;
}

bodyNext = bodyNext.getNext();
if (bodyNext == null)
{
hasNext = false;
break;
}

} while (hasNext);

if (isAllBodiesStatiionary)
{
return true;
}
return false;
}
I'm glad you got something working. I would still lean toward using the body.isSleeping() line because it's going to be considerably more efficient since the engine has already determined the criteria to put the body to sleep (which you'd be doing again, effectively).

Original thread: https://box2d.org/forum/viewtopic.php?t=5578