Title: Video Game Math:
1Video Game Math
- Circle-on-Circle collision detection
2Sample Game 2D Air Hockey
3Like a movie, the action of a video game happens
in frames.
4Like a movie, the action of a video game happens
in frames.
5Like a movie, the action of a video game happens
in frames.
6Like a movie, the action of a video game happens
in frames.
7The math problem
- In a given frame, how do we tell when the puck
hits the paddle?
8What are we given?
- The start and end points of the pucks centerP0
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
S0
S1
P1
P0
9Points are Coordinate Pairs
- Each of the four points we are given is expressed
as an (x,y) pair. - In other words
- P0 (P0x, P0y)
10Circle Intersection is Easy!
- Circles overlap if
- D lt R r
- We can find D
- D2 (Px - Sx)2 (Py - Sy)2
r
P
D
S
R
11Circle Intersection isnt Enough!
- We need to look at the whole path of both objects!
12A 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 ?
13Definitions Constructions
- Let L length of P0P1
- Lx P1x - P0x
- Ly P1y - P0y
- L2 Lx2 Ly2
P1
L
P0
S
R
14Definitions Constructions
- Let C length of P0S
- Cx Sx - P0x
- Cy Sy - P0y
- C2 Cx2 Cy2
P1
L
P0
S
C
R
15Definitions Constructions
P1
L
T
P0
S
C
R
16Definitions Constructions
- Let T angle P2P1S
- ?
- We werent given T!
- Well solve for it later.
P1
L
T
P0
S
C
R
17Equation 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) )
P1
P(t)
L
T
P0
S
C
R
18Equation for Line P0P1
- Introduce parameter t
- Px(t) P0x tLx
- Py(t) P0y tLy
- P(t) is the point ( Px(t), Px(t) )
P1
P(t)
L
T
P0
S
C
R
19Parameter 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
P1
P(t)
L
T
P0
S
C
R
20Distance Moved at Time t
- D(t) Lt
- (Lots of ways to derive this)
P1
P(t)
L
D(t)
T
P0
S
C
R
21Were Finally Ready to Solve!
- Suppose P(t) is the point of impact.
- Solve for t, the time of impact.
P1
P(t)
L
Lt
R
T
P0
S
C
22Using the Law of Cosines
- R2 (Lt)2 C2 - 2CLt cos T
- 0 L2t2 - 2tCL cos T C2 - R2
P1
P(t)
L
Lt
R
T
P0
S
C
23One more definition
- So far we have
- L2t2 - 2tCL cos T C2 - R2 0
- Let a CL cos T
- now
- L2t2 - 2at C2 - R2 0
P1
P(t)
L
Lt
R
T
P0
S
C
24Applying the Quadratic Formula
- So far we have
- L2t2 - 2at C2 - R2 0
- So
-
P1
P(t)
L
Lt
R
T
P0
S
C
25Examining the Discriminant
- Discriminant a2 - L2(C2 - R2)
-
26Examining the Discriminant
- Discriminant a2 - L2(C2 - R2)
- If Discriminant lt 0, no solutions
-
P1
P0
S
27Examining the Discriminant
- Discriminant a2 - L2(C2 - R2)
- If Discriminant lt 0, no solutions
- If Discriminant gt 0, two solutions
-
- We want the earlier solution.
P1
P0
S
28Examining the Discriminant
- Discriminant a2 - L2(C2 - R2)
- If Discriminant lt 0, no solutions
- If Discriminant gt 0, two solutions
- If Discriminant 0, one solution
- We want the earlier solution.
P1
P0
S
29Is t in range?
- We only collide if our time of entry is in the
range 0,1. - If t gt 1, impact comes too late.
S
P1
P0
30Is t in range?
- We only collide if our time of entry is in the
range 0,1. - If t gt 1, impact comes too late.
- If t lt 0, impact is in the past.
P1
P0
S
31Is t in range?
- We only collide if our time of entry is in the
range 0,1. - If t gt 1, impact comes too late.
- If t lt 0, impact is in the past.
- ... Or maybe we started intersecting.
P1
P0
S
32We still need to solve for T!
- Remember, a CL cos T
- We construct K
- By the law of cosines,
- K2 L2 C2 - 2CL cos T
- That is K2 L2 C2 - 2a
P1
L
K
T
P0
S
C
33We only need to solve for a.
- We have K2 L2 C2 - 2a
- We also know
- K2 (P2x - Sx)2 (P2y - Sy)2
P1
L
K
T
P0
S
C
34We only need to solve for a.
- We have K2 L2 C2 - 2a
- We also know
- K2 (P2x - Sx)2 (P2y - Sy)2
- We can also show
- P2x - Sx Lx - Cx
- P2y - Sy Ly - Cy
P1
L
K
T
P0
S
C
35Then, a bunch of algebra happens...
P1
L
K
T
P0
S
C
36Then, a bunch of algebra happens...
- K2 (Lx - Cx)2 (Ly - Cy)2
- Lx2 Cx2 Ly 2 - Cy2
- - 2LxCx - 2 LyCy
-
P1
L
K
T
P0
S
C
37Then, a bunch of algebra happens...
- K2 (Lx - Cx)2 (Ly - Cy)2
- Lx2 Cx2 Ly 2 - Cy2
- - 2LxCx - 2 LyCy
- Lx2 Ly 2 Cx2 Cy2
- - 2LxCx - 2LyCy
-
P1
L
K
T
P0
S
C
38Then, a bunch of algebra happens...
- K2 (Lx - Cx)2 (Ly - Cy)2
- Lx2 Cx2 Ly 2 - Cy2
- - 2LxCx - 2 LyCy
- Lx2 Ly 2 Cx2 Cy2
- - 2LxCx - 2LyCy
- K2 L2 C2 - 2LxCx - 2LyCy
P1
L
K
T
P0
S
C
39Then, a bunch of algebra happens...
- We have
- K2 L2 C2 - 2LxCx - 2LyCy
- K2 L2 C2 - 2a
- 2a 2LxCx 2LyCy
- a LxCx LyCy
-
P1
L
K
T
P0
S
C
40Then, a bunch of algebra happens...
- We have
- K2 L2 C2 - 2LxCx - 2LyCy
- K2 L2 C2 - 2a
- 2a 2LxCx 2LyCy
- a LxCx LyCy
- AxBx AyBy AB cos T
P1
L
K
T
P0
S
C
41A Slightly Harder Problem
- The puck is a circle instead of a point.
r
P1
S
r
P0
42The Point of Impact
- The centers are exactly R r apart.
r
P1
r
R
S
r
P0
43The Point of Impact
- The pucks center lies on a circle
- radius R r
r
P1
r
R
S
r
P0
44Reduce the Problem
- Use our solution to the simpler case.
P1
R r
S
P0
45The Original Problem
S1
R
r
r
P1
P0
S0
R
46Change our Frame of Reference
- Just Imagine that the stick is stationary.
r
r
P1
P0
S0
R
47Change our Frame of Reference
- Consider the relative motion of the puck.
r
P0
S0
R
r
P1
48Once Again, Reduce the Problem
- Use our earlier solution.
r
P0
S0
R
r
P1
49Lets See That Again...
- How exactly do we find the relative motion?
50Lets 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
51Now you can make an Air Hockey Game!
52Other Problems to Solve
- Collision detection for puck vs walls.
- What happens as a result of the collisions.
- All that pesky programming stuff.
53The 3D Problem
- The math is the same, except
- Points are (x, y, z) triples.
- The objects are spheres, not circles.
- L2 Lx2 Ly2 Lz2
- a LxCx LyCy LzCz
54Questions?