Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm currently writing a small game with box2dweb and I need some direction for this: I'm throwing a Box and have to hit a specific place and trigger an event when the object that's been thrown isn't moving anymore, "fell asleep" so to say.

What's the proper way / best practice for this?

I'm currently thinking of asking the b2World whether an Object is within a specific AABB and then wait a few seconds, check if it's still there and then trigger the event. But this seems to me like the roundabout way and the object might still be moving inside of that AABB and eventually even drop out of the AABB.

share|improve this question
2  
can you not check to see if the object's velocity is 0? –  Eric B Nov 28 '12 at 18:15

1 Answer 1

up vote 4 down vote accepted

Probably the most reliable way is to modify the source of box2DWeb and have it fire an event when it puts an object to sleep (or maybe this already happens, I don't know much about box2DWeb). When you detect this event, check to see if the object is within the specific AABB you want.

It looks like the live demo found here changes the color of the object when it goes to sleep. Check out the source for that demo and see how it's being detected.

Alternatively, you can poll the object's velocity, when the velocity is all 0's, check to see that it's inside the specific AABB you want.

The latter method may be good if you want to check when the object is "close enough" to stopped. Then you can test for velocities less than a threshold value to see if it's pretty much stopped. This will give you better response times if an object is taking its time coming to a stop (as can happen with these simulations).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.