54
Video Game Math: Circle-on-Circle collision detection

Video Game Math:

  • Upload
    gaia

  • View
    51

  • Download
    3

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Video Game Math:

Video Game Math:

Circle-on-Circle collision detection

Page 2: Video Game Math:

Sample Game: 2D Air Hockey

Page 3: Video Game Math:

Like a movie, the action of a video game happens in frames.

Page 4: Video Game Math:

Like a movie, the action of a video game happens in frames.

Page 5: Video Game Math:

Like a movie, the action of a video game happens in frames.

Page 6: Video Game Math:

Like a movie, the action of a video game happens in frames.

Page 7: Video Game Math:

The math problem:

• In a given frame, how do we tell when the puck hits the paddle?

Page 8: Video Game Math:

What are we given?

• 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: R and r

P1 P0S1

S0

Page 9: Video Game Math:

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)

Page 10: Video Game Math:

Circles overlap if:

D < R + r

We can find D:

D2 = (Px - Sx)2 + (Py - Sy)2

Circle Intersection is Easy!

SR

P rD

Page 11: Video Game Math:

Circle Intersection isn’t Enough!

We need to look at the whole path of both objects!

Page 12: Video Game Math:

A Simpler ProblemWhat if the puck is very small, and the stick never moves?

S

R

P0

P1

In other words, what if r = 0 and S0 = S1 ?

Page 13: Video Game Math:

Definitions & Constructions

Let L = length of P0P1

Lx = P1x - P0x

Ly = P1y - P0y

L2= Lx2 + Ly

2S

R

P0

P1

L

Page 14: Video Game Math:

Definitions & Constructions

Let C = length of P0S

Cx = Sx - P0x

Cy = Sy - P0y

C2= Cx2 + Cy

2S

RC

L

P0

P1

Page 15: Video Game Math:

Definitions & Constructions

Let Θ = angle P2P1S

SP0

P1

C

L

Θ

R

Page 16: Video Game Math:

Definitions & Constructions

Let Θ = angle P2P1S

We weren’t given Θ!

We’ll solve for it later. S

P1

C

L

Θ

R

P0

Page 17: Video Game Math:

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) )

S

P1

C

L

Θ

R

P0

P(t)

Page 18: Video Game Math:

Equation for Line P0P1

Introduce parameter t

Px(t) = P0x + tLx

Py(t) = P0y + tLy

P(t) is the point ( Px(t), Px(t) )

S

P1

C

L

Θ

R

P0

P(t)

Page 19: Video Game Math:

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

S

P1

C

L

Θ

R

P0

P(t)

Page 20: Video Game Math:

Distance Moved at Time t

D(t) = Lt

(Lots of ways to derive this)

S

P1

C

L

Θ

R

P0

P(t)D(t)

Page 21: Video Game Math:

We’re Finally Ready to Solve!

Suppose P(t) is the point of impact.

Solve for t, the time of impact.

S

R

P0

P1

C

P(t) L

Θ

Lt

Page 22: Video Game Math:

Using the Law of Cosines

R2 = (Lt)2 + C2 - 2CLt cos Θ

0 = L2t2 - 2tCL cos Θ + C2 - R2

S

R

P1

C

P(t) L

Θ

Lt

P0

Page 23: Video Game Math:

One more definition

So far we have:

L2t2 - 2tCL cos Θ + C2 - R2 = 0

Let α = CL cos Θ

now:

L2t2 - 2αt + C2 - R2 = 0

S

R

P1

C

P(t) L

Θ

Lt

P0

Page 24: Video Game Math:

Applying the Quadratic Formula

So far we have:

L2t2 - 2αt + C2 - R2 = 0

So:

S

R

P1

C

P(t) L

Θ

Lt

P0

Page 25: Video Game Math:

Examining the Discriminant

Discriminant = α2 - L2(C2 - R2)

Page 26: Video Game Math:

Examining the Discriminant

Discriminant = α2 - L2(C2 - R2)

If Discriminant < 0, no solutions

S

P0

P1

Page 27: Video Game Math:

Examining the Discriminant

Discriminant = α2 - L2(C2 - R2)

If Discriminant < 0, no solutions

If Discriminant > 0, two solutions

We want the earlier solution.

S

P0

P1

Page 28: Video Game Math:

Examining the Discriminant

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.

S

P0

P1

Page 29: Video Game Math:

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

P0

P1

Page 30: Video Game Math:

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

P1

P0

Page 31: Video Game Math:

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

P1

P0

Page 32: Video Game Math:

We still need to solve for Θ!

Remember, α = CL cos Θ

We construct K

By the law of cosines,

K2 = L2 + C2 - 2CL cos Θ

That is: K2 = L2 + C2 - 2α

SP0

P1

L

ΘK

C

Page 33: Video Game Math:

We only need to solve for α.

We have: K2 = L2 + C2 - 2α

We also know:

K2 = (P2x - Sx)2

+ (P2y - Sy)2

SP0

P1

L

ΘK

C

Page 34: Video Game Math:

We only need to solve for α.

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

SP0

P1

L

ΘK

C

Page 35: Video Game Math:

Then, a bunch of algebra happens...

K2 = (Lx - Cx)2 + (Ly - Cy)2

SP0

P1

L

ΘK

C

Page 36: Video Game Math:

Then, a bunch of algebra happens...

K2 = (Lx - Cx)2 + (Ly - Cy)2

= Lx2

+ Cx2 + Ly

2 - Cy

2

- 2LxCx - 2 LyCy S

P0

P1

L

ΘK

C

Page 37: Video Game Math:

Then, a bunch of algebra happens...

K2 = (Lx - Cx)2 + (Ly - Cy)2

= Lx2

+ Cx2 + Ly

2 - Cy

2

- 2LxCx - 2 LyCy

= Lx2

+ Ly 2 + Cx

2 + Cy2

- 2LxCx - 2LyCy

SP0

P1

L

ΘK

C

Page 38: Video Game Math:

Then, a bunch of algebra happens...

K2 = (Lx - Cx)2 + (Ly - Cy)2

= Lx2

+ Cx2 + Ly

2 - Cy

2

- 2LxCx - 2 LyCy

= Lx2

+ Ly 2 + Cx

2 + Cy2

- 2LxCx - 2LyCy

K2 = L2 + C2 - 2LxCx - 2LyCy

SP0

P1

L

ΘK

C

Page 39: Video Game Math:

Then, a bunch of algebra happens...

We have:

K2 = L2 + C2 - 2LxCx - 2LyCy

K2 = L2 + C2 - 2α

2α = 2LxCx + 2LyCy

α = LxCx + LyCy

SP0

P1

L

ΘK

C

Page 40: Video Game Math:

Then, a bunch of algebra happens...

We have:

K2 = L2 + C2 - 2LxCx - 2LyCy

K2 = L2 + C2 - 2α

2α = 2LxCx + 2LyCy

α = LxCx + LyCy

AxBx + AyBy = AB cos Θ

SP0

P1

L

ΘK

C

Page 41: Video Game Math:

A Slightly Harder ProblemThe puck is a circle instead of a point.

S

P0

P1

r

r

Page 42: Video Game Math:

The Point of ImpactThe centers are exactly R + r apart.

SR

P0

P1

r

r

r

Page 43: Video Game Math:

The Point of ImpactThe puck’s center lies on a circle:

radius = R + r

SR

P0

P1

r

r

r

Page 44: Video Game Math:

Reduce the ProblemUse our solution to the simpler case.

SR + r

P0

P1

Page 45: Video Game Math:

The Original ProblemTwo Moving Circles

S0

R

P0

P1

rr

S1

R

Page 46: Video Game Math:

Change our Frame of ReferenceJust Imagine that the stick is stationary.

S0

R

P0

P1

rr

Page 47: Video Game Math:

Change our Frame of ReferenceConsider the relative motion of the puck.

S0

R

P0

P1

r

r

Page 48: Video Game Math:

Once Again, Reduce the ProblemUse our earlier solution.

S0

R

P0

P1

r

r

Page 49: Video Game Math:

Let’s See That Again...How exactly do we find the relative motion?

S0R

P0

P1rr

S1

R

Page 50: Video Game Math:

Let’s See That Again...We need to subtract the motion of S from the motion of P.

S0

R

P0

P1r

S1

P2

P2x = P1x - (S1x - S0x)

P2y = P1y - (S1y - S0y)

Page 51: Video Game Math:

Now you can make an Air Hockey Game!

(well, not quite)

Page 52: Video Game Math:

Other Problems to Solve

• Collision detection for puck vs walls.

• What happens as a result of the collisions.

• All that pesky programming stuff.

Page 53: Video Game Math:

The 3D Problem

The math is the same, except:

• Points are (x, y, z) triples.

• The objects are spheres, not circles.

• L2= Lx2 + Ly

2 + Lz2

• α = LxCx + LyCy+ LzCz

Page 54: Video Game Math:

Questions?