Video Game Math: - PowerPoint PPT Presentation

About This Presentation
Title:

Video Game Math:

Description:

Like a movie, the action of a video game happens in frames. ... Now you can make an. Air Hockey Game! (well, not quite) Other Problems to Solve ... – PowerPoint PPT presentation

Number of Views:57
Avg rating:3.0/5.0
Slides: 55
Provided by: Mleb8
Category:
Tags: game | math | video

less

Transcript and Presenter's Notes

Title: Video Game Math:


1
Video Game Math
  • Circle-on-Circle collision detection

2
Sample Game 2D Air Hockey
3
Like a movie, the action of a video game happens
in frames.
4
Like a movie, the action of a video game happens
in frames.
5
Like a movie, the action of a video game happens
in frames.
6
Like a movie, the action of a video game happens
in frames.
7
The math problem
  1. In a given frame, how do we tell when the puck
    hits the paddle?

8
What 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
9
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)

10
Circle 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
11
Circle Intersection isnt Enough!
  • We need to look at the whole path of both objects!

12
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 ?
13
Definitions Constructions
  • Let L length of P0P1
  • Lx P1x - P0x
  • Ly P1y - P0y
  • L2 Lx2 Ly2

P1
L
P0
S
R
14
Definitions Constructions
  • Let C length of P0S
  • Cx Sx - P0x
  • Cy Sy - P0y
  • C2 Cx2 Cy2

P1
L
P0
S
C
R
15
Definitions Constructions
  • Let T angle P2P1S

P1
L
T
P0
S
C
R
16
Definitions Constructions
  • Let T angle P2P1S
  • ?
  • We werent given T!
  • Well solve for it later.

P1
L
T
P0
S
C
R
17
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) )

P1
P(t)
L
T
P0
S
C
R
18
Equation 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
19
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

P1
P(t)
L
T
P0
S
C
R
20
Distance Moved at Time t
  • D(t) Lt
  • (Lots of ways to derive this)

P1
P(t)
L
D(t)
T
P0
S
C
R
21
Were 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
22
Using 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
23
One 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
24
Applying the Quadratic Formula
  • So far we have
  • L2t2 - 2at C2 - R2 0
  • So

P1
P(t)
L
Lt
R
T
P0
S
C
25
Examining the Discriminant
  • Discriminant a2 - L2(C2 - R2)

26
Examining the Discriminant
  • Discriminant a2 - L2(C2 - R2)
  • If Discriminant lt 0, no solutions

P1
P0
S
27
Examining 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
28
Examining 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
29
Is 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
30
Is 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
31
Is 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
32
We 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
33
We 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
34
We 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
35
Then, a bunch of algebra happens...
  • K2 (Lx - Cx)2 (Ly - Cy)2

P1
L
K
T
P0
S
C
36
Then, 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
37
Then, 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
38
Then, 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
39
Then, 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
40
Then, 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
41
A Slightly Harder Problem
  • The puck is a circle instead of a point.

r
P1
S
r
P0
42
The Point of Impact
  • The centers are exactly R r apart.

r
P1
r
R
S
r
P0
43
The Point of Impact
  • The pucks center lies on a circle
  • radius R r

r
P1
r
R
S
r
P0
44
Reduce the Problem
  • Use our solution to the simpler case.

P1
R r
S
P0
45
The Original Problem
  • Two Moving Circles

S1
R
r
r
P1
P0
S0
R
46
Change our Frame of Reference
  • Just Imagine that the stick is stationary.

r
r
P1
P0
S0
R
47
Change our Frame of Reference
  • Consider the relative motion of the puck.

r
P0
S0
R
r
P1
48
Once Again, Reduce the Problem
  • Use our earlier solution.

r
P0
S0
R
r
P1
49
Lets See That Again...
  • How exactly do we find the relative motion?

50
Lets 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
51
Now you can make an Air Hockey Game!
  • (well, not quite)

52
Other Problems to Solve
  • Collision detection for puck vs walls.
  • What happens as a result of the collisions.
  • All that pesky programming stuff.

53
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
  • a LxCx LyCy LzCz

54
Questions?
Write a Comment
User Comments (0)
About PowerShow.com