Page 1 of 1

How to detect when all bodies have stopped moving

Posted: Wed Jan 13, 2010 6:12 pm
by radamanthus
Hi all,

Forgive me if this sounds like a n00b question. I"m one.

I'm working on a billiards-like game. Player take turns in moving.

I need to know when the balls have stopped colliding and moving. Is there a callback function for this?

One workaround I'm thinking is to just assume that things have stopped one second after the last collision. The game world is small enough that this assumption is practically valid all the time. But I'm hoping there's a cleaner, built-in way.

Thanks in advance.

Re: How to detect when all bodies have stopped moving

Posted: Wed Jan 13, 2010 6:24 pm
by slembcke
There isn't really a built in way no. One simple way to do it is to simply loop over all the balls and add the magnitude of their velocities together. When it falls below a certain amount, call it good enough.

Code: Select all

cpFloat vsum = 0.0f;
for(ball in balls){
  vsum += cpvlength(ball->body->v);
}

if(vsum < 0.1f){
  nextPlayerTurn = TRUE;
}

Re: How to detect when all bodies have stopped moving

Posted: Thu Jan 14, 2010 11:29 am
by radamanthus
That worked, and it's elegant, too.

Thanks!