All Topics  
Collision detection

 
Collision Detection

   Email Print
   Bookmark   Link





 

Collision detection




 
 
In physical simulations, video games and computational geometry
Computational geometry

Computational geometry is a branch of computer science devoted to the study of algorithms which can be stated in terms of geometry. Some purely geometrical problems arise out of the study of computational geometric algorithms, and such problems are also considered to be part of computational geometry....
, collision detection involves algorithms for checking for collision
Collision

A collision is an isolated event in which two or more bodies exert relatively strong forces on each other for a relatively short time....
, i.e. intersection, of two given solids. Simulating what happens once a collision is detected is sometimes referred to as "collision response", for which see physics engine
Physics engine

A physics engine is a computer program that simulates Newtonian physics models, using variables such as mass, velocity, friction and wind resistance....
 and ragdoll physics
Ragdoll physics

In computer physics engines, ragdoll physics are a type of procedural animation that is often used as a replacement for traditional static death animations....
. Collision detection algorithms are a basic component of 3D video games. Without them, characters could go through walls and other obstacles.

hysical simulation, we wish to conduct experiments, such as playing billiards
Billiards

Cue sports are a wide variety of Game of skill generally played with a cue stick which is used to strike billiard balls, moving them around a Baize-covered billiards table bounded by rubber ....
.






Discussion
Ask a question about 'Collision detection'
Start a new discussion about 'Collision detection'
Answer questions from other users
Full Discussion Forum



Encyclopedia


In physical simulations, video games and computational geometry
Computational geometry

Computational geometry is a branch of computer science devoted to the study of algorithms which can be stated in terms of geometry. Some purely geometrical problems arise out of the study of computational geometric algorithms, and such problems are also considered to be part of computational geometry....
, collision detection involves algorithms for checking for collision
Collision

A collision is an isolated event in which two or more bodies exert relatively strong forces on each other for a relatively short time....
, i.e. intersection, of two given solids. Simulating what happens once a collision is detected is sometimes referred to as "collision response", for which see physics engine
Physics engine

A physics engine is a computer program that simulates Newtonian physics models, using variables such as mass, velocity, friction and wind resistance....
 and ragdoll physics
Ragdoll physics

In computer physics engines, ragdoll physics are a type of procedural animation that is often used as a replacement for traditional static death animations....
. Collision detection algorithms are a basic component of 3D video games. Without them, characters could go through walls and other obstacles.

Overview

Billiards Balls
In physical simulation, we wish to conduct experiments, such as playing billiards
Billiards

Cue sports are a wide variety of Game of skill generally played with a cue stick which is used to strike billiard balls, moving them around a Baize-covered billiards table bounded by rubber ....
. The physics
Physics

Physics is the natural science which examines basic concepts such as energy, force, and spacetime and all that derives from these, such as mass, charge, matter and its Motion ....
 of bouncing billiard balls are well understood, under the umbrella of rigid body motion and elastic collision
Elastic collision

An elastic collision is a collision in which the total kinetic energy of the colliding bodies after collision is equal to their total kinetic energy before collision....
s. An initial description of the situation would be given, with a very precise physical description of the billiard table and balls, as well as initial positions of all the balls. Given a certain impulsion on the cue ball (probably resulting from a player hitting the ball with his cue stick), we want to calculate the trajectories, precise motion, and eventual resting places of all the balls with a computer program
Computer program

Computer programs are Instruction for a computer. A computer requires programs to function. Moreover, a computer program does not run unless its instructions are executed by a Central processing unit; however, a program may communicate an Algorithm#Formalization of algorithms to people without running....
. A program to simulate this game would consist of several portions, one of which would be responsible for calculating the precise impacts between the billiard balls. This particular example also turns out to be numerically unstable
Numerical stability

In the mathematics subfield of numerical analysis, numerical stability is a desirable property of numerical algorithms. The precise definition of stability depends on the context, but it is related to the accuracy of the algorithm....
: a small error in any calculation will cause drastic changes in the final position of the billiard balls.

Video games have similar requirements, with some crucial differences. While physical simulation needs to simulate real-world physics as precisely as possible, video games need to simulate real-world physics in an acceptable way, in real time
Real-time computing

In computer science, real-time computing is the study of Computer hardware and computer software systems that are subject to a "real-time constraint"?i.e., operational deadlines from event to system response....
 and robustly. Compromises are allowed, so long as the resulting simulation is satisfying to the game player.

Collision detection in physical simulation

Physical simulators differ in the way they react on a collision. Some use the softness of the material to calculate a force, which will resolve the collision in the following time steps like it is in reality. Due to the low softness of some materials this is very CPU intensive. Some simulators estimate the time of collision by linear interpolation, roll back
Rollback (data management)

In database technologies, a rollback is an operation which returns the database to some previous state. Rollbacks are important for database data integrity, because they mean that the database can be restored to a clean copy even after erroneous operations are performed....
 the simulation, and calculate the collision by the more abstract methods of conservation laws.

Some iterate the linear interpolation (Newton's method
Newton's method

In numerical analysis, Newton's method is perhaps the best known method for finding successively better approximations to the zeroes of a Real number-valued function ....
) to calculate the time of collision with a much higher precision than the rest of the simulation. Collision detection utilizes time coherence to allow ever finer time steps without much increasing CPU demand, such as in air traffic control
Air traffic control

Air traffic control is a service provided by ground-based Air traffic controller who direct aircraft on the ground and in the air. The primary purpose of ATC systems worldwide is to separate aircraft to prevent collisions, to organize and expedite the flow of traffic, and to provide information and other support for pilots when able....
.

After an inelastic collision, special states of sliding and resting can occur and, for example, the Open Dynamics Engine
Open Dynamics Engine

The Open Dynamics Engine is a physics engine. Its two main components are a rigid body dynamics simulation engine and a collision detection engine....
 uses constrains to simulate them. Constrains avoid inertia and thus instability. Implementation of rest by means of a scene graph
Scene graph

A scene graph is a general data structure commonly used by vector graphics applications and modern computer games. Examples of such programs include AutoCAD, Adobe Illustrator, Acrobat 3D, OpenSceneGraph and CorelDRAW....
 avoids drift.

In other words, physical simulators usually function one of two ways, where the collision is detected a posteriori
A priori and a posteriori (philosophy)

The terms "a priori" and "a posteriori" are used in philosophy to distinguish two types of knowledge, justifications or arguments....
 (after the collision occurs) or a priori
A priori and a posteriori (philosophy)

The terms "a priori" and "a posteriori" are used in philosophy to distinguish two types of knowledge, justifications or arguments....
 (before the collision occurs). In addition to the a posteriori and a priori distinction, almost all modern collision detection algorithms are broken into a hierarchy of algorithms.

A posteriori versus a priori

In the a posteriori case, we advance the physical simulation by a small time step, then check if any objects are intersecting, or are somehow so close to each other that we deem them to be intersecting. At each simulation step, a list of all intersecting bodies is created, and the positions and trajectories of these objects are somehow "fixed" to account for the collision. We say that this method is a posteriori because we typically miss the actual instant of collision, and only catch the collision after it has actually happened.

In the a priori methods, we write a collision detection algorithm which will be able to predict very precisely the trajectories of the physical bodies. The instants of collision are calculated with high precision, and the physical bodies never actually interpenetrate. We call this a priori because we calculate the instants of collision before we update the configuration of the physical bodies.

The main benefits of the a posteriori methods are as follows. In this case, the collision detection algorithm need not be aware of the myriad physical variables; a simple list of physical bodies is fed to the algorithm, and the program returns a list of intersecting bodies. The collision detection algorithm doesn't need to understand friction, elastic collisions, or worse, nonelastic collisions and deformable bodies. In addition, the a posteriori algorithms are in effect one dimension simpler than the a priori algorithms. Indeed, an a priori algorithm must deal with the time variable, which is absent from the a posteriori problem.

On the other hand, a posteriori algorithms cause problems in the "fixing" step, where intersections (which aren't physically correct) need to be corrected.

The benefits of the a priori algorithms are increased fidelity and stability. It is difficult (but not completely impossible) to separate the physical simulation from the collision detection algorithm. However, in all but the simplest cases, the problem of determining ahead of time when two bodies will collide (given some initial data) has no closed form solution -- a numerical root finder
Root-finding algorithm

A root-finding algorithm is a numerical method, or algorithm, for finding a value x such that f = 0, for a given function f. Such an x is called a root of the function f....
 is usually involved.

Some objects are in resting contact, that is, in collision, but neither bouncing off, nor interpenetrating, such as a vase resting on a table. In all cases, resting contact requires special treatment: If two objects collide (a posteriori) or slide (a priori) and their relative motion is below a threshold, friction becomes stiction
Stiction

Stiction is an informal portmanteau of the term "static friction" , perhaps also influenced by the verb "Adhesion".Two solid objects pressing against each other will require some threshold of force parallel to the surface of contact in order to overcome static cohesion....
 and both objects are arranged in the same branch of the scene graph
Scene graph

A scene graph is a general data structure commonly used by vector graphics applications and modern computer games. Examples of such programs include AutoCAD, Adobe Illustrator, Acrobat 3D, OpenSceneGraph and CorelDRAW....
.

Optimization

The obvious approaches to collision detection for multiple objects are very slow. Checking every object against every other object will, of course, work, but is too inefficient to be used when the number of objects is at all large. Checking objects with complex geometry against each other in the obvious way, by checking each face against each other face, is itself quite slow. Thus, considerable research has been applied to speeding up the problem.

Exploiting temporal coherence

In many applications, the configuration of physical bodies from one time step to the next changes very little. Many of the objects may not move at all. Algorithms have been designed so that the calculations done in a preceding time step can be reused in the current time step, resulting in faster algorithms.

At the coarse level of collision detection, the objective is to find pairs of objects which might potentially intersect. Those pairs will require further analysis. An early high performance algorithm for this was developed by M. C. Lin at U.C. Berkley , who suggested using axis-aligned bounding boxes for all n bodies in the scene.

Each box is represented by the product of three intervals (i.e., a box would be .) A common algorithm for collision detection of bounding boxes is sweep and prune
Sweep and prune

In physical simulations, sweep and prune is a broad phase algorithm used during collision detection to limit the number of pairs of solids that need to be checked for collision, i.e....
. We observe that two such boxes, and intersect if, and only if, intersects , intersects and intersects . We suppose that, from one time step to the next, and intersect, then it is very likely that at the next time step, they will still intersect. Likewise, if they did not intersect in the previous time step, then they are very likely to continue not to.

So we reduce the problem to that of tracking, from frame to frame, which intervals do intersect. We have three lists of intervals (one for each axis) and all lists are the same length (since each list has length , the number of bounding boxes.) In each list, each interval is allowed to intersect all other intervals in the list. So for each list, we will have an matrix of zeroes and ones: is 1 if intervals and intersect, and 0 if they do not intersect.

By our assumption, the matrix associated to a list of intervals will remain essentially unchanged from one time step to the next. To exploit this, the list of intervals is actually maintained as a list of labeled endpoints. Each element of the list has the coordinate of an endpoint of an interval, as well as a unique integer identifying that interval. Then, we sort
Sorting algorithm

In computer science and mathematics, a sorting algorithm is an algorithm that puts elements of a List in a certain Total order. The most-used orders are numerical order and lexicographical order....
 the list by coordinates, and update the matrix as we go. It's not so hard to believe that this algorithm will work relatively quickly if indeed the configuration of bounding boxes does not change significantly from one time step to the next.

In the case of deformable bodies such as cloth simulation, it may not be possible to use a more specific pairwise pruning algorithm as discussed below, and an n-body pruning algorithm is the best that can be done.

If an upper bound can be placed on the velocity of the physical bodies in a scene, then pairs of objects can be pruned based on their initial distance and the size of the time step.

Pairwise pruning

Once we've selected a pair of physical bodies for further investigation, we need to check for collisions more carefully. However, in many applications, individual objects (if they are not too deformable) are described by a set of smaller primitives, mainly triangles. So now, we have two sets of triangles, and (for simplicity, we will assume that each set has the same number of triangles.)

The obvious thing to do is to check all triangles against all triangles for collisions, but this involves comparisons, which is highly inefficient. If possible, it is desirable to use a pruning algorithm to reduce the number of pairs of triangles we need to check.

The most widely used family of algorithms is known as the hierarchical bounding volumes method. As a preprocessing step, for each object (in our example, and ) we will calculate a hierarchy of bounding volumes. Then, at each time step, when we need to check for collisions between and , the hierarchical bounding volumes are used to reduce the number of pairs of triangles under consideration. For the sake of simplicity, we will give an example using bounding spheres, although it has been noted that spheres are undesirable in many cases.

If is a set of triangles, we can precalculate a bounding sphere . There are many ways of choosing , we only assume that is a sphere that completely contains and is as small as possible.

Ahead of time, we can compute and . Clearly, if these two spheres do not intersect (and that is very easy to test,) then neither do and . This is not much better than an n-body pruning algorithm, however.

If is a set of triangles, then we can split it into two halves and . We can do this to and , and we can calculate (ahead of time) the bounding spheres and . The hope here is that these bounding spheres are much smaller than and . And, if, for instance, and do not intersect, then there is no sense in checking any triangle in against any triangle in .

As a precomputation, we can take each physical body (represented by a set of triangles) and recursively decompose it into a binary tree
Binary tree

In computer science, a binary tree is a Tree in which each node has at most two child node. Typically the child nodes are called left and right....
, where each node represents a set of triangles, and its two children represent and . At each node in the tree, as a we can precompute the bounding sphere .

When the time comes for testing a pair of objects for collision, their bounding sphere tree can be used to eliminate many pairs of triangles.

Many variants of the algorithms are obtained by choosing something other than a sphere for . If one chooses axis-aligned bounding boxes, one gets AABBTrees. Oriented bounding box trees are called OBBTrees. Some trees are easier to update if the underlying object changes. Some trees can accommodate higher order primitives such as spline
Spline (mathematics)

In mathematics, a spline is a special Function defined piecewise by polynomials.In interpolation problems, spline interpolation is often preferred to polynomial interpolation because it yields similar results, even when using low degree polynomials, while avoiding Runge's phenomenon for higher degrees....
s instead of simple triangles.

Exact pairwise collision detection

Once we're done pruning, we are left with a number of candidate pairs to check for exact collision detection.

A basic observation is that for any two convex
Convex set

In Euclidean space, an object is convex if for every pair of points within the object, every point on the straight line segment that joins them is also within the object....
 objects which are disjoint, one can find a plane in space so that one object lies completely on one side of that plane, and the other object lies on the opposite side of that plane. This allows the development of very fast collision detection algorithms for convex objects.

Early work in this area involved "separating plane
Separating axis theorem

For objects lying in a plane , the separating axis theorem states that, given two convex set shapes, there exists a line onto which their projections will be separate if and only if they are not intersecting....
" methods. Two triangles collide essentially only when they can not be separated by a plane going through three vertices. That is, if the triangles are and where each is a vector in , then we can take three vertices, , find a plane going through all three vertices, and check to see if this is a separating plane. If any such plane is a separating plane, then the triangles are deemed to be disjoint. On the other hand, if none of these planes are separating planes, then the triangles are deemed to intersect. There are twenty such planes.

If the triangles are coplanar, this test is not entirely successful. One can either add some extra planes, for instance, planes that are normal to triangle edges, to fix the problem entirely. In other cases, objects that meet at a flat face must necessarily also meet at an angle elsewhere, hence the overall collision detection will be able to find the collision.

Better methods have since been developed. Very fast algorithms are available for finding the closest points on the surface of two convex polyhedral objects. Early work by M. C. Lin used a variation on the simplex algorithm
Simplex algorithm

In mathematical optimization , the simplex algorithm, created by the United States mathematician George Dantzig in 1947, is a popular algorithm for numerical analysis solution of the linear programming problem....
 from linear programming
Linear programming

In mathematics, linear programming is a technique for optimization of a linear objective function, subject to linear equality and linear inequality Constraint ....
. The Gilbert-Johnson-Keerthi distance algorithm
Gilbert-Johnson-Keerthi distance algorithm

The Gilbert?Johnson?Keerthi distance algorithm is a method of determining the minimum distance between two convex sets. Unlike many other distance algorithms, it does not require that the geometry data be stored in any specific format, but instead relies solely on a support to iteratively generate closer simplexes to the correct answer using...
 has superseded that approach. These algorithms approach constant time when applied repeatedly to pairs of stationary or slow-moving objects, when used with starting points from the previous collision check.

The end result of all this algorithmic work is that collision detection can be done efficiently for thousands of moving objects in real time on typical personal computers and game consoles.

A priori pruning

Where most of the objects involved are fixed, as is typical of video games, a priori methods using precomputation can be used to speed up execution.

Pruning is also desirable here, both n-body pruning and pairwise pruning, but the algorithms must take time and the types of motions used in the underlying physical system into consideration.

When it comes to the exact pairwise collision detection, this is highly trajectory dependent, and one almost has to use a numerical root-finding algorithm
Root-finding algorithm

A root-finding algorithm is a numerical method, or algorithm, for finding a value x such that f = 0, for a given function f. Such an x is called a root of the function f....
 to compute the instant of impact.

As an example, consider two triangles moving in time and . At any point in time, the two triangles can be checked for intersection using the twenty planes previously mentioned. However, we can do better, since these twenty planes can all be tracked in time. If is the plane going through points in then there are twenty planes to track. Each plane needs to be tracked against three vertices, this gives sixty values to track. Using a root finder on these sixty functions produces the exact collision times for the two given triangles and the two given trajectory. We note here that if the trajectories of the vertices are assumed to be linear polynomials in then the final sixty functions are in fact cubic polynomials, and in this exceptional case, it is possible to locate the exact collision time using the formula for the roots of the cubic. Some numerical analysts suggest that using the formula for the roots of the cubic is not as numerically stable as using a root finder for polynomials.

Spatial partitioning

Alternative algorithms are grouped under the spatial partitioning umbrella, which includes octree
Octree

An octree is a tree data structure in which each internal node has up to eight children. Octrees are most often used to partition a three dimensional space by recursively subdividing it into eight octants....
s, binary space partitioning
Binary space partitioning

Binary space partitioning is a method for recursively subdividing a Euclidean space into convex sets by hyperplanes. This subdivision gives rise to a representation of the scene by means of a Tree known as a BSP tree....
 (or BSP trees) and other, similar approaches. If one splits space into a number of simple cells, and if two objects can be shown not to be in the same cell, then they need not be checked for intersection. Since BSP trees can be precomputed, that approach is well suited to handling walls and fixed obstacles in games. These algorithms are generally older than the algorithms described above.

Video games

Video games have to split their very limited computing time between several tasks. Despite this resource limit, and the use of relatively primitive collision detection algorithms, programmers have been able to create believeable, if inexact, systems for use in games.

For a long time, video games had a very limited number of objects to treat, and so checking all pairs was not a problem. In two-dimensional games, in some cases, the hardware was able to efficiently detect and report overlapping pixels between sprite
Sprite (computer graphics)

In computer graphics, a sprite is a two-dimensional/three-dimensional or animation that is integrated into a larger scene.Sprites were originally invented as a method of quickly compositing several images together in two-dimensional video games using special hardware....
s on the screen. In other cases, simply tiling the screen and binding each sprite into the tiles it overlaps provides sufficient pruning, and for pairwise checks, bounding rectangles or circles are used and deemed sufficiently accurate.

Three dimensional games have used spatial partitioning methods for -body pruning, and for a long time used one or a few spheres per actual 3D object for pairwise checks. Exact checks are very rare, except in games attempting to simulate reality closely. Even then, exact checks are not necessarily used in all cases.

Because games use simplified physics, stability is not as much of an issue. Almost all games use a posteriori collision detection, and collisions are often resolved using very simple rules. For instance, if a character becomes embedded in a wall, he might be simply moved back to his last known good location. Some games will calculate the distance the character can move before getting embedded into a wall, and only allow him to move that far.

A slightly more sophisticated and striking effect is ragdoll physics
Ragdoll physics

In computer physics engines, ragdoll physics are a type of procedural animation that is often used as a replacement for traditional static death animations....
. If a video game character is disabled, instead of playing a preset animation, a simplified skeleton of the character is animated as if it were a rag doll. This rag doll falls limp, and might collide with itself and the environment, in which case it should behave appropriately.

In many cases for video games, approximating the characters by a point is sufficient for the purpose of collision detection with the environment. In this case, binary space partition trees provide a viable, efficient and simple algorithm for checking if a point is embedded in the scenery or not. Such a data structure can also be used to handle "resting position" situation gracefully when a character is running along the ground. Collisions between characters, and collisions with projectiles and hazards, are treated separately.

A robust simulator is one that will react to any input in a reasonable way. For instance, if we imagine a high speed racecar video game
Racing game

A racing game is a genre of Video games. Racing games are either in the first or third person perspective. They may be based on anything from real-world racing leagues to entirely fantastical settings, and feature any type of land, air, or sea vehicles....
, from one simulation step to the next, it is conceivable that the cars would advance a substantial distance along the race track. If there is a shallow obstacle on the track (such as a brick wall), it is not entirely unlikely that the car will completely leap over it, and this is very undesirable. In other instances, the "fixing" that the a posteriori algorithms require isn't implemented correctly, and characters find themselves embedded in walls, or falling off into a deep black void. These are the hallmarks of a mediocre collision detection and physical simulation system.

Open Source Collision Detection

  • A 2D implementation of the Gilbert-Johnson-Keerthi (GJK) algorithm, written in D.
  • A 2D implementation of the Minkowski Portal Refinement (MPR) Algorithm, written in D.


See also

  • Bounding volume
    Bounding volume

    In computer graphics and computational geometry, a bounding volume for a set of objects is a closed volume that completely contains the union of the objects in the set....
  • Game physics
    Game physics

    Computer animation physics or game physics involves the introduction of the laws of physics into a simulation or game engine, particularly in 3D computer graphics, for the purpose of making the effects appear more real to the observer....
  • Gilbert–Johnson–Keerthi distance algorithm
  • Physics engine
    Physics engine

    A physics engine is a computer program that simulates Newtonian physics models, using variables such as mass, velocity, friction and wind resistance....
  • Ragdoll physics
    Ragdoll physics

    In computer physics engines, ragdoll physics are a type of procedural animation that is often used as a replacement for traditional static death animations....


External links

  • by George Beck, Wolfram Demonstrations Project
    Wolfram Demonstrations Project

    The Wolfram Demonstrations Project is a website developed by Wolfram Research, whose stated goal is to bring computational exploration to the widest possible audience....
    .
  • .