PSE Game Physics - Technische Universität München · Technische Universit¨at Munc¨ hen PSE Game...
Transcript of PSE Game Physics - Technische Universität München · Technische Universit¨at Munc¨ hen PSE Game...
Technische Universitat Munchen
PSE Game PhysicsSession (1)
Oliver Meister, Sebastian Rettenberger, Roland Wittmann
16.04.2013
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 1
Technische Universitat Munchen
Outline
Organization
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
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 2
Technische Universitat Munchen
Organization
• Teams: Up to 3 students• 7 worksheets + project phase• Deadline for worksheets: Monday, 12 p.m. before next meeting
Send your solutions to [email protected]
• Small presentations of your work during meetings (no exams)• Tuesday, 15:30 - 17:30
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 3
Technische Universitat Munchen
Outline
Organization
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
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 4
Technische Universitat Munchen
Physical systems - Our modelPhysics engine - What do we talk about here?
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 5
Technische Universitat Munchen
Physical systems - Our modelPhysics engine - What do we talk about here?
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 5
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!)
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 6
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
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 7
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
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 8
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.
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 9
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.
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 10
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?
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 11
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
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 12
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.
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 13
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 :-)
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 14
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.
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 15
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?
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 16
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?
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 17
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
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 18
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
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 18
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
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 19
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
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 19
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
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 19
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 :-(
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 20
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 :-(
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 20
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 :-(
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 20
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 :-(
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 20
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
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 21
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
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 22
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?
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 23
Technische Universitat Munchen
Outline
Organization
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
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 24
Technische Universitat Munchen
C++ introduction
Live demonstration in eclipse
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 25
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• ...
O. Meister, S. Rettenberger, R. Wittmann: PSE Game Physics
Session (1), 16.04.2013 26