Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit...
Transcript of Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit...
![Page 1: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/1.jpg)
Erin Catto
Blizzard Entertainment
Numerical Integration
![Page 2: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/2.jpg)
Basic Idea
⚫ Games use differential equations for
physics.
⚫ These equations are hard to solve
exactly.
⚫ We can use numerical integration to
solve them approximately.
![Page 3: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/3.jpg)
Overview
⚫ Differential Equations
⚫ Numerical Integrators
⚫ Demos
![Page 4: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/4.jpg)
Typical Game Loop
Start t = 0
Player Input
Simulate Dt
Render
t = t + Dt
Choose Dt
![Page 5: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/5.jpg)
Simulation
⚫ Animation
⚫ AI
⚫ Physics
⚫ Differential Equations
![Page 6: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/6.jpg)
What is a differential
equation?
⚫ An equation involving derivatives.
rate of change of a variable = a function
![Page 7: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/7.jpg)
Anatomy of Differential
Equations
⚫ State
⚫ Dependent variables
⚫ Independent variables
⚫ Initial Conditions
⚫ Model
⚫ The differential equation itself
![Page 8: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/8.jpg)
Projectile Motion
⚫ State
⚫ Independent variable: time (t)
⚫ Dependent variables: position (y) and velocity (v)
⚫ Initial Conditions
⚫ t0, y0, v0
![Page 9: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/9.jpg)
Projectile Motion
2
2
ma F
d ym mg
dt
=
= −
⚫ Model: vertical motion
x
y
2
2
d yg
dt= −
![Page 10: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/10.jpg)
First Order Form
⚫ Numerical integrators need differential
equations to be put into a special format.
( )
( ) 0
,
0
dxf t x
dt
x x
=
=
![Page 11: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/11.jpg)
First Order Form
⚫ Arrays of equations work too.
( )
( )
11 1
1
, , ,
, , ,
n
nn n
dxf t x x
dt
dxf t x x
dt
=
=
( ),d
tdt
=x
f x
![Page 12: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/12.jpg)
Projectile Motion
First Order Form
dyv
dt
dvg
dt
=
= −
x
y2
2
d yg
dt= −
![Page 13: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/13.jpg)
Projectile Motion
First Order Form
( )
( )
0
0
0
0
y y
v v
=
=
x
y
![Page 14: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/14.jpg)
Mass-Spring Motion
⚫ Consider the vertical motion of a character.
![Page 15: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/15.jpg)
Mass-Spring Motion
⚫ State
⚫ time: t
⚫ position: x
⚫ velocity: v
⚫ Initial Conditions
⚫ t0, x0, v0
x
![Page 16: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/16.jpg)
Mass-Spring Motion
⚫ Idealized model
m
ground
k
2
2
ma F
d xm kx
dt
=
= −
x
2
2
d x kx
dt m= −
![Page 17: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/17.jpg)
Mass-Spring Motion
⚫ First Order Form
dxv
dt
dv kx
dt m
=
= −
( )
( )
0
0
0
0
x x
v v
=
=
![Page 18: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/18.jpg)
Solving Differential Equations
⚫ Sometimes we can solve our DE exactly.
⚫ Many times our DE is too complicated to be
solved exactly.
![Page 19: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/19.jpg)
Hard Problems
⚫ Nonlinear equations
⚫ Multiple variables
![Page 20: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/20.jpg)
Hard Problems
⚫ Projectile with air resistance
( )d
m c mdt
= − −v v
v v gv
![Page 21: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/21.jpg)
Hard Problems
⚫ Mass-spring in 3D
( )0
dm k L
dt= − −
v xx
x
![Page 22: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/22.jpg)
Hard Problems
⚫ Numerical integration can help!
⚫ Handles nonlinearities
⚫ Handles multiple variables
![Page 23: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/23.jpg)
Numerical Integration
⚫ Start with our first order form
( )
( ) 0
,
0
dxf t x
dt
x x
=
=
![Page 24: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/24.jpg)
A Simple Idea
⚫ Approximate the slope.
t
x
( ) ( )x t h x tslope
h
+ −
t t+h
![Page 25: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/25.jpg)
A Simple Idea
( ) ( )x t h x tdx
dt h
+ −
⚫ Forward difference:
![Page 26: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/26.jpg)
A Simple Idea
( ) ( )( )( ),
x t h x tf t x t
h
+ −=
⚫ Shuffle terms:
( ) ( ) ( )( ),x t h x t h f t x t+ = +
![Page 27: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/27.jpg)
A Simple Idea
⚫ Using this formula, we can make a time step
h to find the new state.
⚫ We can continue making time steps as long
as we want.
⚫ The time step is usually small
( ) ( ) ( )( ),x t h x t h f t x t+ = +
![Page 28: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/28.jpg)
Explicit Euler
⚫ This is called the Explicit Euler method.
⚫ All terms on the right-hand side are known.
⚫ Substitute in the known values and compute
the new state.
( ) ( ) ( )( ),x t h x t h f t x t+ = +
![Page 29: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/29.jpg)
What If …
⚫ This is called the Implicit Euler method.
⚫ The function depends on the new state.
⚫ But we don’t know the new state!
( ) ( ) ( )( ),x t h x t h f t h x t h+ = + + +
![Page 30: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/30.jpg)
Implicit Euler
⚫ We have to solve for the new state.
⚫ We may have to solve a nonlinear equation.
⚫ Can be solved using Newton-Raphson.
⚫ Usually impractical for games.
( ) ( ) ( )( ),x t h x t h f t h x t h+ = + + +
![Page 31: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/31.jpg)
Implicit vs Explicit
⚫ Explicit is fast.
⚫ Implicit is slow.
⚫ Implicit is more stable than explicit.
![Page 32: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/32.jpg)
Opening the Black Box
⚫ Explicit and Implicit Euler don’t know about
position or velocity.
⚫ Some numerical integrators work with
position and velocity to gain some
advantages.
![Page 33: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/33.jpg)
The Position ODE
⚫ This equation is trivially linear in velocity.
⚫ We can exploit this to our advantage.
dxv
dt=
![Page 34: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/34.jpg)
Symplectic Euler
⚫ First compute the new velocity.
⚫ Then compute the new position using the
new velocity.
( ) ( )( ) ( )( )
( ) ( )( )
, ,v t h v t
f t x t v th
x t h x tv t h
h
+ −=
+ −= +
![Page 35: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/35.jpg)
Symplectic Euler
⚫ We get improved stability over Explicit Euler,
without added cost.
⚫ But not as stable as Implicit Euler
![Page 36: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/36.jpg)
Verlet
⚫ Assume forces only depend on position.
⚫ We can eliminate velocity from Symplectic
Euler.
( ) ( )( )( )
( ) ( )( )
,v t h v t
f t x th
x t h x tv t h
h
+ −=
+ −= +
![Page 37: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/37.jpg)
Verlet
1 0 1
2 1 2
2 1 1
x x hv
x x hv
v v h f
= +
= +
= +
⚫ Write two position formulas and one velocity
formula.
![Page 38: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/38.jpg)
Verlet
2
2 1 0 12x x x h f= − +
⚫ Eliminate velocity to get:
![Page 39: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/39.jpg)
Newton
⚫ Assume constant force:
( ) ( ) ( ) 21
2x t h x t v t h ah+ = + +
⚫ Exact for projectiles (parabolic motion).
![Page 40: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/40.jpg)
Demos
⚫ Projectile Motion
⚫ Mass-Spring Motion
![Page 41: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/41.jpg)
Integrator Quality
1. Stability
2. Performance
3. Accuracy
![Page 42: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/42.jpg)
Stability
⚫ Extrapolation
⚫ Interpolation
⚫ Mixed
⚫ Energy
![Page 43: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/43.jpg)
Performance
⚫ Derivative evaluations
⚫ Matrix Inversion
⚫ Nonlinear equations
⚫ Step-size limitation
![Page 44: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/44.jpg)
Accuracy
⚫ Accuracy is measured using the Taylor
Series.
( ) ( ) ( ) ( ) 21
2x t h x t x t h x t h + = + + +
![Page 45: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/45.jpg)
Accuracy
⚫ First-order accuracy is usually sufficient for
games.
⚫ You can safely ignore RK4, BDF, Midpoint,
Predictor-Corrector, etc.
⚫ Accuracy != Stability
![Page 46: Erin Catto Blizzard Entertainment - box2d.org · Explicit Euler ⚫This is called the Explicit Euler method. ⚫All terms on the right-hand side are known. ⚫Substitute in the known](https://reader034.fdocuments.net/reader034/viewer/2022051921/600ec2df1699ac5d8f347474/html5/thumbnails/46.jpg)
Further Reading &
Sample Code
⚫ http://www.gphysics.com/downloads/
⚫ Hairer, Geometric Numerical Integration