This project has moved and is read-only. For the latest updates, please go here.

Active Collision Detection

Topics: Developer Forum
May 7, 2012 at 10:45 PM

So I was wondering if there is a way to instantaneously check if a physics body is colliding with another. Like is there a way I can say

if (body.IsColliding()) { [insert code here] }

I know of the event handler for when a body collides with another but in order to use that I'd have to keep an active list of collisions on the body and also I dont know if there is a onStopCollision event for a physics body that fires when the body stops colliding with whatever it was before.

Anyways thanks for your time. Tell me if you need any more info.

May 8, 2012 at 7:30 PM
Edited May 8, 2012 at 7:35 PM


it's not very clear what you want to achieve. Take a step back and look at what you are asking. The engine does collision detection *and* collision response. Since we deal with rigid bodies collision response would mean separating the bodies so that they do not overlap. They can still be "touching" but they are not allowed to try to interpenetrate each other. In other words applying the law of conservation of momentum will mean that after a collision, at the end of the World.Step() when the solver is done with everything, the bodies are simply moving at some new speed and direction free from overlap and with constant acceleration (equal to gravity which is global). Technically they are not colliding anymore (first law of motion ignoring gravity) - that's the collision response for rigid bodies. Now since the solver is imperfect more than one time step might be necessary to satisfy all forces and constraints but that's another topic.

The above is why a Body.IsColliding() method would be very misleading. What is used, instead, is an event system (callbacks): mainly you deal with collisions through firing of events that occur during the solver iterations while it's trying to calculate the new speeds. Highest level events are OnCollision() and OnSeparation(). Also there are the contact methods Begin/EndContact, Pre/PostSolve for more specific needs. 

You can also get the contact list for the whole world and check if your body is in contact with anything else. That's closest to IsColliding() but actually means IsTouching() and there is even yet another method called IsTouching().

You can read more in the manual (of Box2D) and also this site:

You can also use geometric queries like TestOverlap, QueryAABB and RayCasts.

Finally you can have special "sensor" fixtures for detecting overlap.

To complicate stuff further there is Continuous Collision Detection (CCD) - a unique feature - that also takes into account the projected positions in time (TOI - time of impact).

Each of the above good for different scenarios. Without knowing more I suppose you are actually looking for overlap detection so QueryAABB is probably the method you should look into. Or subscribe to the OnCollision and OnSeparation events.