Intersections And Collision Detection

Intersections

Finding intersections of lines and more specifically line segments can be part of a collision detection scheme. Imagine a collision between one very fast moving object as a bullet and another object say a target. In the span of one cycle of the game loop, the bullet may have moved from being on one side of the target to the other. If we do not check for the intersection of their paths from one time step to the next it may be possible for the bullet to pass completely through the target without detection. This would be bad.

Intersection of Two Lines in Standard Form

The intersection of two lines is the point that lies on the graph of both lines. Algebraically, it is the ordered pair $(x,y)$ that is a solution to both equations. So let us start with the equations of two arbitrary lines in standard form.

(1)
\begin{eqnarray} Ax+By=C\\ Dx+Ey=F\\ \end{eqnarray}

There are several techniques from Algebra for solving such a system. We will use elimination. First we will eliminate the variable x, by multiplying the first equation by D and the second equation by -A. That gives us the following

(2)
\begin{eqnarray} ADx+BDy=CD\\ -ADx-AEy=-AF\\ \end{eqnarray}

Now add the two equations together,

(3)
\begin{equation} (BD-AE)y=(CD-AF) \end{equation}

And finally, dividing we get,

(4)
\begin{align} y=\frac{CD-AF}{BD-AE} \end{align}

Some of you may recognize this as the result of Cramer's Rule. Using the same process to eliminate y instead of x, we get

(5)
\begin{align} x=\frac{BF-CE}{BD-AE} \end{align}

Notice that we have the possibility of dividing by zero, in the case where $BD-AE=0$. This indicates that the lines are parallel and distinct and therefore do not intersect or that the two equations are for the same line. To determine which of these two cases has occured, one needs to examine the numerators of the two fractions. The details of which will be left for an exercise.

Intersection of Two Line Segments

If we do find that the lines intersect, we are not finished. The path of each object has a beginning and ending point at each step of the game loop, so we need to model this using line segments not lines. So once we have our point of intersection, we need to determine if it occurs within the bounds of our line segment.

Example
Determine if the line segment from (-1,1) to (3,3) intersects the line segment from (1,-2) to (2,5).

Intersections of Circles and Spheres

For the purposes of collision detection, it is computationally efficient to think of objects as being enveloped by a circle in the 2D case or by a sphere in the 3D case. Then collision detection becomes a matter of detecting when two circles or spheres have intersected. We will define both of these objects algebraically and then look at how to determine if two circles or spheres have intersected one another.

The image below shows two circles that are not overlapping. In terms of collision detection. These two objects have not collided.

CircleIntersection.jpg

Notice that the sum of the two radii is less than the distance between the two centers. If we call the distance between centers d, we have the conditions,

(6)
\begin{eqnarray} r_1+r_2<d& \mbox{ No Collision}\\ r_1+r_2 \geq d& \mbox{ Collision} \end{eqnarray}
Example
Determine if the circle with center (2.-3) and radius 5 intersects with the circle with center (4,5) and radius 2.

Intersection of Axially Aligned Bounding Boxes

An axially aligned bounding box is a rectangle in 2D that has its sides parallel to the coordinate axes or a rectangular solid in 3D that has its sides parallel to the major planes of the coordinate system. The images below illustrate one of each.
AABB2D.jpg AABB3D.jpg
In collision detection, the idea is to enclose an object with an AABB and then detect when the boxes overlap. Often an AABB can give a tighter envelope around the object then a circle or sphere, but an AABB detection algorithm is not as fast.
In order for two AABB's to overlap, the interval of x-values, y-values and in the 3D case the z-values for the two AABB's must all overlap. For the detection of overlap for intervals, the interval can be thought of as a 1D analog of a circle. Every interval has a center and a radius and two intervals overlap if $r_1+r_2 \geq d$.
Example
Determine if the AABB with extreme points (-1,-3) and (3,5) intersects the AABB with extreme points (2,-4) and (8,10).

Exercises

  1. Do $(x-4)^2+(y-3)^2=17$ and $(x+2)^2+(y-5)^2=33$ overlap?
  2. Do $(x+2)^2+(y+5)^2+(z-1)^2=5$ and $(x-9)^2+y^2+(z+3)^2=7$overlap?
  3. Find the point of intersection, if there is one, of the lines $2x+3y=8$ and $x-y=-1$.
  4. Find the point of intersection, if there is one, of the lines $x+4y=6$ and $3x+12y=18$.
  5. Find the point of intersection, if there is one, of the lines $2x-5y=1$ and $-4x+10y=-6$.
  6. Does the line segment with endpoints (-2,-8) and (-4,0) intersect the line segment with endpoints (2,3) and (-3,-3)?
  7. Does the line segment with endpoints (3,4) and (6,1) intersect the line segment with endpoints (1,-2) and (6,4)?
  8. One AABB has extreme points (2, 5, 7) and (4, 9, 8). A second AABB has extreme points (–1, 5, 4) and (3, 6, 8). Do the boxes overlap?
  9. One AABB has extreme points (-3, 1) and (5,-2). A second AABB has extreme points (8, 3) and (4,5). Do the boxes overlap?
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License