Download - Video Game Math:
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
• The start and end points of the paddle (stick)’s center: S0 and S1
• The radius of the stick and puck: R and r
P1 P0S1
S0
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)
Circles overlap if:
D < R + r
We can find D:
D2 = (Px - Sx)2 + (Py - Sy)2
Circle Intersection is Easy!
SR
P rD
Circle Intersection isn’t Enough!
We need to look at the whole path of both objects!
A Simpler ProblemWhat if the puck is very small, and the stick never moves?
S
R
P0
P1
In other words, what if r = 0 and S0 = S1 ?
Definitions & Constructions
Let L = length of P0P1
Lx = P1x - P0x
Ly = P1y - P0y
L2= Lx2 + Ly
2S
R
P0
P1
L
Definitions & Constructions
Let C = length of P0S
Cx = Sx - P0x
Cy = Sy - P0y
C2= Cx2 + Cy
2S
RC
L
P0
P1
Definitions & Constructions
Let Θ = angle P2P1S
SP0
P1
C
L
Θ
R
Definitions & Constructions
Let Θ = angle P2P1S
We weren’t given Θ!
We’ll solve for it later. S
P1
C
L
Θ
R
P0
Equation for Line P0P1
Introduce parameter t
Px(t) = P0x + t(P1x - P0x)
Py(t) = P0y + t(P1y - P0y)
P(t) is the point ( Px(t), Px(t) )
S
P1
C
L
Θ
R
P0
P(t)
Equation for Line P0P1
Introduce parameter t
Px(t) = P0x + tLx
Py(t) = P0y + tLy
P(t) is the point ( Px(t), Px(t) )
S
P1
C
L
Θ
R
P0
P(t)
Parameter t is Time
Px(t) = P0x + t(P1x - P0x)
Py(t) = P0y + t(P1y - P0y)
Beginning of frame:
P(0) = P0
End of frame:
P(1) = P1
S
P1
C
L
Θ
R
P0
P(t)
Distance Moved at Time t
D(t) = Lt
(Lots of ways to derive this)
S
P1
C
L
Θ
R
P0
P(t)D(t)
We’re Finally Ready to Solve!
Suppose P(t) is the point of impact.
Solve for t, the time of impact.
S
R
P0
P1
C
P(t) L
Θ
Lt
Using the Law of Cosines
R2 = (Lt)2 + C2 - 2CLt cos Θ
0 = L2t2 - 2tCL cos Θ + C2 - R2
S
R
P1
C
P(t) L
Θ
Lt
P0
One more definition
So far we have:
L2t2 - 2tCL cos Θ + C2 - R2 = 0
Let α = CL cos Θ
now:
L2t2 - 2αt + C2 - R2 = 0
S
R
P1
C
P(t) L
Θ
Lt
P0
Applying the Quadratic Formula
So far we have:
L2t2 - 2αt + C2 - R2 = 0
So:
S
R
P1
C
P(t) L
Θ
Lt
P0
Examining the Discriminant
Discriminant = α2 - L2(C2 - R2)
Examining the Discriminant
Discriminant = α2 - L2(C2 - R2)
If Discriminant < 0, no solutions
S
P0
P1
Examining the Discriminant
Discriminant = α2 - L2(C2 - R2)
If Discriminant < 0, no solutions
If Discriminant > 0, two solutions
We want the earlier solution.
S
P0
P1
Examining the Discriminant
Discriminant = α2 - L2(C2 - R2)
If Discriminant < 0, no solutions
If Discriminant > 0, two solutions
If Discriminant = 0, one solution
We want the earlier solution.
S
P0
P1
Is t in range?
We only collide if our time of entry is in the range [0,1].
If t > 1, impact comes too late. S
P0
P1
Is t in range?
We only collide if our time of entry is in the range [0,1].
If t > 1, impact comes too late.
If t < 0, impact is in the past. S
P1
P0
Is t in range?
We only collide if our time of entry is in the range [0,1].
If t > 1, impact comes too late.
If t < 0, impact is in the past.
... Or maybe we started intersecting.
S
P1
P0
We still need to solve for Θ!
Remember, α = CL cos Θ
We construct K
By the law of cosines,
K2 = L2 + C2 - 2CL cos Θ
That is: K2 = L2 + C2 - 2α
SP0
P1
L
ΘK
C
We only need to solve for α.
We have: K2 = L2 + C2 - 2α
We also know:
K2 = (P2x - Sx)2
+ (P2y - Sy)2
SP0
P1
L
ΘK
C
We only need to solve for α.
We have: K2 = L2 + C2 - 2α
We also know:
K2 = (P2x - Sx)2
+ (P2y - Sy)2
We can also show:
P2x - Sx = Lx - Cx
P2y - Sy = Ly - Cy
SP0
P1
L
ΘK
C
Then, a bunch of algebra happens...
K2 = (Lx - Cx)2 + (Ly - Cy)2
SP0
P1
L
ΘK
C
Then, a bunch of algebra happens...
K2 = (Lx - Cx)2 + (Ly - Cy)2
= Lx2
+ Cx2 + Ly
2 - Cy
2
- 2LxCx - 2 LyCy S
P0
P1
L
ΘK
C
Then, a bunch of algebra happens...
K2 = (Lx - Cx)2 + (Ly - Cy)2
= Lx2
+ Cx2 + Ly
2 - Cy
2
- 2LxCx - 2 LyCy
= Lx2
+ Ly 2 + Cx
2 + Cy2
- 2LxCx - 2LyCy
SP0
P1
L
ΘK
C
Then, a bunch of algebra happens...
K2 = (Lx - Cx)2 + (Ly - Cy)2
= Lx2
+ Cx2 + Ly
2 - Cy
2
- 2LxCx - 2 LyCy
= Lx2
+ Ly 2 + Cx
2 + Cy2
- 2LxCx - 2LyCy
K2 = L2 + C2 - 2LxCx - 2LyCy
SP0
P1
L
ΘK
C
Then, a bunch of algebra happens...
We have:
K2 = L2 + C2 - 2LxCx - 2LyCy
K2 = L2 + C2 - 2α
2α = 2LxCx + 2LyCy
α = LxCx + LyCy
SP0
P1
L
ΘK
C
Then, a bunch of algebra happens...
We have:
K2 = L2 + C2 - 2LxCx - 2LyCy
K2 = L2 + C2 - 2α
2α = 2LxCx + 2LyCy
α = LxCx + LyCy
AxBx + AyBy = AB cos Θ
SP0
P1
L
ΘK
C
A Slightly Harder ProblemThe puck is a circle instead of a point.
S
P0
P1
r
r
The Point of ImpactThe centers are exactly R + r apart.
SR
P0
P1
r
r
r
The Point of ImpactThe puck’s center lies on a circle:
radius = R + r
SR
P0
P1
r
r
r
Reduce the ProblemUse our solution to the simpler case.
SR + r
P0
P1
The Original ProblemTwo Moving Circles
S0
R
P0
P1
rr
S1
R
Change our Frame of ReferenceJust Imagine that the stick is stationary.
S0
R
P0
P1
rr
Change our Frame of ReferenceConsider the relative motion of the puck.
S0
R
P0
P1
r
r
Once Again, Reduce the ProblemUse our earlier solution.
S0
R
P0
P1
r
r
Let’s See That Again...How exactly do we find the relative motion?
S0R
P0
P1rr
S1
R
Let’s See That Again...We need to subtract the motion of S from the motion of P.
S0
R
P0
P1r
S1
P2
P2x = P1x - (S1x - S0x)
P2y = P1y - (S1y - S0y)
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 + Ly
2 + Lz2
• α = LxCx + LyCy+ LzCz
Questions?