Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 54

Video Game Math:

Circle-on-Circle collision detection


Sample Game: 2D Air Hockey
Like a movie, the action of a video
game happens in frames.
Like a movie, the action of a video
game happens in frames.
Like a movie, the action of a video
game happens in frames.
Like a movie, the action of a video
game happens in frames.
The math problem:
• In a given frame, how do we tell when the
puck hits the paddle?
What are we given?
• The start and end points
of the puck’s center:
P0 and P1
S0
• The start and end points
of the paddle (stick)’s
center: S0 and S1 S1
P1 P0
• The radius of the stick
and puck: R and r
Points are Coordinate Pairs
• Each of the four points we are given is
expressed as an (x,y) pair.

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!

We need to look at the


whole path of both objects!
A Simpler Problem
What if the puck is very small, and the stick
never moves? P1

P0
S
R

In other words, what if r = 0 and S0 = S1 ?


Definitions & Constructions
Let L = length of P0P1 P1

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

Px(t) = P0x + t(P1x - P0x) P(t) L


Py(t) = P0y + t(P1y - P0y) Θ
P0
C S
R

P(t) is the point ( Px(t), Px(t) )


Equation for Line P0P1
Introduce parameter t P1

Px(t) = P0x + tLx P(t) L


Py(t) = P0y + tLy Θ
P0
C S
R

P(t) is the point ( Px(t), Px(t) )


Parameter t is Time
Px(t) = P0x + t(P1x - P0x)
P1
Py(t) = P0y + t(P1y - P0y)
P(t) L
Beginning of frame:
Θ
P(0) = P0 P0
C S
R
End of frame:
P(1) = P1
Distance Moved at Time t

P1
D(t) = Lt
P(t) L
D(t)
Θ
P0
C S
R

(Lots of ways to derive this)


We’re Finally Ready to Solve!
Suppose P(t) is the P1
point of impact.
P(t) L
Lt
Solve for t, the time of R
Θ
impact. P0
S
C
Using the Law of Cosines
R2 = (Lt)2 + C2 - 2CLt cos Θ P1

0 = L2t2 - 2tCL cos Θ + C2 - R2 P(t) L


Lt
R
Θ
P0
C S
One more definition
So far we have: P1
L2t2 - 2tCL cos Θ + C2 - R2 = 0
P(t) L
Lt
Let α = CL cos Θ R
Θ
P0
C S

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

We want the earlier solution.


Examining the Discriminant
Discriminant = α2 - L2(C2 - R2)
P1
If Discriminant < 0, no solutions P0

If Discriminant > 0, two solutions


If Discriminant = 0, one solution S

We want the earlier solution.


Is t in range?
We only collide if our time of
entry is in the range [0,1].

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

If t > 1, impact comes too late.


P0
If t < 0, impact is in the past. S
... Or maybe we started
intersecting.
We still need to solve for Θ!
Remember, α = CL cos Θ 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

We can also show:


P2x - Sx = Lx - Cx
P2y - Sy = Ly - Cy
Then, a bunch of algebra
happens...
K2 = (Lx - Cx)2 + (Ly - Cy)2 P1

L
K
Θ
P0
C S
Then, a bunch of algebra
happens...
K2 = (Lx - Cx)2 + (Ly - Cy)2 P1

= Lx2 + Cx2 + Ly 2 - Cy2 L


- 2LxCx - 2 LyCy K
Θ
P0
C S
Then, a bunch of algebra
happens...
K2 = (Lx - Cx)2 + (Ly - Cy)2 P1

= Lx2 + Cx2 + Ly 2 - Cy2 L


- 2LxCx - 2 LyCy K
Θ
P0
C S
= Lx2 + Ly 2 + Cx2 + Cy2
- 2LxCx - 2LyCy
Then, a bunch of algebra
happens...
K2 = (Lx - Cx)2 + (Ly - Cy)2
P1

= Lx2 + Cx2 + Ly 2 - Cy2


L
- 2LxCx - 2 LyCy K
Θ
P0
C S
= Lx2 + Ly 2 + Cx2 + Cy2
- 2LxCx - 2LyCy

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

AxBx + AyBy = AB cos Θ


A Slightly Harder Problem
The puck is a circle instead of a point.

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?

You might also like