How to detect when all bodies have stopped moving

Official forum for the Chipmunk2D Physics Library.
Post Reply
radamanthus
Posts: 2
Joined: Wed Jan 13, 2010 11:01 am
Contact:

How to detect when all bodies have stopped moving

Post 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.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: How to detect when all bodies have stopped moving

Post 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;
}
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
radamanthus
Posts: 2
Joined: Wed Jan 13, 2010 11:01 am
Contact:

Re: How to detect when all bodies have stopped moving

Post by radamanthus »

That worked, and it's elegant, too.

Thanks!
Post Reply

Who is online

Users browsing this forum: Heise IT-Markt [Crawler] and 8 guests