Collision Detection
Collision Detection
What we've described earlier refers to the body movement without constraints and obstacles, but this rarely happens in real life, and therefore, in virtual life, too. To correctly describe the situation when the body meets an obstacle, collision detection is used.
All collision detection algorithms, regardless of their implementation, usually start with creating simple shapes that approximate the colliding objects. Such approximations are called colliders, their vertices are used during collision detection calculation, not the vertices of the original objects. There are many types of colliders, which can be combined, for example, boxes, spheres, cylinders, capped cylinders, convex hulls, and meshes (often simplified).
Collider vertices are tested, if they are in contact with some plane or even behind it. There are two types of contact: static, when the body rests on another body, and dynamic, when two bodies collide. In the first case, one calculates force that prevents the upper body from moving downwards. In the second case, one needs to calculate instantaneous change in velocity for both colliding objects.
There are two main approaches to collision detection calculation, which, in essence, do the same work, and both are iterative in their nature:
- Iterative LCP constraint solving (all contact points are solved simultaneously).
Though often this problem is solved iteratively, it is not always so. Non-iterative methods also exist, and they are frequently more accurate, but slower.
- Sequential impulse-based calculations (one contact point is solved after another, updating the current velocity of the bodies).
Another, completely different, approach is continuous collision detection, when the body that is moving is extruded along its way (between two frames), and if something gets into this volume, a collision is detected, and the body is taken back in time to correctly react to the collision.
No matter what approach is used to detect collisions, factors like friction and restitution, etc. are taken into account. Also, physics engines often "freeze" the body, if it does not move or move a little during some period of time. In this case all physics calculations are disabled for the body, except collision detection. The body remains "frozen" until a collision occurs.