# Video Game Math: Circle-on-Circle collision detection.

## Presentation on theme: "Video Game Math: Circle-on-Circle collision detection."— Presentation transcript:

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.

The math problem: 1.In a given frame, how do we tell when the puck hits the paddle?

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

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 )

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

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

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 ?

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

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

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

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

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)

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)

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)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 )

Now you can make an Air Hockey Game! (well, not quite)

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

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

Questions?

Similar presentations