Download presentation

Presentation is loading. Please wait.

Published byBertha Jennings Modified about 1 year ago

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

5

6

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 puck’s center: P 0 and P 1 The start and end points of the paddle (stick)’s center: S 0 and S 1 The radius of the stick and puck: R and r P1P1 P0P0 S1S1 S0S0

9
Points are Coordinate Pairs Each of the four points we are given is expressed as an (x,y) pair. In other words: P 0 = (P 0x, P 0y )

10
Circles overlap if: D < R + r We can find D: D 2 = (P x - S x ) 2 + (P y - S y ) 2 Circle Intersection is Easy! S R P r D

11
Circle Intersection isn’t 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? S R P0P0 P1P1 In other words, what if r = 0 and S 0 = S 1 ?

13
Definitions & Constructions Let L = length of P 0 P 1 L x = P 1x - P 0x L y = P 1y - P 0y L 2 = L x 2 + L y 2 S R P0P0 P1P1 L

14
Definitions & Constructions Let C = length of P 0 S C x = S x - P 0x C y = S y - P 0y C 2 = C x 2 + C y 2 S R C L P0P0 P1P1

15
Definitions & Constructions Let Θ = angle P 2 P 1 S S P0P0 P1P1 C L Θ R

16
Definitions & Constructions Let Θ = angle P 2 P 1 S We weren’t given Θ! We’ll solve for it later. S P1P1 C L Θ R P0P0

17
Equation for Line P 0 P 1 Introduce parameter t P x (t) = P 0x + t(P 1x - P 0x ) P y (t) = P 0y + t(P 1y - P 0y ) P(t) is the point ( P x (t), P x (t) ) S P1P1 C L Θ R P0P0 P(t)

18
Equation for Line P 0 P 1 Introduce parameter t P x (t) = P 0x + tL x P y (t) = P 0y + tL y P(t) is the point ( P x (t), P x (t) ) S P1P1 C L Θ R P0P0 P(t)

19
Parameter t is Time P x (t) = P 0x + t(P 1x - P 0x ) P y (t) = P 0y + t(P 1y - P 0y ) Beginning of frame: P(0) = P 0 End of frame: P(1) = P 1 S P1P1 C L Θ R P0P0 P(t)

20
Distance Moved at Time t D(t) = Lt (Lots of ways to derive this) S P1P1 C L Θ R P0P0 P(t) D(t)

21
We’re Finally Ready to Solve! Suppose P(t) is the point of impact. Solve for t, the time of impact. S R P0P0 P1P1 C P(t) L Θ Lt

22
Using the Law of Cosines R 2 = (Lt) 2 + C 2 - 2CLt cos Θ 0 = L 2 t 2 - 2tCL cos Θ + C 2 - R 2 S R P1P1 C P(t) L Θ Lt P0P0

23
One more definition So far we have: L 2 t 2 - 2tCL cos Θ + C 2 - R 2 = 0 Let α = CL cos Θ now: L 2 t 2 - 2αt + C 2 - R 2 = 0 S R P1P1 C P(t) L Θ Lt P0P0

24
Applying the Quadratic Formula So far we have: L 2 t 2 - 2αt + C 2 - R 2 = 0 So: S R P1P1 C P(t) L Θ Lt P0P0

25
Examining the Discriminant Discriminant = α 2 - L 2 (C 2 - R 2)

26
Examining the Discriminant Discriminant = α 2 - L 2 (C 2 - R 2) If Discriminant < 0, no solutions S P0P0 P1P1

27
Examining the Discriminant Discriminant = α 2 - L 2 (C 2 - R 2) If Discriminant < 0, no solutions If Discriminant > 0, two solutions We want the earlier solution. S P0P0 P1P1

28
Examining the Discriminant Discriminant = α 2 - L 2 (C 2 - R 2) If Discriminant < 0, no solutions If Discriminant > 0, two solutions If Discriminant = 0, one solution We want the earlier solution. S P0P0 P1P1

29
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 P0P0 P1P1

30
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 P1P1 P0P0

31
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 P1P1 P0P0

32
We still need to solve for Θ! Remember, α = CL cos Θ We construct K By the law of cosines, K 2 = L 2 + C 2 - 2CL cos Θ That is: K 2 = L 2 + C 2 - 2α S P0P0 P1P1 L Θ K C

33
We only need to solve for α. We have: K 2 = L 2 + C 2 - 2α We also know: K 2 = (P 2x - S x ) 2 + (P 2y - S y ) 2 S P0P0 P1P1 L Θ K C

34
We only need to solve for α. We have: K 2 = L 2 + C 2 - 2α We also know: K 2 = (P 2x - S x ) 2 + (P 2y - S y ) 2 We can also show: P 2x - S x = L x - C x P 2y - S y = L y - C y S P0P0 P1P1 L Θ K C

35
Then, a bunch of algebra happens... K 2 = (L x - C x ) 2 + (L y - C y ) 2 S P0P0 P1P1 L Θ K C

36
Then, a bunch of algebra happens... K 2 = (L x - C x ) 2 + (L y - C y ) 2 = L x 2 + C x 2 + L y 2 - C y 2 - 2L x C x - 2 L y C y S P0P0 P1P1 L Θ K C

37
Then, a bunch of algebra happens... K 2 = (L x - C x ) 2 + (L y - C y ) 2 = L x 2 + C x 2 + L y 2 - C y 2 - 2L x C x - 2 L y C y = L x 2 + L y 2 + C x 2 + C y 2 - 2L x C x - 2L y C y S P0P0 P1P1 L Θ K C

38
Then, a bunch of algebra happens... K 2 = (L x - C x ) 2 + (L y - C y ) 2 = L x 2 + C x 2 + L y 2 - C y 2 - 2L x C x - 2 L y C y = L x 2 + L y 2 + C x 2 + C y 2 - 2L x C x - 2L y C y K 2 = L 2 + C 2 - 2L x C x - 2L y C y S P0P0 P1P1 L Θ K C

39
Then, a bunch of algebra happens... We have: K 2 = L 2 + C 2 - 2L x C x - 2L y C y K 2 = L 2 + C 2 - 2α 2α = 2L x C x + 2L y C y α = L x C x + L y C y S P0P0 P1P1 L Θ K C

40
Then, a bunch of algebra happens... We have: K 2 = L 2 + C 2 - 2L x C x - 2L y C y K 2 = L 2 + C 2 - 2α 2α = 2L x C x + 2L y C y α = L x C x + L y C y A x B x + A y B y = AB cos Θ S P0P0 P1P1 L Θ K C

41
A Slightly Harder Problem The puck is a circle instead of a point. S P0P0 P1P1 r r

42
The Point of Impact The centers are exactly R + r apart. S R P0P0 P1P1 r r r

43
The Point of Impact The puck’s center lies on a circle: radius = R + r S R P0P0 P1P1 r r r

44
Reduce the Problem Use our solution to the simpler case. S R + r P0P0 P1P1

45
The Original Problem Two Moving Circles S0S0 R P0P0 P1P1 r r S1S1 R

46
Change our Frame of Reference Just Imagine that the stick is stationary. S0S0 R P0P0 P1P1 r r

47
Change our Frame of Reference Consider the relative motion of the puck. S0S0 R P0P0 P1P1 r r

48
Once Again, Reduce the Problem Use our earlier solution. S0S0 R P0P0 P1P1 r r

49
Let’s See That Again... How exactly do we find the relative motion? S0S0 R P0P0 P1P1 r r S1S1 R

50
Let’s See That Again... We need to subtract the motion of S from the motion of P. S0S0 R P0P0 P1P1 r S1S1 P2P2 P 2x = P 1x - (S 1x - S 0x ) P 2y = P 1y - (S 1y - S 0y )

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. L 2 = L x 2 + L y 2 + L z 2 α = L x C x + L y C y + L z C z

54
Questions?

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google