TExcelTAD
Home › Pre-Calculus, Geometry & Trigonometry › Collision Geometry

Collision Geometry

Games, drawing apps and physics engines ask the same few geometric questions thousands of times per frame: do these two circles touch, do these boxes overlap, do these two line segments cross, is this point inside that triangle? Each one has a short, exact answer built from distances and cross products. This lesson derives the four classic tests and writes them in Python.

Circle vs circle

Two circles touch or overlap when the distance between their centres is at most the sum of their radii:

∣c1−c2∣≤r1+r2\lvert c_1 - c_2 \rvert \le r_1 + r_2

Square both sides to skip the square root, which is slower and not needed for a yes/no answer (distance formula):

(x1−x2)2+(y1−y2)2≤(r1+r2)2(x_1 - x_2)^2 + (y_1 - y_2)^2 \le (r_1 + r_2)^2

When they do overlap, the penetration depth (r1+r2)−∣c1−c2∣(r_1 + r_2) - \lvert c_1 - c_2 \rvert tells a physics engine how far to push them apart; for that one number you do need the square root.

Box vs box (AABB)

An axis-aligned bounding box (AABB) is a rectangle whose sides are parallel to the axes, stored as its minimum and maximum corners. Two AABBs overlap exactly when their ranges overlap on both axes:

text
a.min_x ≤ b.max_x  and  b.min_x ≤ a.max_x      (x ranges overlap)
a.min_y ≤ b.max_y  and  b.min_y ≤ a.max_y      (y ranges overlap)

If either axis has a gap, there is a line between the boxes that separates them. That idea, generalised to rotated shapes, is the separating axis theorem (SAT). AABBs are so cheap that engines wrap every object in one and run this test first (AABB).

python · try it yourself
import math

def circles_overlap(c1, r1, c2, r2):
    dx, dy = c1[0] - c2[0], c1[1] - c2[1]
    return dx * dx + dy * dy <= (r1 + r2) ** 2

def depth(c1, r1, c2, r2):
    return r1 + r2 - math.dist(c1, c2)

def aabb_overlap(a, b):          # box = (min_x, min_y, max_x, max_y)
    return a[0] <= b[2] and b[0] <= a[2] and a[1] <= b[3] and b[1] <= a[3]

print(circles_overlap((0, 0), 2, (3, 1), 1.5), round(depth((0, 0), 2, (3, 1), 1.5), 4))
print(circles_overlap((0, 0), 2, (4, 3), 3), depth((0, 0), 2, (4, 3), 3))
print(circles_overlap((0, 0), 1, (5, 0), 2))

player = (1, 1, 3, 4)
for name, box in [("crate", (2, 3, 5, 6)), ("wall", (3, 0, 4, 10)), ("coin", (4, 1, 5, 2))]:
    print(f"player vs {name}: {aabb_overlap(player, box)}")
True 0.3377
True 0.0
False
player vs crate: True
player vs wall: True
player vs coin: False

The second pair touches exactly (distance 5=2+35 = 2 + 3), so the depth is 0. The wall shares the edge x=3x = 3 with the player; with <= touching counts as overlapping, with < it would not. Pick one rule and use it everywhere.

The orientation test

Most of the remaining tests rest on one formula: the 2D cross product of the vectors from aa to bb and from aa to cc (vectors in geometry):

orient⁡(a,b,c)=(bx−ax)(cy−ay)−(by−ay)(cx−ax)\operatorname{orient}(a, b, c) = (b_x - a_x)(c_y - a_y) - (b_y - a_y)(c_x - a_x)
Sign Meaning
>0> 0 a→b→ca \to b \to c turns left (counter-clockwise)
<0< 0 turns right (clockwise)
=0= 0 the three points are collinear

Its absolute value is twice the area of triangle abcabc. With integer coordinates it is computed exactly, with no rounding at all.

Segment vs segment

Segments PQPQ and RSRS cross when RR and SS lie on opposite sides of line PQPQ and PP and QQ lie on opposite sides of line RSRS. Opposite sides means the two orientations have opposite signs. If an orientation is 0, the point is on the other segment's line, and we check whether it lies within the segment's bounding box.

To find where they cross, write P+t⋅(Q−P)=R+u⋅(S−R)P + t \cdot (Q - P) = R + u \cdot (S - R) and solve with cross products: t=cross⁡(R−P,S−R)/cross⁡(Q−P,S−R)t = \operatorname{cross}(R - P, S - R) / \operatorname{cross}(Q - P, S - R).

Point in triangle

A point lies inside triangle ABCABC (counter-clockwise) when it is on the left of all three edges: orient⁡(A,B,P)\operatorname{orient}(A, B, P), orient⁡(B,C,P)\operatorname{orient}(B, C, P) and orient⁡(C,A,P)\operatorname{orient}(C, A, P) are all ≥0\ge 0. Dividing each by the whole triangle's orient⁡(A,B,C)\operatorname{orient}(A, B, C) gives the barycentric coordinates (u,v,w)(u, v, w) of PP: weights that add up to 1, with P=u⋅A+v⋅B+w⋅CP = u \cdot A + v \cdot B + w \cdot C. All three are non-negative exactly when PP is inside. GPUs use these weights to decide which pixels a triangle covers and to blend colours and texture coordinates across it.

python · try it yourself
def orient(a, b, c):
    return (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0])

def on_box(a, b, p):             # p collinear with ab: is it between a and b?
    return min(a[0], b[0]) <= p[0] <= max(a[0], b[0]) and min(a[1], b[1]) <= p[1] <= max(a[1], b[1])

def segments_cross(p, q, r, s):
    o1, o2, o3, o4 = orient(p, q, r), orient(p, q, s), orient(r, s, p), orient(r, s, q)
    if o1 * o2 < 0 and o3 * o4 < 0:
        return True
    return (o1 == 0 and on_box(p, q, r)) or (o2 == 0 and on_box(p, q, s)) or \
           (o3 == 0 and on_box(r, s, p)) or (o4 == 0 and on_box(r, s, q))

def crossing_point(p, q, r, s):
    d1, d2 = (q[0] - p[0], q[1] - p[1]), (s[0] - r[0], s[1] - r[1])
    t = orient((0, 0), (r[0] - p[0], r[1] - p[1]), d2) / orient((0, 0), d1, d2)
    return (p[0] + t * d1[0], p[1] + t * d1[1])

print(segments_cross((0, 0), (4, 4), (0, 4), (4, 0)), crossing_point((0, 0), (4, 4), (0, 4), (4, 0)))
print(segments_cross((0, 0), (2, 2), (3, 0), (5, 3)))
print(segments_cross((0, 0), (4, 0), (2, 0), (6, 0)), segments_cross((0, 0), (1, 0), (2, 0), (3, 0)))

def barycentric(a, b, c, p):
    area = orient(a, b, c)
    return orient(b, c, p) / area, orient(c, a, p) / area, orient(a, b, p) / area

A, B, C = (0, 0), (6, 0), (2, 5)
for P in [(2, 2), (5, 3), (3, 0), A]:
    u, v, w = barycentric(A, B, C, P)
    print(P, f"u={u:.3f} v={v:.3f} w={w:.3f}", "inside" if min(u, v, w) >= 0 else "outside")
True (2.0, 2.0)
False
True False
(2, 2) u=0.400 v=0.200 w=0.400 inside
(5, 3) u=-0.233 v=0.633 w=0.600 outside
(3, 0) u=0.500 v=0.500 w=0.000 inside
(0, 0) u=1.000 v=0.000 w=0.000 inside

The point (3,0)(3, 0) lies on edge ABAB, so w=0w = 0: it counts as inside with >= 0. Renderers use a "top-left rule" to decide which of two triangles sharing an edge owns such pixels, so none is drawn twice.

Tip: Keep coordinates as integers (pixels, grid cells) where you can. Orientation tests are then exact. With floats, nearly collinear points can give the wrong sign; robust libraries use exact arithmetic for that case.

In software

  • Broad phase, then narrow phase: engines first compare AABBs or circles (cheap), then run exact shape tests only on the pairs that survive (closest point, circles and spheres).
  • Rasterisation: every triangle on your screen was filled with the edge test above.
  • Maps and CAD: "does this road cross that border?" is segment intersection; the convex hull uses the same orientation test.

Exercises

Exercise 1: Two circles

Circle 1 has centre (1,2)(1, 2) and radius 3; circle 2 has centre (7,10)(7, 10) and radius 4. Do they overlap?

Answer

No: the centres are 10 apart and the radii add up to 7.

Solution
dx=6,dy=8dx2+dy2=36+64=100(r1+r2)2=72=49100>49  ⟹  no overlap (gap=10−7=3)\begin{aligned} &dx = 6, \quad dy = 8 \\ &dx^2 + dy^2 = 36 + 64 = 100 \\ &(r_1 + r_2)^2 = 7^2 = 49 \\ &100 > 49 \;\Longrightarrow\; \text{no overlap (gap} = 10 - 7 = 3\text{)} \end{aligned}

Exercise 2: Which side?

Is the point P=(4,1)P = (4, 1) to the left or right of the directed line from A=(1,1)A = (1, 1) to B=(3,5)B = (3, 5)?

Answer

Right (the orientation is −12-12).

Solution
orient⁡(A,B,P)=(3−1)(1−1)−(5−1)(4−1)=2⋅0−4⋅3=−12\operatorname{orient}(A, B, P) = (3 - 1)(1 - 1) - (5 - 1)(4 - 1) = 2 \cdot 0 - 4 \cdot 3 = -12

The orientation is negative, so A→B→PA \to B \to P turns clockwise: PP is to the right of the line.

python · try it yourself
def orient(a, b, c):
    return (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0])
print(orient((1, 1), (3, 5), (4, 1)))
-12

Exercise 3: Barycentric weights

Find the barycentric coordinates of P=(2,1)P = (2, 1) in the triangle A=(0,0)A = (0, 0), B=(4,0)B = (4, 0), C=(0,4)C = (0, 4). Is PP inside?

Answer

(u,v,w)=(14,12,14)(u, v, w) = (\frac{1}{4}, \frac{1}{2}, \frac{1}{4}); inside.

Solution
orient⁡(A,B,C)=4⋅4−0⋅0=16u=orient⁡(B,C,P)/16=((0−4)(1−0)−(4−0)(2−4))/16=(−4+8)/16=1/4v=orient⁡(C,A,P)/16=((0−0)(1−4)−(0−4)(2−0))/16=8/16=1/2w=orient⁡(A,B,P)/16=(4⋅1−0⋅2)/16=4/16=1/4\begin{aligned} \operatorname{orient}(A, B, C) &= 4 \cdot 4 - 0 \cdot 0 = 16 \\ u &= \operatorname{orient}(B, C, P)/16 = ((0 - 4)(1 - 0) - (4 - 0)(2 - 4))/16 = (-4 + 8)/16 = 1/4 \\ v &= \operatorname{orient}(C, A, P)/16 = ((0 - 0)(1 - 4) - (0 - 4)(2 - 0))/16 = 8/16 = 1/2 \\ w &= \operatorname{orient}(A, B, P)/16 = (4 \cdot 1 - 0 \cdot 2)/16 = 4/16 = 1/4 \end{aligned}

Check: u+v+w=1u + v + w = 1, and u⋅A+v⋅B+w⋅C=(0,0)+(2,0)+(0,1)=(2,1)u \cdot A + v \cdot B + w \cdot C = (0, 0) + (2, 0) + (0, 1) = (2, 1). ✓ All weights are positive, so PP is inside.

Exercise 4: Box sweep (code)

A player box (0, 0, 2, 2) moves right by 1 unit per step. Write a loop that prints the first step at which it overlaps the box (6.5, 1, 8, 3).

Answer

Step 5 (the player's max xx reaches 7≥6.57 \ge 6.5).

Solution

After step kk the player spans xx from kk to k+2k + 2. It overlaps when k+2≥6.5k + 2 \ge 6.5, so k≥4.5k \ge 4.5, the first whole step is 5. The yy ranges [0,2][0, 2] and [1,3][1, 3] overlap all the time.

python · try it yourself
def aabb_overlap(a, b):
    return a[0] <= b[2] and b[0] <= a[2] and a[1] <= b[3] and b[1] <= a[3]
target = (6.5, 1, 8, 3)
for step in range(1, 10):
    if aabb_overlap((step, 0, step + 2, 2), target):
        print("first overlap at step", step)
        break
first overlap at step 5

Test yourself

Why compare squared distances in the circle test?

  1. Squared values are more accurate
  2. It avoids a square root and gives the same yes/no answer for non-negative values
  3. Circles are defined by squares
  4. It handles negative radii

orient⁡(a,b,c)=0\operatorname{orient}(a, b, c) = 0. What do you know?

  1. aa, bb and cc are collinear
  2. cc is inside triangle abab
  3. The segments cross
  4. a=ba = b

Two AABBs overlap on the xx-axis but not on the yy-axis. Do they collide?

  1. Yes
  2. Only if they are squares
  3. No, a horizontal line separates them
  4. It depends on their areas