Professional Documents
Culture Documents
Video Game Math:: Circle-on-Circle Collision Detection
Video Game Math:: Circle-on-Circle Collision Detection
In other words:
P0 = (P0x, P0y)
Circle Intersection is Easy!
Circles overlap if:
D<R+r
r
We can find D: P D
S
D2 = (Px - Sx)2 + (Py - Sy)2 R
Circle Intersection isn’t Enough!
P0
S
R
Lx = P1x - P0x
L
Ly = P1y - P0y
L2= Lx2 + Ly2 P0
S
R
Definitions & Constructions
Let C = length of P0S P1
Cx = Sx - P0x
L
Cy = Sy - P0y
C2= Cx2 + Cy2 P0
C S
R
Definitions & Constructions
Let Θ = angle P2P1S P1
Θ
P0
C S
R
Definitions & Constructions
Let Θ = angle P2P1S P1
L
We weren’t given Θ! Θ
P0
We’ll solve for it later. C S
R
Equation for Line P0P1
Introduce parameter t P1
P1
D(t) = Lt
P(t) L
D(t)
Θ
P0
C S
R
now:
L2t2 - 2αt + C2 - R2 = 0
Applying the Quadratic Formula
So far we have:
P1
L2t2 - 2αt + C2 - R2 = 0
P(t) L
So: Lt
R
Θ
P0
C S
Examining the Discriminant
Discriminant = α2 - L2(C2 - R2)
Examining the Discriminant
Discriminant = α2 - L2(C2 - R2)
P1
P0
If Discriminant < 0, no solutions
S
Examining the Discriminant
Discriminant = α2 - L2(C2 - R2)
P1
If Discriminant < 0, no solutions
P0
If Discriminant > 0, two solutions
S
S
If t > 1, impact comes too late.
P1
P0
Is t in range?
We only collide if our time of
entry is in the range [0,1]. P1
P0
If t > 1, impact comes too late.
If t < 0, impact is in the past. S
Is t in range?
We only collide if our time of
entry is in the range [0,1].
P1
We construct K L
K
Θ
P0
By the law of cosines, C S
K2 = L2 + C2 - 2CL cos Θ
That is: K2 = L2 + C2 - 2α
We only need to solve for α.
We have: K2 = L2 + C2 - 2α P1
We also know: L
K2 = (P2x - Sx)2 + (P2y - Sy)2 K
Θ
P0
C S
We only need to solve for α.
We have: K2 = L2 + C2 - 2α P1
We also know: L
K2 = (P2x - Sx)2 + (P2y - Sy)2 K
Θ
P0
C S
L
K
Θ
P0
C S
Then, a bunch of algebra
happens...
K2 = (Lx - Cx)2 + (Ly - Cy)2 P1
K2 = L2 + C2 - 2LxCx - 2LyCy
Then, a bunch of algebra
happens...
We have: P1
K2 = L2 + C2 - 2LxCx - 2LyCy
K2 = L2 + C2 - 2α L
K
Θ
P0
C S
2α = 2LxCx + 2LyCy
α = LxCx + LyCy
Then, a bunch of algebra
happens...
We have: P1
K2 = L2 + C2 - 2LxCx - 2LyCy
K2 = L2 + C2 - 2α L
K
Θ
P0
C S
2α = 2LxCx + 2LyCy
α = LxCx + LyCy
r
P1
r S
P0
The Point of Impact
The centers are exactly R + r apart.
r
P1
r
R
r S
P0
The Point of Impact
The puck’s center lies on a circle:
radius = R + r
r
P1
r
R
r S
P0
Reduce the Problem
Use our solution to the simpler case.
P1
R+r
S
P0
The Original Problem
Two Moving Circles
S1
R
r r
P1
P0
S0
R
Change our Frame of Reference
Just Imagine that the stick is stationary.
r r
P1
P0
S0
R
Change our Frame of Reference
Consider the relative motion of the puck.
r
P0
S0
R
r
P1
Once Again, Reduce the Problem
Use our earlier solution.
r
P0
S0
R
r
P1
Let’s See That Again...
How exactly do we find the relative motion?
S1
R
r r P
1
P0
S0
R
Let’s See That Again...
We need to subtract the motion of S from the
motion of P.
S1
P2x = P1x - (S1x - S0x)
P2y = P1y - (S1y - S0y) r P1
P0
S0
R
P2
Now you can make an
Air Hockey Game!
(well, not quite)
Other Problems to Solve
• Collision detection for puck vs walls.
• What happens as a result of the collisions.
• All that pesky programming stuff.
The 3D Problem
The math is the same, except:
• Points are (x, y, z) triples.
• The objects are spheres, not circles.
• L2= Lx2 + Ly2 + Lz2
• α = LxCx + LyCy+ LzCz
Questions?