Collision Detection |
The namespace DigitalRune.Geometry.Collisions contains classes that compute contact information or closest features of objects.
This topic contains the following sections:
DigitalRune Geometry can compute three different types of collision queries:
Note |
---|
The term boolean query is not equal to the term boolean operation used in 3D modeling. A boolean operation is an operation that combines objects using operations like AND or OR. |
The collision detection in DigitalRune Geometry can compute collision information between collision objects (see CollisionObject). Each collision object contains a reference to a geometric object (see IGeometricObject). Besides the geometric object a collision object stores additional information for the collision detection system.
The class CollisionDetection contains methods to compute contact points for a pair of touching objects or the two closest points for a pair of separated objects.
For a pair of touching objects the collision detection computes contacts (see Contact) that represent points where objects touch each other.
A contact describes a single contact point (or closest-point pair) between two objects (called "object A" and "object B"). A contact consists of 2 points: a point on object A (see PositionALocal or PositionAWorld) and a point on object B (see PositionBLocal or PositionBWorld). The property Position is a point that lies halfway between those two points. The contact further stores the Normal, the PenetrationDepth and other information.
There are 3 basic types of contacts:
Touching Contacts: Object A and object B are touching at the surface. The penetration depth is 0. The points on object A and object B are identical.
Pentrating Contacts: Object A and object B are penetrating each other. The penetration depth is greater than 0. The contact stores the points on object A and B that have maximum penetration.
Closest points (separated objects): Object A and object B are separated. They are not in contact. This can be the result of a closest-point query. (Other contact queries do not compute closest points. Separated objects are simply ignored in normal queries.)
The contact stores closest points between object A and B. The penetration depth is negative. The absolute value of penetration depth indicates the distance between object A and object B. (Remember: The penetration depth is the inverse of the separation distance.)
All contacts for a pair of objects are gathered in a contact set (see ContactSet).
A ContactSetCollection is a collection of the contact sets of different pairs of collision objects.
Collision detection is one of the most costly part of modern computer games. A collision domain (CollisionDomain) is a way to improve the performance of collision detection. A collision domain manages a collection of collision objects. For these objects it can compute the collision information much faster by re-using old collision information.
Additionally, if the property EnableMultithreading is set, the workload is automatically distributed across multiple CPU cores to further improve performance.
Caution |
---|
The performance of closest point queries is not improved by using collision domains. |
The computation of collision information is performed with different collision algorithms (see namespace DigitalRune.Geometry.Collisions.Algorithms). The different collision algorithms handle collisions between different shape types. Some collision algorithms can handle several different shape types, some handle only a specific shape type.
The collision detection uses the class CollisionAlgorithmMatrix to define which collision algorithms should be used for which pair of shape types. Per default, all shapes can collide with all other shapes with following exceptions:
Ray casting can be done by using a geometric object that has a RayShape. Thus, rays can be used similar to other shape types.
A few things are specials for contacts between a ray and another object:
Some collision algorithms are iterative algorithms that abort when the error of the result falls under a given tolerance limit. In DigitalRune Geometry this tolerance limit is defined using CollisionDetectionEpsilon.
Some shapes like points, lines, line segments are treated as infinitely thin - which mean two points will only collide if they are exactly on the same position. If you want more tolerant checks, use thick shapes like spheres, capsule, cylinder, etc. instead.
Objects do not have an "outer margin" and do not collide if their separation distance is greater than zero. Sometimes it is helpful to detect collisions if objects are separated but the separation distance is very small. To achieve this you can scale the shapes to make them slightly bigger, so that "near collisions" are also detected. To scale an object you can use one of the following methods:
Collision algorithms have (numerical) limitations. For example:
We have tried to minimize numerical problems and to filter sub-optimal contact information where possible. To get the best results please look at the tips below. If you need more tips to make your application faster and more robust, please contact our support.
Here are some tips for choosing the right shape to get the best performance and the most accurate contact information.
Try to use reasonable sizes for all geometric objects. Avoid extreme sizes:
Here are a few tips that help to improve the performance and stability of the collision detection: