90
Game Programming Physics Nick Prühs

Game Programming 11 - Game Physics

Embed Size (px)

Citation preview

Page 1: Game Programming 11 - Game Physics

Game ProgrammingPhysics

Nick Prühs

Page 2: Game Programming 11 - Game Physics

Objectives

• To understand the basics of kinematics and dynamics in games

• To get an overview of a simple numeric integration approach for

phyics

• To learn how to resolve rigid body collisions

2 / 83

Page 3: Game Programming 11 - Game Physics

Motivation

• Next thing to make your game feel right, besides graphics and

sound

• Can be integral part of your gameplay

• Usually just a close approximation to real physics will be enough

“Speedy thing goes in, speedy thing comes out.”

- GLaDOS

3 / 83

Page 4: Game Programming 11 - Game Physics

Kinematics vs. Dynamics

• Kinematics is the study of movement over time.

Doesn’t matter why things are where there are now

Doesn’t matter what causes the movement

Just deals with the actual movement itself

• Dynamics is the study of forces and masses that cause kinematic

quantities to change over time.

4 / 83

Page 5: Game Programming 11 - Game Physics

Kinematics – Velocity

Velocity is the rate of change of position over time.

5 / 83

𝒗 =𝒅𝒙

𝒅𝒕

Page 6: Game Programming 11 - Game Physics

Kinematics – Acceleration

Acceleration is the rate of change of velocity over time.

6 / 83

𝒂 =𝒅𝒗

𝒅𝒕

Page 7: Game Programming 11 - Game Physics

Change of velocity

Solving for v and integrating yields the velocity after a given time t,

aside from some unknown constant C:

7 / 83

𝐚 =𝐝𝐯

𝐝𝐭

𝒅𝒗 = 𝒂 𝒅𝒕

𝒗(𝒕) = න𝒂 𝒅𝒕

𝒗(𝒕) = 𝒂𝒕 + 𝑪

Page 8: Game Programming 11 - Game Physics

Change of velocity

We can find the unknown constant to be the initial velocity by

computing the initial velocity:

8 / 83

𝒗 = 𝒂𝒕 + 𝑪

𝒗𝟎 = 𝟎𝒂 + 𝑪

𝒗𝟎 = 𝑪

Page 9: Game Programming 11 - Game Physics

Change of velocity

Thus, given the acceleration a and initial velocity v0, the velocity after

any given time t is

9 / 83

𝒗(𝒕) = 𝒂𝒕 + 𝒗𝟎

Page 10: Game Programming 11 - Game Physics

Change of position

The position after any given time t can be found the same way:

10 / 83

𝐯 =𝐝𝐱

𝐝𝐭

𝒅𝒙 = 𝒗 𝒅𝒕

𝒅𝒙 = 𝒂𝒕 + 𝒗𝟎 𝒅𝒕

𝒙(𝒕) = න𝒂𝒕 + 𝒗𝟎 𝒅𝒕

𝒙(𝒕) =𝟏

𝟐𝒂𝒕𝟐 + 𝒗𝟎𝒕 + 𝒙𝟎

Page 11: Game Programming 11 - Game Physics

Kinematics – Momentum

Momentum is the product of the mass and velocity of an object.

11 / 83

𝒑 = 𝒎𝒗

Page 12: Game Programming 11 - Game Physics

Dynamics – Force

Force is the rate of change of momentum over time (Newton’s Second

Law).

12 / 83

𝑭 =𝒅𝒑

𝒅𝒕

Page 13: Game Programming 11 - Game Physics

Change of acceleration

For constant mass, force and acceleration are related as follows:

13 / 83

𝐹 = 𝐝𝐩

𝐝𝐭

definition force

= 𝒅 𝒎𝒗

𝒅𝒕

definitionmomentum

=𝒎𝒅𝒗

𝒅𝒕

constant mass

= 𝒎𝒂 definition acceleration

Page 14: Game Programming 11 - Game Physics

Numerical Integration

• Start at a certain initial position and velocity

• Take a small step forward in time to find the velocity and position at

the next time value

• Do this repeatedly to go forward in time in small increments, each

time taking the results of the previous integration as the starting

point for the next

14 / 83

Page 15: Game Programming 11 - Game Physics

Explicit Euler Integration

C#

15 / 83

// Fixed time step and constant force.const float dt = 1;const float force = 10.0f;

// Create new body without initial velocity.var body = new Body

Mass = 1.0f,Position = 0.0f,Velocity = 0.0f

;

// Simulate ten steps.for (float t = 1; t <= 10; t++)

body.Position += body.Velocity * dt;var acceleration = force / body.Mass;body.Velocity += acceleration * dt;

Page 16: Game Programming 11 - Game Physics

Explicit Euler Integration

t position velocity

1 0 10

2 10 20

3 30 30

4 60 40

5 100 50

6 150 60

7 210 70

8 280 80

9 360 90

10 450 100

16 / 83

Explicit Euler integration with dt = 1

Page 17: Game Programming 11 - Game Physics

Inaccuracy

𝒙 = 𝟎. 𝟓𝒂𝒕𝟐 + 𝒗𝒕 + 𝒙𝟎 with 𝒂 = 𝟏𝟎, 𝒕 = 𝟏𝟎, 𝒗 = 𝟎, 𝒙𝟎 =𝟎

= 0.5 × 10 × 102 + 0𝑡+ 0

= 0.5 × 10 × 100

= 500

17 / 83

Exact physical position at t = 10 is:

This implies an error of (500 – 450) / 500 = 10% after only ten seconds for dt = 1!

Page 18: Game Programming 11 - Game Physics

Explicit Euler Integration

t position velocity

1 4.5 10

2 19 20

3 43.5 30

4 78 40

5 122.5 50

6 177 60

7 241.5 70

8 316 80

9 400.5 90

10 495 100

18 / 83

Explicit Euler integration with dt = 0.1

Page 19: Game Programming 11 - Game Physics

Variable vs. fixed time steps

Usually, we’re working with variable time steps in game simulations:

However, this approach has major drawbacks in when simulating

physics.

19 / 83

public void Update(float deltaTime)

// Do something awesome here...

Page 20: Game Programming 11 - Game Physics

Variable time steps in physics

• Physics will “feel” slightly different depending on your framerate

• Fast objects won’t collide as expected

• Spring simulation will explode to infinity

20 / 83

Page 21: Game Programming 11 - Game Physics

Fixed time steps in physics

• In order to ensure a fixed time step that feels right, we need to have

the physics simulation …

Don’t update too often if frames are rendered very fast

Catch up if frames are rendered very slowly

• This is achieved by accumulating deltas across frames, updating

several times per frame if necessary.

21 / 83

Page 22: Game Programming 11 - Game Physics

Fixed time steps in physics

C#

22 / 83

var random = new Random();

// Fixed time step and constant force.const float fixedDt = 1f / 60f;const float force = 10.0f;float totalTime = 0.0f;float accumulatedDt = 0.0f;

// Create new body without initial velocity.var body = new Body Mass = 1.0f, Position = 0.0f, Velocity = 0.0f ;

// Simulate ten steps.for (int t = 0; t <= 10; t++)

// Random delta.float dt = (float)random.NextDouble() / 45;

totalTime += dt;accumulatedDt += dt;

while (accumulatedDt > fixedDt)

var acceleration = force / body.Mass;body.Velocity += acceleration * fixedDt;body.Position += body.Velocity * fixedDt;

accumulatedDt -= fixedDt;

Page 23: Game Programming 11 - Game Physics

Fixed time steps in physics

t dt accumulatedTime position velocity

0 0.022 0.022 0 0

0 0.022 0.005 0.003 0.167

1 0.020 0.026 0.003 0.167

1 0.020 0.009 0.008 0.333

2 0.005 0.014 0.008 0.333

3 0.003 0.017 0.008 0.333

3 0.003 0 0.017 0.500

4 0.011 0.011 0.017 0.500

5 0.019 0.030 0.017 0.500

5 0.019 0.013 0.028 0.667

23 / 83

Fixed time steps with dt = 1 / 60 = 0.016

Page 24: Game Programming 11 - Game Physics

Gotcha!

Accumulated time steps can cause an infinite loop if your physics simulation takes more time than your fixed time

step!

Clamp at a maximum number of simulation steps per frame to avoid this.

24 / 83

Page 25: Game Programming 11 - Game Physics

Rigid bodies

• All of the above assumes a constant mass concentrated in a single

point

• However, in games we have to deal with bodies having their mass

distributed over their area (or volume)

• Rigid bodies are shapes that don’t change or deform during physics

simulation

• We’ll focus on these for the time being

25 / 83

Page 26: Game Programming 11 - Game Physics

Rigid bodies

• For the time being, we’ll model our rigid body as a set of point

masses

• The total momentum of the rigid body equals the sum of all

momentums of all points that make up that body

𝑝𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦 =

𝑖

𝑚𝑖𝑣𝑖

26 / 83

Page 27: Game Programming 11 - Game Physics

Center of mass

We define the center of mass of a rigid body as the linear combination

of the position vectors of all points that make up that body, weighted by

their masses, divided by the total mass of the body.

𝑥𝐶𝑒𝑛𝑡𝑒𝑟𝑂𝑓𝑀𝑎𝑠𝑠 =σ𝑖 𝑥𝑖𝑚𝑖

𝑀𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦

27 / 83

Page 28: Game Programming 11 - Game Physics

Center of mass

Let’s modify this equation a bit:

28 / 12

𝑥𝐶𝑒𝑛𝑡𝑒𝑟𝑂𝑓𝑀𝑎𝑠𝑠 = σ𝑖 𝑥𝑖𝑚𝑖

𝑀𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦

𝑀𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦𝑥𝐶𝑒𝑛𝑡𝑒𝑟𝑂𝑓𝑀𝑎𝑠𝑠 =

𝑖

𝑥𝑖𝑚𝑖multiplied with

𝑀𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦

𝑑(𝑀𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦𝑥𝐶𝑒𝑛𝑡𝑒𝑟𝑂𝑓𝑀𝑎𝑠𝑠)

𝑑𝑡

=

𝑖

𝑑(𝑥𝑖𝑚𝑖)

𝑑𝑡

𝑑/𝑑𝑡

=

𝑖

𝑚𝑖

𝑑𝑥𝑖𝑑𝑡

constant mass

=

𝑖

𝑚𝑖𝑣𝑖definition velocity

= 𝑝𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦 definition momentum

Page 29: Game Programming 11 - Game Physics

Center of mass

Now, let’s take a look at the second part again:

29 / 83

𝑑(𝑀𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦𝑥𝐶𝑒𝑛𝑡𝑒𝑟𝑂𝑓𝑀𝑎𝑠𝑠)

𝑑𝑡

=𝑀𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦

𝑑𝑥𝐶𝑒𝑛𝑡𝑒𝑟𝑂𝑓𝑀𝑎𝑠𝑠𝑑𝑡

constantmass

= 𝑀𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦𝑣𝐶𝑒𝑛𝑡𝑒𝑟𝑂𝑓𝑀𝑎𝑠𝑠 definition

velocity

Page 30: Game Programming 11 - Game Physics

Center of mass

Combining both results yields a stunning property

of the center of mass!

𝒑𝑹𝒊𝒈𝒊𝒅𝒃𝒐𝒅𝒚 = 𝑴𝑹𝒊𝒈𝒊𝒅𝒃𝒐𝒅𝒚𝒗𝑪𝒆𝒏𝒕𝒆𝒓𝑶𝒇𝑴𝒂𝒔𝒔

30 / 83

Page 31: Game Programming 11 - Game Physics

Center of mass

Combining both results yields a stunning property

of the center of mass!

𝒑𝑹𝒊𝒈𝒊𝒅𝒃𝒐𝒅𝒚 = 𝑴𝑹𝒊𝒈𝒊𝒅𝒃𝒐𝒅𝒚𝒗𝑪𝒆𝒏𝒕𝒆𝒓𝑶𝒇𝑴𝒂𝒔𝒔

For finding the momentums of any rigid body, we

can treat that body as single point mass and

velocity.

31 / 83

Page 32: Game Programming 11 - Game Physics

Center of mass

This further applies to forces, as well:

32 / 83

𝐹𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦 = 𝑑𝑝𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦

𝑑𝑡

= 𝑑(𝑀𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦𝑣𝐶𝑒𝑛𝑡𝑒𝑟𝑂𝑓𝑀𝑎𝑠𝑠)

𝑑𝑡

as we’ve just proven

=𝑀𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦

𝑑𝑣𝐶𝑒𝑛𝑡𝑒𝑟𝑂𝑓𝑀𝑎𝑠𝑠𝑑𝑡

constant mass

= 𝑀𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦𝑎𝐶𝑒𝑛𝑡𝑒𝑟𝑂𝑓𝑀𝑎𝑠𝑠 definition acceleration

Page 33: Game Programming 11 - Game Physics

Center of mass

This further applies to forces, as well:

We can treat all forces acting our rigid body as if

their sum is acting on a point at the center of mass

with the mass of the entire body.33 / 83

𝐹𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦 = 𝑑𝑝𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦

𝑑𝑡

= 𝑑(𝑀𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦𝑣𝐶𝑒𝑛𝑡𝑒𝑟𝑂𝑓𝑀𝑎𝑠𝑠)

𝑑𝑡

as we’ve just proven

=𝑀𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦

𝑑𝑣𝐶𝑒𝑛𝑡𝑒𝑟𝑂𝑓𝑀𝑎𝑠𝑠𝑑𝑡

constant mass

= 𝑀𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦𝑎𝐶𝑒𝑛𝑡𝑒𝑟𝑂𝑓𝑀𝑎𝑠𝑠 definition acceleration

Page 34: Game Programming 11 - Game Physics

Rotation

• So far, we’ve been talking all about linear momentum and linear

acceleration.

• Now, we want to figure out how forces applied to our rigid bodies

make them rotate.

• Where these forces are applied to the body will play an important

role.

34 / 83

Page 35: Game Programming 11 - Game Physics

Kinematics – Orientation

The orientation Ω of an object is the angular difference between the

world coordinate system and a coordinate system fixed in that object, in

radians.

35 / 83

Page 36: Game Programming 11 - Game Physics

Kinematics – Angular Velocity

Angular Velocity is the rate of change of orientation over time.

36 / 83

𝝎 =𝒅Ω

𝒅𝒕

Page 37: Game Programming 11 - Game Physics

Kinematics – Angular Acceleration

Angular Acceleration is the rate of change of angular velocity over time.

37 / 83

𝜶 =𝒅𝜔

𝒅𝒕

Page 38: Game Programming 11 - Game Physics

Linear Velocity from Angular Velocity

• We often need to find the velocity of an arbitrary point on our object

i.e. velocity of colliding points to compute how hard they hit each

other

• Without rotation, the velocity of any point in the body is the same

Velocity of the center of mass

• With rotation, every point might have a different velocity

Obviously, we can’t keep track of the velocity of each of the

infinity of points

38 / 83

Page 39: Game Programming 11 - Game Physics

Linear Velocity from Angular Velocity

Claim:

The linear velocity of any point P inside an object that is rotating about

its origin O, but not translating, is given by the following equation:

39 / 83

𝒗𝑷 = 𝝎𝑶𝑷⊥

Page 40: Game Programming 11 - Game Physics

Hint

In 2D, the perpendicular of a vector (x, y) is (-y, x).

40 / 83

Page 41: Game Programming 11 - Game Physics

Linear Velocity from Angular Velocity - Proof

𝝎𝑶𝑷⊥ has the correct magnitude, because

and Ω 𝑶𝑷 is the length P is moving when moving Ω

radians along the arc whose radius vector is 𝑶𝑷 , by definition of radians.

41 / 83

𝜔𝑂𝑃⊥ = 𝜔 𝑂𝑃⊥

= 𝜔 𝑂𝑃 perpendicularydoesn’t change

length

= 𝑑Ω

𝑑𝑡𝑂𝑃

definition 𝜔

= 𝑑(Ω 𝑂𝑃 )

𝑑𝑡

𝑂𝑃 is constant

Page 42: Game Programming 11 - Game Physics

Linear Velocity from Angular Velocity - Proof

𝝎𝑶𝑷⊥ has the correct direction, because a point

rotating around another fixed point can only move

perpendicularly to the vector between the points,

or the movement wouldn’t be a simple rotation.

𝝎𝑶𝑷⊥ has the correct sign, because we’re

measuring Ω in the counterclockwise direction. ω

is positive when the point is rotating

counterclockwise. The perpendicular operator

points in the counterclockwise direction relative to

the radius vector.

42 / 83

Page 43: Game Programming 11 - Game Physics

Linear Velocity from Angular Velocity

The linear velocity of any point P inside an object that is rotating about

its origin O, but not translating, is given by the following equation:

43 / 83

𝒗𝑷 = 𝝎𝑶𝑷⊥

Page 44: Game Programming 11 - Game Physics

Linear Velocity from Angular Velocity

The linear velocity of any point P inside an object that is rotating about

its origin O, and is translating, is given by the following equation:

44 / 83

?

Page 45: Game Programming 11 - Game Physics

Chasles’ Theorem

• Chasles’ Theorem breaks up motion into linear and angular

components.

• We consider any movement of our rigid body as

translating a single point in the body

rotating the rest of the body around that point

45 / 83

Page 46: Game Programming 11 - Game Physics

Chasles’ Theorem

The linear velocity of any point P inside a moving object that is rotating

about its origin O is given by the following equation:

(without proof)

46 / 83

𝒗𝑷 = 𝒗𝑶 + 𝝎𝑶𝑷⊥

Page 47: Game Programming 11 - Game Physics

Kinematics – Angular Momentum

The Angular Momentum of a point P tells us how much of the linear

momentum pP of P is rotating around the origin.

47 / 83

𝑳𝑶𝑷 = 𝑶𝑷⊥ × 𝒑𝑷

Page 48: Game Programming 11 - Game Physics

Kinematics – Angular Momentum

The Angular Momentum of a point P tells us how much of the linear

momentum pP of P is rotating around the origin.

Note how angular momentum of a point P needs a reference (here: O),

in contrast to linear momentum.

48 / 83

𝑳𝑶𝑷 = 𝑶𝑷⊥ × 𝒑𝑷

Page 49: Game Programming 11 - Game Physics

Dynamics – Torque

Torque is the rate of change of angular momentum over time.

49 / 83

𝝉𝑶𝑷 =𝒅𝑳𝑶𝑷𝒅𝒕

Page 50: Game Programming 11 - Game Physics

Dynamics – Torque

We can use the torque to determine how much of the force applied at

point P is causing the object to rotate:

50 / 83

𝜏𝑂𝑃 = dLOPdt

definition torque

= d(OP⊥× pP)

dt

definitionangular momentum

=OP⊥ ×

dpp

dt+

dOP⊥dt

× ppproduct rule

= (OP⊥ × 𝐹𝑃) + (vP × 𝑝P) def. linear force,def. linear velocity

= OP⊥ × 𝐹𝑃 velocity and momentum of P are parallel

Page 51: Game Programming 11 - Game Physics

Calculating Angular Momentum

Again, just like change in velocity can be numerically integrated using

acceleration, change in angular momentum can be integrated using

torque, from an applied force and position of application:

𝐿𝑂𝑃 𝑡 = න𝜏𝑂𝑃 𝑑𝑡 = නOP⊥ × 𝐹𝑃 𝑑𝑡

51 / 83

Page 52: Game Programming 11 - Game Physics

Moment of Inertia

• The moment of inertia I of an object is a measure of how hard it is to

rotate the object about its center of mass.

• It is the sum of the squared distances from the center of mass to

each other point in the body, scaling each squared distance by the

mass of the respective point.

𝐼 =

𝑖

𝑚𝑖Oi⊥2

52 / 83

Page 53: Game Programming 11 - Game Physics

Moment of Inertia

The moment of inertia can be used to derive the total angular

momentum:

𝐿 =

𝑖

Oi⊥ × pidefinition

angular momentum

=

𝑖

Oi⊥ × (𝑚𝑖𝑣𝑖)Definition linear momentum

=

𝑖

Oi⊥ × (𝑚𝑖𝜔𝑂𝑖⊥)Linear velocity from angular

velocity

=𝜔

𝑖

Oi⊥ × (𝑚𝑖𝑂𝑖⊥)Angular velocity same for all

points i

=𝜔

𝑖

𝑚𝑖Oi⊥2

= 𝜔𝐼 definition moment of intertia

Page 54: Game Programming 11 - Game Physics

Hint

As the moment of inertia is based on the mass and relative

position of all points of a rigid body, only, it is constant and has

to be computed only once!

54 / 83

Page 55: Game Programming 11 - Game Physics

Dynamics – Torque

Integration shows how torque and angular acceleration are related:

𝜏 = 𝑑𝐿

𝑑𝑡definition torque

= 𝑑𝜔𝐼

𝑑𝑡

as we’ve just proven

=𝐼𝑑𝜔

𝑑𝑡

moment of inertia constant

= 𝐼𝛼 Definitionangular acceleration

55 / 83

Page 56: Game Programming 11 - Game Physics

Dynamics – Torque

Thus, knowing the torque on our body, we can compute angular

acceleration, and then find angular velocity and orientation by numeric

integration.

𝝉 = 𝑰𝜶

56 / 83

Page 57: Game Programming 11 - Game Physics

Full Physics Simulation – Setup

For each rigid body:

1. Calculate center of mass and moment of inertia at the center of

mass.

2. Set initial position, orientation, linear velocity and angular velocity.

57 / 83

Page 58: Game Programming 11 - Game Physics

Full Physics Simulation – Loop

For each rigid body:

1. Collect all forces on the body, including their points of application.

2. Sum all forces and compute linear acceleration.

3. Compute the torque caused by each force.

4. Sum all torque and compute angular acceleration.

5. Numerically integrate linear acceleration and angular acceleration to

update linear velocity and angular velocity, and position and

orientation.

58 / 83

Page 59: Game Programming 11 - Game Physics

Hint

Usually, games will treat both mass and moment of inertia as

properties of the rigid body.

59 / 83

Page 60: Game Programming 11 - Game Physics

Collision Response

• Given we know that there is a collision, the task is to find out how to

handle that collision.

• We need to decide where the colliding objects move, and if and how

they start spinning.

• By now, velocities never changes instantly, but by means forces

applied over time, only.

60 / 83

Page 61: Game Programming 11 - Game Physics

Impulse

• An impulse changes the momentum (and thus, the velocity) of a

rigid body instantly, without the need of integration over time.

• We’re going to use Newton’s Law of Restitution for Instantaneous

Collisions with No Friction to find the impulses to apply in case of a

collision.

61 / 83

Page 62: Game Programming 11 - Game Physics

Newton’s Law of Restitution for Instantaneous Collisions with No

Friction

• Instantaneous: in no time

• Restitution: coefficient of restitution models the compression and

restitution of impacting bodies with a single scalar

• No friction: impulse is entirely pointed in the direction of the collision

normal

62 / 83

Page 63: Game Programming 11 - Game Physics

Collision Data

• Collision point P

• Center of mass of both bodies A, B

• Velocity of the collision point of both bodies 𝑣𝐴, 𝑣𝐵• Collision normal n

63 / 83

Page 64: Game Programming 11 - Game Physics

Derived Collision Data

Relative velocity

𝑣𝐴𝐵 = 𝑣𝐴 − 𝑣𝐵

Relative normal velocity

𝑣𝐴𝐵𝑛 = 𝑣𝐴 − 𝑣𝐵 𝑛

64 / 83

Page 65: Game Programming 11 - Game Physics

Coefficient of Restitution

• The Coefficient of Restitution 𝜖 tells us how much of the incoming

energy is dissipated during the collision.

• 𝜖 = 1 yields a totally elastic collision (super ball)

• 𝜖 = 0 yields a totally plastic collision (all energy absorbed)

𝑣𝐴𝐵′ 𝑛 = −𝜖𝑣𝐴𝐵𝑛

65 / 83

Page 66: Game Programming 11 - Game Physics

Collision Impulse

By understanding the collision impulse j as change of momentum, we

expect the resulting velocities 𝑣𝐴′ and 𝑣𝐵

′ to yield the following:

𝑣𝐴′ = 𝑣𝐴 +

𝑗

𝑀𝐴𝑛

𝑣𝐵′ = 𝑣𝐵 −

𝑗

𝑀𝐵𝑛

66 / 83

Page 67: Game Programming 11 - Game Physics

Finding the Collision Impulse

Now that we’ve got everything in place, we can finally compute the

collision impulse.

67 / 83

−𝜖𝑣𝐴𝐵𝑛 = 𝑣𝐴𝐵′ 𝑛 definition

coefficient of restitution

= (𝑣𝐴′ − 𝑣𝐵

′ )𝑛 definitionrelative velocity

=(𝑣𝐴 +

𝑗

𝑀𝐴𝑛 − 𝑣𝐵 +

𝑗

𝑀𝐵𝑛)𝑛

definitioncollision impulse

=𝑣𝐴𝑛 +

𝑗

𝑀𝐴𝑛𝑛 − 𝑣𝐵𝑛 +

𝑗

𝑀𝐵𝑛𝑛

distribution

=𝑣𝐴𝑛 − 𝑣𝐵𝑛 +

𝑗

𝑀𝐴𝑛𝑛 +

𝑗

𝑀𝐵𝑛𝑛

commutative property

=𝑣𝐴𝐵𝑛 +

𝑗

𝑀𝐴𝑛𝑛 +

𝑗

𝑀𝐵𝑛𝑛

definitionrelative velocity

Page 68: Game Programming 11 - Game Physics

Finding the Collision Impulse

Now that we’ve got everything in place, we can finally compute the

collision impulse.

68 / 83

𝑣𝐴𝐵𝑛 +𝑗

𝑀𝐴𝑛𝑛 +

𝑗

𝑀𝐵𝑛𝑛

= −𝜖𝑣𝐴𝐵𝑛

𝑗

𝑀𝐴𝑛𝑛 +

𝑗

𝑀𝐵𝑛𝑛

= −𝜖𝑣𝐴𝐵𝑛 − 𝑣𝐴𝐵𝑛

𝑗(1

𝑀𝐴𝑛𝑛 +

1

𝑀𝐵𝑛𝑛) = −𝜖𝑣𝐴𝐵𝑛 − 𝑣𝐴𝐵𝑛 distribution

𝑗 = −𝜖𝑣𝐴𝐵𝑛 − 𝑣𝐴𝐵𝑛1𝑀𝐴

𝑛𝑛 +1𝑀𝐵

𝑛𝑛

Page 69: Game Programming 11 - Game Physics

Finding the Collision Impulse

Now that we’ve got everything in place, we can finally compute the

collision impulse.

69 / 83

𝑗 = −𝜖𝑣𝐴𝐵𝑛 − 𝑣𝐴𝐵𝑛1𝑀𝐴

𝑛𝑛 +1𝑀𝐵

𝑛𝑛

= −𝑣𝐴𝐵𝑛(1 + ϵ)

1𝑀𝐴

𝑛𝑛 +1𝑀𝐵

𝑛𝑛

distribution

= −𝑣𝐴𝐵𝑛(1 + ϵ)

𝑛𝑛(1𝑀𝐴

+1𝑀𝐵

)

distribution

Page 70: Game Programming 11 - Game Physics

Finding the Collision Impulse

Now that we’ve got everything in place, we can finally compute the

collision impulse.

70 / 83

𝑗 = −𝜖𝑣𝐴𝐵𝑛 − 𝑣𝐴𝐵𝑛1𝑀𝐴

𝑛𝑛 +1𝑀𝐵

𝑛𝑛

= −𝑣𝐴𝐵𝑛(1 + ϵ)

1𝑀𝐴

𝑛𝑛 +1𝑀𝐵

𝑛𝑛

distribution

= −𝑣𝐴𝐵𝑛(1 + ϵ)

𝑛𝑛(1𝑀𝐴

+1𝑀𝐵

)

distribution

Plugging in j back into our collision impulse equation yields the new velocities of A and B!

Page 71: Game Programming 11 - Game Physics

Collision Impulse II

Finally, we need to understand how to have the collision impulse j

change the angular momentum:

𝜔𝐴′ = 𝜔𝐴 +

𝐴𝑃⊥𝑗𝑛

𝐼𝐴

𝜔𝐵′ = 𝜔𝐵 −

𝐵𝑃⊥𝑗𝑛

𝐼𝐵

71 / 83

Page 72: Game Programming 11 - Game Physics

Finding the Collision Impulse

Let’s close by computing the collision impulse with spin:

72 / 12

−𝜖𝑣𝐴𝐵𝑛 = 𝑣𝐴𝐵′ 𝑛 definition

coefficient of restitution

= (𝑣𝐴𝑃′ − 𝑣𝐵𝑃

′ )𝑛 definitionrelative velocity

= (𝑣𝐴′ + 𝜔𝐴

′ 𝐴𝑃⊥ − (𝑣𝐵′ + 𝜔𝐵

′ 𝐵𝑃⊥))𝑛 Chasles’ Theorem

= (𝑣𝐴′ + 𝜔𝐴

′ 𝐴𝑃⊥ − 𝑣𝐵′ − 𝜔𝐵

′ 𝐵𝑃⊥)𝑛

=(𝑣𝐴 +

𝑗

𝑀𝐴𝑛 + (𝜔𝐴 +

𝐴𝑃⊥𝑗𝑛

𝐼𝐴)𝐴𝑃⊥ − (𝑣𝐵 −

𝑗

𝑀𝐵𝑛) − (𝜔𝐵 −

𝐵𝑃⊥𝑗𝑛

𝐼𝐵)𝐵𝑃⊥)𝑛

definitioncollision impulse

=(𝑣𝐴 +

𝑗

𝑀𝐴𝑛 + 𝜔𝐴𝐴𝑃⊥ +

𝐴𝑃⊥2𝑗𝑛

𝐼𝐴− 𝑣𝐵 +

𝑗

𝑀𝐵𝑛 − 𝜔𝐵𝐵𝑃⊥ +

𝐵𝑃⊥2𝑗𝑛

𝐼𝐵)𝑛

=𝑣𝐴𝑛 +

𝑗

𝑀𝐴𝑛𝑛 + 𝜔𝐴𝐴𝑃⊥𝑛 +

𝐴𝑃⊥2𝑗𝑛𝑛

𝐼𝐴− 𝑣𝐵𝑛 +

𝑗

𝑀𝐵𝑛𝑛 − 𝜔𝐵𝐵𝑃⊥𝑛

+𝐵𝑃⊥

2𝑗𝑛𝑛

𝐼𝐵

distribution

Page 73: Game Programming 11 - Game Physics

Finding the Collision Impulse

Let’s close by computing the collision impulse with spin:

73 / 12

𝑗

𝑀𝐴𝑛𝑛 +

𝐴𝑃⊥2𝑗𝑛𝑛

𝐼𝐴+

𝑗

𝑀𝐵𝑛𝑛 +

𝐵𝑃⊥2𝑗𝑛𝑛

𝐼𝐵

= −𝑣𝐴𝑛 − 𝜔𝐴𝐴𝑃⊥𝑛 + 𝑣𝐵𝑛 + 𝜔𝐵𝐵𝑃⊥𝑛− 𝜖𝑣𝐴𝐵𝑛

j to left side,non-j to right

side

𝑗𝑛𝑛(1

𝑀𝐴+𝐴𝑃⊥

2

𝐼𝐴+

1

𝑀𝐵+𝐵𝑃⊥

2

𝐼𝐵)

= −𝑛(𝑣𝐴 +𝜔𝐴𝐴𝑃⊥ − 𝑣𝐵 −𝜔𝐵𝐵𝑃⊥ + 𝜖𝑣𝐴𝐵) distribution

𝑗𝑛𝑛(1

𝑀𝐴+𝐴𝑃⊥

2

𝐼𝐴+

1

𝑀𝐵+𝐵𝑃⊥

2

𝐼𝐵)

= −𝑛(𝑣𝐴𝑃 − 𝑣𝐵𝑃 + 𝜖𝑣𝐴𝐵) Chasles’ Theorem

𝑗𝑛𝑛(1

𝑀𝐴+𝐴𝑃⊥

2

𝐼𝐴+

1

𝑀𝐵+𝐵𝑃⊥

2

𝐼𝐵)

= −𝑛(𝑣𝐴𝐵 + 𝜖𝑣𝐴𝐵) definitionrelative velocity

𝑗𝑛𝑛(1

𝑀𝐴+𝐴𝑃⊥

2

𝐼𝐴+

1

𝑀𝐵+𝐵𝑃⊥

2

𝐼𝐵)

= −𝑛𝑣𝐴𝐵(1 + 𝜖) distribution

𝑗 = −𝑛𝑣𝐴𝐵(1 + 𝜖)

𝑛𝑛(1𝑀𝐴

+𝐴𝑃⊥

2

𝐼𝐴+

1𝑀𝐵

+𝐵𝑃⊥

2

𝐼𝐵)

Page 74: Game Programming 11 - Game Physics

Collision Detection

• Now that we know how to handle collisions, all that’s left is to

understand how to detect them

• As detecting intersections between arbitrary polygons is quite

expensive, they are usually wrapped by some kind of collision shape

• With these shapes, typical test like shape-shape intersection and

ray-shape intersection become far cheaper

• The quality of the collision detection depends on how good the

shapes fit the actual body

74 / 83

Page 75: Game Programming 11 - Game Physics

Collision Spheres

Detecting whether two spheres A and B intersect is as easy as

comparing their distance to the sum of their radii.

75 / 83

Page 76: Game Programming 11 - Game Physics

Collision Spheres

The potential collision point lies on the ray from sphere A to B at the

exact radius of A.

76 / 83

Page 77: Game Programming 11 - Game Physics

Collision Spheres

Detecting whether a ray intersects a sphere requires some tedious, but

basic math (see References)

77 / 83

Page 78: Game Programming 11 - Game Physics

Axis-Aligned Bounding Boxes

Detecting whether two axis-aligned bounding boxes 𝐴𝑚𝑖𝑛, 𝐴𝑚𝑎𝑥 and

(𝐵𝑚𝑖𝑛, 𝐵𝑚𝑎𝑥) intersect can be easily checked using the separating axis

theorem:

78 / 83

Page 79: Game Programming 11 - Game Physics

Axis-Aligned Bounding Boxes

Detecting whether two axis-aligned bounding boxes 𝐴𝑚𝑖𝑛, 𝐴𝑚𝑎𝑥 and

(𝐵𝑚𝑖𝑛, 𝐵𝑚𝑎𝑥) intersect can be easily checked using the separating axis

theorem:

𝐴𝑚𝑖𝑛𝑥 > 𝐵𝑚𝑎𝑥𝑥 or 𝐵𝑚𝑖𝑛𝑥 > 𝐴𝑚𝑎𝑥𝑥 or

𝐴𝑚𝑖𝑛𝑦 > 𝐵𝑚𝑎𝑥𝑦 or 𝐵𝑚𝑖𝑛𝑦 > 𝐴𝑚𝑎𝑥𝑦 or

𝐴𝑚𝑖𝑛𝑧> 𝐵𝑚𝑎𝑥𝑧

or 𝐵𝑚𝑖𝑛𝑧> 𝐴𝑚𝑎𝑥𝑧

79 / 83

Page 80: Game Programming 11 - Game Physics

Tunneling

• If your objects move too fast, you run in danger of missing collisions

due to your numerical integration step size.

• Imagine a sphere moving fast towards a thin wall.

80 / 83

Page 81: Game Programming 11 - Game Physics

Tunneling – Possible Solutions

• Make the wall thicker.

Need to instruct all level designers.

• Impose an upper bound on object speed.

• Find the speculative contact through the bounding box of the

previous position of the moving object and the current one, and sub-

step from the contact point

Arbitrary convex polygons are a challenge (see Continuous

Collision by Erin Catto in References)

81 / 83

Page 82: Game Programming 11 - Game Physics

Octrees

• Checking every pair of objects can be very expensive

• The number of required checks can be reduced by subdividing the

space into smaller parts, and checking only pairs of objects who are

found within the same part

82 / 83Image by Bill Jacobs

Page 83: Game Programming 11 - Game Physics

Octree Construction

• Start with an empty root node covering the entire world

• Whenever you add an object to the world, start at the root node and

traverse the tree, finding the node farthest from the root that fully

contains the object

• If the node has reached its maximum capacity now, subdivide it into

children

83 / 83

Page 84: Game Programming 11 - Game Physics

Collision Detection using Octrees

• Given an octree containing shapes, we only need to check all pairs

of shapes that are found within the same node

• Shaped in non-leaf nodes need to be checked against all shapes of

all child nodes, and their children

84 / 83

Page 85: Game Programming 11 - Game Physics

Optimizing Octrees

• If the average object size begins to exceed the node size, objects

will start to be put in parent nodes more often.

• Limiting the depth of the octree helps avoiding this issue.

• For the same reason, it might be necessary to merge nodes again if

objects have moved away.

85 / 83

Page 86: Game Programming 11 - Game Physics

Future Work

• 3D (Matrices & Quaternions)

• Joints

• Non-rigid bodies

• Detecting Arbitrary Collisions

86 / 83

Page 87: Game Programming 11 - Game Physics

References

• Fiedler. Game Physics – Integration Basics. http://gafferongames.com/game-physics/integration-basics/, 2006.

• Fielder. Game Physics – Fix Your Timestep! http://gafferongames.com/game-physics/fix-your-timestep/, 2006.

• Hecker. Physics, The Next Frontier. Game Developer Magazine, October/November 1996.

• Hecker. Physics, Part 2: Angular Effects. Game Developer Magazine, December 1996/January 1997.

• Hecker. Physics, Part 3: Collision Response. Game Developer Magazine, March 1997.

• Catto. Box2D User Manual. http://box2d.org/manual.pdf, 2007.

• Catto. Physics for Game Programmers – Continuous Collision. http://www.gdcvault.com/play/1018239/Physics-for-Game-Programmers-Continuous, 2013.

• Baraff. Physically Based Modelling – Rigid Body Simulation. http://www.pixar.com/companyinfo/research/pbm2001/pdf/notesg.pdf, 2001.

• Jacobs. OpenGL Tutorial – Collision Detection. http://www.videotutorialsrock.com/opengl_tutorial/collision_detection/text.php, 2014.

• Su. Ray-Sphere Intersection. http://www.cs.tufts.edu/~sarasu/courses/comp175-2009fa/pdf/comp175-15-ray-sphere.pdf, November 11, 2009.

87 / 83

Page 88: Game Programming 11 - Game Physics

Thank you!

http://www.npruehs.de

https://github.com/npruehs

@npruehs

[email protected]

Page 89: Game Programming 11 - Game Physics

10 Minute Review Session

• What’s the difference between kinematics and dynamics?

• What is velocity?

• What is acceleration?

• What is momentum?

• What is force?

• In your own words: How does Explicit Euler Integration work?

• Why are fixed time steps important in physics simulation?

• What is a rigid body?

Page 90: Game Programming 11 - Game Physics

10 Minute Review Session

• What is the center of mass?

• In your own words: Explain Chasles’ Theorem!

• What is torque?

• What is moment of inertia?

• What is an impulse?

• Which data is required for resolving collisions?

• Which collision shapes do you know?

• How can you prevent tunneling?

• How can you reduce the number of collisions to check for?