Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber,...

31
Technische Universit¨ at M ¨ unchen PSE Game Physics Session (1) Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1

Transcript of Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber,...

Page 1: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

PSE Game PhysicsSession (1)

Martin Schreiber, Kristof Unterweger

16.04.2012

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 1

Page 2: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

Outline

Mathematics and PhysicsPhysics and Mathematical ModelingMathematical Description: Ordinary Differential EquationsTime Discretisation: Finite Difference SchemesProperties of Finite Difference Schemes: Consistency, Stabilityand Convergence

C++Live demonstration in eclipseTemplates

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 2

Page 3: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

Physical systems - Our modelPhysics engine - What do we talk about here?

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 3

Page 4: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

Physical systems - Our modelPhysics engine - What do we talk about here?

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 3

Page 5: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

Physical systems - Our model

Physics engine - What do we talk about here?

• Starting point: Physical system with certain properties• Determine laws which hold for these properties

Our physical system:• Set of rigid bodies n, n ∈ {1, ...,N}, N ∈ N• Each body is described by

• its position ~x ∈ RD

• its velocity ~v ∈ RD

• its mass m ∈ R• its geometrical form (we first only consider spheres!)

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 4

Page 6: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

Property relations (1)

What do we know about the relations of these properties?

• Position ~x(t) changes according to the body’s velocity ~v(t) overtime t

• Velocity of a rigid body changes due to• collisions with other rigid bodies (more information in the

next lab)• external forces acting on the bodies, e.g. gravity

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 5

Page 7: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

Property relations (2)How do we put our knowledge about the properties into amathematical description?

taken from http://tiramisu.mirellacastellano.com/wp-content/uploads/2010/10/cartoon-car.giftaken from http://www.allmystery.de/dateien/uh60967,1292866473,obelix latein plakat klein 1.jpg

Velocity ⇔ change of position over time ⇔ d~xdt

Forces ⇔ change in velocity over time ⇔ d~vdt

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 6

Page 8: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

Conservation laws - The equations of motion

d~xdt = ~v

d~vdt = 1

m

(~Fcoll + ~Fext

) (1)

with external forces ~Fext , collisional forces ~Fcoll andinitial conditions ~x(t = 0) := ~x0, ~v(t = 0) := v0.

Remark: Within the lab, we use the equations of motion asmathematical model that we try to solve.The difference between the “real-world solution” andthe mathematical solution of our model is called model error.

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 7

Page 9: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

Mathematical description - BasicsHow do we solve Eq.(1)?

→ A little bit of formalism...• Definition:

dfdt := lim

dt→0

f (t+dt)−f (t)dt is called (time) derivative of a function f : R→ R.

Analogously, the derivative for a vector-type function can be defined asthe derivatives of each vector component.

• Definition:An equation of the type F (t , df

dt ,d2fdt2 , ...,

d l fdt l ) = 0 is called ordinary

differential equation of order l .• Definition:

A system of equations of the form

Fk (t ,df1dt

, ...,dfNdt

, ....,d l f1dt l , ...,

d l fNdt l ) = 0, k = 1, ...,K

with functions f1(t), ..., fN(t) is called a system of ordinary differentialequations.

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 8

Page 10: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

Let’s consider our mathematical model...

d~xdt = ~v

d~vdt = 1

m

(~Fcoll + ~Fext

) (1)

How can we describe our Eq.(1) in terms of our definitions?

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 9

Page 11: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

Let’s consider our mathematical model...

Answer: We need to deal with a system of ordinarydifferential equations of order 1, consistingof 2 · D · N equations.

Which general approaches are there to solve the system?• Analytically→ Not always possible (many rigid bodies, complex

geometries, complex forces); available for simple problems• Numerically→ Can handle many rigid bodies, complex

geometries, complex forces; in most cases: cannot solve theproblem exactly, but up to a certain accuracy

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 10

Page 12: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

Towards the numerical solution:The three steps of discretisation

• Step 1:Instead of solving the problem on a certain time interval [0, tend ],we try to solve it only at discrete points in timet0 := 0 < t1 < t2 < ... < tT := tend .In our case, ti := t0 + i ·∆t , i = 0, ...,T , ∆t := tT−t0

T timestep• Step 2:

We try to find discrete approximate solutions ~xh(t), ~vh(t) that are“close” to the exact solutions ~x(t), ~v(t).

• Step 3:We try to find a discrete form for our differential operators fromEq.(1).

Remark: The error between the exact solution of our mathematicalmodel and the solution of our discretised problem is calleddiscretisation error.

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 11

Page 13: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

Towards the numerical solution:The three steps of discretisation

How can we approximate the differential expressions from Eq.(1)?

Let’s consider the position vector ~x(t) and define xd (t) := d-thcomponent of vector ~x(t).

Assume that our position evolution is sufficiently “smooth”, i.e. asoften differentiable as we need it to be :-)

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 12

Page 14: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

Towards the numerical solution:Finite differences (step 3)

Then, use Taylor expansion for xd (t + ∆t):

xd (t + ∆t) = xd (t) + dxd

dt ∆t + O(∆t2)⇔

dxd

dt = xd (t+∆t)−xd (t)∆t + O(∆t)

(2)

So, we can approximate xd (t + ∆t) from xd (t) as follows:

xd (t + ∆t) = xd (t) + ∆t · vd (t)

Explicit Euler approximation(3)

Remark: Expressions of the form xd (t+∆t)−xd (t)∆t are called

finite differences.

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 13

Page 15: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

Towards the numerical solution:Finite difference schemes

Approximation Scheme Error

xd (t+∆t)−xd (t)∆t = vd (t) Explicit Euler O (∆t)

xd (t+∆t)−xd (t)∆t = vd (t + ∆t) Implicit Euler O (∆t)

xd (t+∆t)−xd (t−∆t)2∆t = vd (t) Central difference O

(∆t2

)

Can you think of advantages/ disadvantages of these schemes?

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 14

Page 16: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

Questions revisited

How “close” are our approximate solutions to the continuous solutionof the mathematical model?

How big should we choose the timestep?

Does our discrete solution approach the continuous solution when wedecrease the timestep?

What happens to numerical errors, that is errors introduced by e.g.the limited computational precision of our computers?

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 15

Page 17: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

ConsistencyDefinition:A numerical method (for a differential equation) is called consistent,if the solution x∆t (t) according to our solving algorithm approachesthe exact solution x(t) of the mathematical model for ∆t → 0 in asingle timestep, i.e.

lim∆t→0

x∆t (t) = x(t) (4)

taken from http://nl.toonpool.com/user/589/files/the square 556785.jpg

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 16

Page 18: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

ConsistencyDefinition:A numerical method (for a differential equation) is called consistent,if the solution x∆t (t) according to our solving algorithm approachesthe exact solution x(t) of the mathematical model for ∆t → 0 in asingle timestep, i.e.

lim∆t→0

x∆t (t) = x(t) (4)

taken from http://nl.toonpool.com/user/589/files/the square 556785.jpg

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 16

Page 19: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

Consistency: Error estimate

The consistency of our Euler scheme is...

wait for it...

trivial :-), as long as we stick to x(t) ∈ C2:

‖x∆t (t + ∆t)− x(t + ∆t)‖ ≤ C2

maxt

∥∥∥∥d2x(t)dt2

∥∥∥∥dt2

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 17

Page 20: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

Consistency: Error estimate

The consistency of our Euler scheme is...

wait for it...

trivial :-), as long as we stick to x(t) ∈ C2:

‖x∆t (t + ∆t)− x(t + ∆t)‖ ≤ C2

maxt

∥∥∥∥d2x(t)dt2

∥∥∥∥dt2

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 17

Page 21: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

Consistency: Error estimate

The consistency of our Euler scheme is...

wait for it...

trivial :-), as long as we stick to x(t) ∈ C2:

‖x∆t (t + ∆t)− x(t + ∆t)‖ ≤ C2

maxt

∥∥∥∥d2x(t)dt2

∥∥∥∥dt2

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 17

Page 22: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

Stability

Definition:A numerical method is called stable, if numerical errors do notincrease in subsequent timesteps.

The stability of our scheme is...

wait for it...

...here it comes...not that trivial at all :-(

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 18

Page 23: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

Stability

Definition:A numerical method is called stable, if numerical errors do notincrease in subsequent timesteps.

The stability of our scheme is...

wait for it...

...here it comes...not that trivial at all :-(

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 18

Page 24: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

Stability

Definition:A numerical method is called stable, if numerical errors do notincrease in subsequent timesteps.

The stability of our scheme is...

wait for it...

...here it comes...

not that trivial at all :-(

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 18

Page 25: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

Stability

Definition:A numerical method is called stable, if numerical errors do notincrease in subsequent timesteps.

The stability of our scheme is...

wait for it...

...here it comes...not that trivial at all :-(

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 18

Page 26: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

Stability: Remarks

• Purely implicit discretisation schemestypically are unconditionally stable

• Explicit schemes are only stable in a certaindiscretisation range. For time discretisationschemes, the timestep ∆t often needs to belimited:

∆t < C(Problem) taken from http://www.allmystery.de/dateien/

uh60967,1292866473,obelix latein plakat klein 1.jpg

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 19

Page 27: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

Convergence

Definition:A numerical method is called convergent, if our numerical methodyields an approximate (global) solution of the continuous problem.

• Consistency, Stability and Convergence are closely related• For linear initial value problems, it holds:

Consistency + Stability⇔ Convergence

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 20

Page 28: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

Questions re-revisited

How “close” are our approximate solutions to the continuous solutionof the mathematical model?

How big should we choose the timestep?

Does our discrete solution approach the continuous solution when wedecrease the timestep?

What happens to numerical errors, that is errors introduced by e.g.the limited computational precision of our computers?

Talking about errors: Which errors do you remember?

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 21

Page 29: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

Outline

Mathematics and PhysicsPhysics and Mathematical ModelingMathematical Description: Ordinary Differential EquationsTime Discretisation: Finite Difference SchemesProperties of Finite Difference Schemes: Consistency, Stabilityand Convergence

C++Live demonstration in eclipseTemplates

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 22

Page 30: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

C++ introduction

Live demonstration in eclipse

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 23

Page 31: Martin Schreiber, Kristof Unterweger...Martin Schreiber, Kristof Unterweger 16.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (1), 16.04.2012 1 Technische Universit¨at

Technische Universitat Munchen

Templates

Generics in Java• Specialized during runtime• ...

Templates in C++• Specialized during compilation time• Advantage: Compiler can run optimizations• Use inlining as much as possible• ...

M. Schreiber, K. Unterweger: PSE Game Physics

Session (1), 16.04.2012 24