This section shows how to use Continuous Collision Detection.
This topic contains the following sections.
Discrete Collision Detection vs. Continuous Collision Detection
The normal Discrete Collision Detection checks for collisions at the current objects positions. Then the game moves the objects to new positions and in the next frame the collision detection checks for collision at the new positions.
Here is an example (from the ContinuousCollisionDetectionSample). The capsule is moving from top to bottom. The table is moving from left to right.
In the first frame of the game the objects are at their top and left positions. In the next frame of the game the objects are at their bottom and right positions. Discrete Collision Detection does not detect a collision because only the start and end positions were checked.
This problem is known as "tunneling" and happens for small and fast moving objects in a game. A game must make sure that tunneling does not happen for game critical objects, for example, for bullet that is shot at an enemy or a rocket that should hit a wall.
Continuous Collision Detection (CCD) provides methods to compute the first time of impact of objects that are in motion. With CCD the intermediate collision can be found: (Objects in red show the first time of impact.)
To compute the first time of impact the CollisionDetection class has the method CollisionDetection..::..GetTimeOfImpact(CollisionObject, Pose, CollisionObject, Pose, Single). This method takes two objects (positioned at their start poses) and the target poses as parameters. It returns the timeOfImpact, a value in the range [0,1]:
timeOfImpact = 0
The objects touch at the start poses.
timeOfImpact = 1
The objects either collide at the end poses or they do not collide at all.
0 < timeOfImpact < 1
The objects collide somewhere between start and end pose. The returned value indicates the time when the objects collide.
The last parameter of CollisionDetection..::..GetTimeOfImpact(CollisionObject, Pose, CollisionObject, Pose, Single) is the allowedPenetration. This parameter should be set to a small positive value. For example, when most game objects have an extent around 1 unit (1 m) an allowedPenetration of 0.01 (1 cm) is a good choice. If the parameter is 0, the time of impact is the exact time when the objects start to touch. But because of numerical inaccuracies or approximations in the CCD-phase the discrete collision detection may or may not detect a collision at this time. Therefore, it is recommended to allow penetrations in the CCD computation. This way, when the objects are moved to the time of impact the collision detection will find a solid contact.
DigitalRune Geometry supports two CCD modes that can be set with CollisionDetection..::..ContinuousCollisionDetectionMode:
The continuous collision detection considers only the linear movement of the objects involved. The rotational movement is ignored. This mode is faster, but less accurate.
The continuous collision detection considers the linear and the rotational movement of the objects involved. This mode is slower, but more accurate.
CCD assumes that the object rotates less than 180°. The motion of the object is defined using a start and end pose. It is not possible to correctly handle objects that make several rotations per frame. - But even for such high rotation speeds an approximate solution is usually sufficient and visual errors are not noticable.
CCD assumes that the objects rotate around their local space origin (which is the position stored in the Pose of the geometric objects). If the center of rotation is not equal to the local space origin, the computed time of impact is only an approximation.
For CompositeShapes vs. other shapes the computed CCD is only an approximation.
Similar to closest-point queries, time of impact queries are not (yet) integrated into the CollisionDomain class. CollisionDetection..::..GetTimeOfImpact(CollisionObject, Pose, CollisionObject, Pose, Single) must be called manually when needed.
Use CCD only for important game objects.
Use CCD only for objects that move with a velocity that is high (relative to the average object size).
Do not use CCD if the objects are already touching at the start positions.