560 likes | 790 Views
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.
E N D
Video Game Math: Circle-on-Circle collision detection
The math problem: • In a given frame, how do we tell when the puck hits the paddle?
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
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)
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
We need to look at the whole path of both objects! Circle Intersection isn’t Enough!
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 ?
Let L = length of P0P1 Lx = P1x - P0x Ly = P1y - P0y L2= Lx2 + Ly2 Definitions & Constructions P1 L P0 S R
Let C = length of P0S Cx = Sx - P0x Cy = Sy - P0y C2= Cx2 + Cy2 Definitions & Constructions P1 L P0 S C R
Let Θ = angle P2P1S Definitions & Constructions P1 L Θ P0 S C R
Let Θ = angle P2P1S We weren’t given Θ! We’ll solve for it later. Definitions & Constructions P1 L Θ P0 S C R
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
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
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
D(t) = Lt (Lots of ways to derive this) Distance Moved at Time t P1 P(t) L D(t) Θ P0 S C R
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
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
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
So far we have: L2t2 - 2αt + C2 - R2 = 0 So: Applying the Quadratic Formula P1 P(t) L Lt R Θ P0 S C
Discriminant = α2 - L2(C2 - R2) Examining the Discriminant
Discriminant = α2 - L2(C2 - R2) If Discriminant < 0, no solutions Examining the Discriminant P1 P0 S
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
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
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
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
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
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
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
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
K2 = (Lx - Cx)2 + (Ly - Cy)2 Then, a bunch of algebra happens... P1 L K Θ P0 S C
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
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
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
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
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
A Slightly Harder Problem The puck is a circle instead of a point. r P1 S r P0
The Point of Impact The centers are exactly R + r apart. r P1 r R S r P0
The Point of Impact The puck’s center lies on a circle: radius = R + r r P1 r R S r P0
Reduce the Problem Use our solution to the simpler case. P1 R + r S P0
The Original Problem Two Moving Circles S1 R r r P1 P0 S0 R
Change our Frame of Reference Just Imagine that the stick is stationary. r r P1 P0 S0 R
Change our Frame of Reference Consider the relative motion of the puck. r P0 S0 R r P1
Once Again, Reduce the Problem Use our earlier solution. r P0 S0 R r P1
S1 R r r P1 P0 S0 R Let’s See That Again... How exactly do we find the relative motion?
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