1 / 54

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.

gaia
Télécharger la présentation

Video Game Math:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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: • In a given frame, how do we tell when the puck hits the paddle?

  8. 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: Rand r What are we given? 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 < R + r We can find D: D2 = (Px - Sx)2 + (Py - Sy)2 r P D S R

  11. We need to look at the whole path of both objects! Circle Intersection isn’t Enough!

  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. Let L = length of P0P1 Lx = P1x - P0x Ly = P1y - P0y L2= Lx2 + Ly2 Definitions & Constructions P1 L P0 S R

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

  15. Let Θ = angle P2P1S Definitions & Constructions P1 L Θ P0 S C R

  16. Let Θ = angle P2P1S  We weren’t given Θ! We’ll solve for it later. Definitions & Constructions P1 L Θ P0 S C R

  17. Introduce parameter t Px(t) = P0x + t(P1x - P0x) Py(t) = P0y + t(P1y - P0y) P(t) is the point( Px(t), Px(t) ) Equation for Line P0P1 P1 P(t) L Θ P0 S C R

  18. Introduce parameter t Px(t) = P0x + tLx Py(t) = P0y + tLy P(t) is the point( Px(t), Px(t) ) Equation for Line P0P1 P1 P(t) L Θ P0 S C R

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

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

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

  22. R2 = (Lt)2 + C2 - 2CLt cos Θ 0 = L2t2 - 2tCL cos Θ + C2 - R2 Using the Law of Cosines P1 P(t) L Lt R Θ P0 S C

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

  24. So far we have: L2t2 - 2αt + C2 - R2 = 0 So: Applying the Quadratic Formula P1 P(t) L Lt R Θ P0 S C

  25. Discriminant = α2 - L2(C2 - R2) Examining the Discriminant

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

  27. Discriminant = α2 - L2(C2 - R2) If Discriminant < 0, no solutions If Discriminant > 0, two solutions We want the earlier solution. Examining the Discriminant P1 P0 S

  28. 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. Examining the Discriminant P1 P0 S

  29. We only collide if our time of entry is in the range [0,1]. If t > 1, impact comes too late. Is t in range? S P1 P0

  30. 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. Is t in range? P1 P0 S

  31. 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. Is t in range? P1 P0 S

  32. Remember, α = CL cos Θ We construct K By the law of cosines, K2 = L2 + C2 - 2CL cos Θ That is: K2 = L2 + C2 - 2α We still need to solve for Θ! P1 L K Θ P0 S C

  33. We have: K2 = L2 + C2 - 2α We also know: K2= (P2x -Sx)2+(P2y -Sy)2 We only need to solve for α. P1 L K Θ P0 S C

  34. 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 We only need to solve for α. P1 L K Θ P0 S C

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

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

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

  38. 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 Then, a bunch of algebra happens... P1 L K Θ P0 S C

  39. We have: K2 = L2 + C2 - 2LxCx - 2LyCy K2 = L2 + C2 - 2α 2α = 2LxCx + 2LyCy α = LxCx + LyCy Then, a bunch of algebra happens... P1 L K Θ P0 S C

  40. We have: K2 = L2 + C2 - 2LxCx - 2LyCy K2 = L2 + C2 - 2α 2α = 2LxCx + 2LyCy α = LxCx + LyCy AxBx + AyBy = AB cos Θ Then, a bunch of algebra happens... P1 L K Θ 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 puck’s 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. S1 R r r P1 P0 S0 R Let’s See That Again... How exactly do we find the relative motion?

  50. 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

More Related