Search code examples
ioscocos2d-iphonespritechipmunk

How to tell when a sprite has stopped moving


In chipmunk/cocos2d, I have the classic bouncing ball demo happily running with an arbitrary number of balls. How can I tell when a sprite has stopped moving (i.e. the ball has settled to the bottom and is "done")?

I tried declaring float prevX, prevY in the Layer class, so I could compare current x,y to previous x,y in updateShape, but I can't access the sprite's parent.

         ((CustomLayer *)(sprite->parent))->preX = body->p.x;

The compiler informs me that the sprite's parent is protected.

Thanks.


Solution

  • If you have Chipmunk's sleeping algorithm enabled, you can just call cpBodyIsSleeping().

    Chipmunk figures out when things are idle by checking that their kinetic energy is lower than a certain threshold and incrementing a timer. If the timer reaches a certain amount, then it considers the body to be idle.