Continuous Collision Detection
From Box2D Wiki
Most physics engines perform retroactive collision detection, meaning that objects are simulated forwards in time and if overlaps are detected, they are resolved. This is easy to do and can be implemented in a fast way.
However, especially when high velocities are involved, retroactive collision detection can be inaccurate. Bodies may pass through walls, an unpleasant situation known as tunneling. As of version 2.0, Box2D is capable of performing continuous collision detection (CCD) using conservative advancement to avoid these problems.
How to use CCD
In Box2D CCD is always active when checking dynamic bodies against static bodies, so you don't need to do anything to make sure your dynamic bodies don't tunnel through walls or other static scenery. In many cases, this is all that is important.
You may enable it for dynamic vs. dynamic bodies by setting the "isBullet" property in the b2BodyDef you use to create the body. Use this sparingly, as the algorithm is more expensive than the usual tests. A pair of bodies will be solved using continuous collision detection if either body is marked as a bullet, so only bodies likely to be moving fast need to be market as bullets.
//The following code creates a circle with isBullit on. Taken from the testbed. // This will ensure that the circle won't pass trough any other dynamic bodies without hitting it. b2BodyDef bd; bd.position.Set(0.0f, 0.0f); bd.isBullet = true; b2Body* body = m_world->CreateBody(&bd); body->SetAngularVelocity(b2Random(-50.0f, 50.0f)); b2CircleDef sd; sd.radius = 0.25f; sd.density = 1.0f; sd.restitution = 0.0f; body->CreateShape(&sd); body->SetMassFromShapes();
How CCD works: Conservative Advancement
| Internal Information
The following text is interesting in case you want to know how Box2D behaves internally, it is not required to use the API. |
The conservative advancement (CA) algorithm tries to simulate motion right up to the point of contact rather than resolving it after the fact. It does this by calculating a conservative estimate of how long a body may be simulated without hitting another body. Once this estimate is calculated for all bodies (at least for all potentially interacting bodies in an island computed from swept AABB overlaps), it is possible to safely move the bodies forward by the minimum safe time and repeat. In this way the simulator will approach the first collision, at which point some other method must step in and resolve the collision before moving on. Typically CA must simulate many steps over the course of a single real-world time step, so it is quite a bit more expensive than retroactive collision detection and should only be used when necessary.
Box2D uses the concept of core shapes to implement CA. Core shapes are slightly shrunken versions of the actual geometry, and these are used in the CA calculations, whereas the original geometry is used for collision resolution. This helps speed resolution, as it takes fewer CA sub-steps to reach the point of contact (which is computed and resolved using the original, full shapes). This is also convenient because the algorithms used for normal collision resolution, which require a bit of overlap, need not be rewritten to take advantage of CCD.
To calculate times of impact (TOIs), Box2d uses a very conservative estimate of approach velocity obtained from a combination of the linear velocities of the objects and the maximum local velocities due to rotation. Maximum approach velocity is compared to the closest distance between the objects, and a minimum TOI is calculated.
