lecture 4 - time discretization 4... · • this is a differential equation • often difficult to...

28
Lecture IV: Time Discretization

Upload: others

Post on 11-Oct-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 4 - Time Discretization 4... · • This is a differential equation • Often difficult to analytically solve in real-time applications. • Often ) = ) ,," dependence

Lecture IV: Time Discretization

Page 2: Lecture 4 - Time Discretization 4... · • This is a differential equation • Often difficult to analytically solve in real-time applications. • Often ) = ) ,," dependence

Game Physics

• Kinematics: continuous motion in continuous time• Computer simulation:

• Discrete time steps ∆𝑡• Discrete Space (mesh particles)

Motivation

Page 3: Lecture 4 - Time Discretization 4... · • This is a differential equation • Often difficult to analytically solve in real-time applications. • Often ) = ) ,," dependence

Game Physics

• Force induces acceleration.• When mass is constant:

𝐹 𝑝, 𝑡 = 𝑚 ∗ 𝑎(𝑝, 𝑡)• Derivatives: 𝑣- 𝑡 = 𝑎(𝑡) and 𝑝- 𝑡 = 𝑣(𝑡)• Thus: 𝐹 𝑝, 𝑡 = 𝑚 ∗ 𝑝--(𝑡)

• This is a differential equation• Often difficult to analytically solve in real-time

applications.• Often 𝑎 = 𝑎 𝑣, 𝑡 dependence.

• Discretization: stability and convergence issues.3

Updating Position

Page 4: Lecture 4 - Time Discretization 4... · • This is a differential equation • Often difficult to analytically solve in real-time applications. • Often ) = ) ,," dependence

Game Physics

• A function at 𝑡 + ∆𝑡 can be approximated by the function at 𝑡 with derivatives:

𝑝 𝑡 + ∆𝑡

= 𝑝 𝑡 + ∆t ∗ 𝑝- 𝑡 +∆𝑡0

2𝑝-- 𝑡 + ⋯+

∆𝑡3

𝑛!𝑝 3 𝑡

• We do not usually use (or have) more than second derivatives.

4

Taylor Approximation

Page 5: Lecture 4 - Time Discretization 4... · • This is a differential equation • Often difficult to analytically solve in real-time applications. • Often ) = ) ,," dependence

Game Physics

• If ∆𝑡 is small enough, we approximate linearly:

𝑝6 𝑡 + ∆𝑡 ≈ 𝑝6 𝑡 + ∆t ∗ 𝑝6- 𝑡

• Euler’s Method: approximating forward both velocity and position within the same step:

𝑣 𝑡 + ∆𝑡 = 𝑣 𝑡 + 𝑎 𝑡 ∆𝑡 = 𝑣 𝑡 +𝐹(𝑡)𝑚

∆𝑡

𝑝6(𝑡 + ∆𝑡) = 𝑝6(𝑡) + 𝑣(𝑡)∆𝑡

5

First-Order Approximation

Known for this time stepUnknown for next time step

Page 6: Lecture 4 - Time Discretization 4... · • This is a differential equation • Often difficult to analytically solve in real-time applications. • Often ) = ) ,," dependence

Game Physics

• This is known as Euler’s method𝑣 𝑡 + ∆𝑡 = 𝑣 𝑡 + 𝑎 𝑡 ∆𝑡𝑝6(𝑡 + ∆𝑡) = 𝑝6(𝑡) + 𝑣(𝑡)∆𝑡

6

Euler’s Method5.1

𝑣(𝑡 − ∆𝑡)𝑎(𝑡 − ∆𝑡)

𝑣(𝑡)𝑣(𝑡)

𝑎(𝑡)𝑣(𝑡 + ∆𝑡)

𝑡

𝑡 + ∆𝑡

Page 7: Lecture 4 - Time Discretization 4... · • This is a differential equation • Often difficult to analytically solve in real-time applications. • Often ) = ) ,," dependence

Game Physics

• Note: we approximate the velocity as constantbetween frames. • We compute the acceleration of the object from the net

force applied on it:

𝑎 𝑡 =𝐹 𝑡𝑚

• We compute the velocity from the acceleration:𝑣 𝑡 + ∆𝑡 = 𝑣 𝑡 + 𝑎 𝑡 ∆𝑡

• We compute the position from the velocity:𝑝6 𝑡 + ∆𝑡 = 𝑝6 𝑡 + 𝑣(𝑡)∆𝑡

7

Euler’s Method

Page 8: Lecture 4 - Time Discretization 4... · • This is a differential equation • Often difficult to analytically solve in real-time applications. • Often ) = ) ,," dependence

Game Physics

• A mere sequence of instants.• Without the precise instant of

bouncing.

• Trajectories are piecewise-linear.• Constant velocity and

acceleration in-between frames.

8

Issues with Linear Dynamics

Page 9: Lecture 4 - Time Discretization 4... · • This is a differential equation • Often difficult to analytically solve in real-time applications. • Often ) = ) ,," dependence

Game Physics

• When ∆𝑡 → 0, we converge to 𝑝6 𝑠 → ∫ 𝑣 𝑡 𝑑𝑡?@ .

• Reducing ∆𝑡 as much as possible?

• First-order method, piecewise-constant velocity: not very stable.

9

Time Step

Page 10: Lecture 4 - Time Discretization 4... · • This is a differential equation • Often difficult to analytically solve in real-time applications. • Often ) = ) ,," dependence

Game Physics

• First-order assumption: the slope at 𝑡as a good estimate for the slope over the entire interval ∆𝑡.

• The approximation can drift off the function.• Farther drifting ó tangent approximation worse.

10

Time Step

𝑥@

𝑥B𝑥0

errors

Page 11: Lecture 4 - Time Discretization 4... · • This is a differential equation • Often difficult to analytically solve in real-time applications. • Often ) = ) ,," dependence

Game Physics

• Estimating tangent in Half step:

𝑣 𝑡 +∆𝑡2

= 𝑣 𝑡 + 𝑎 𝑡, 𝑣∆𝑡2

• Full step:

𝑣 𝑡 + ∆𝑡 = 𝑣 𝑡 + 𝑎 𝑡 + ∆C0, 𝑣 𝑡 + ∆C

0∆𝑡

• Second-order approximation.

11

Midpoint Method

Page 12: Lecture 4 - Time Discretization 4... · • This is a differential equation • Often difficult to analytically solve in real-time applications. • Often ) = ) ,," dependence

Game Physics

• Approximating the tangent in mid-interval.• Applying it to initial point across the entire interval.• Error order: the square of the time step 𝑂 ∆𝑡0 .

Better than Euler’s method (𝑂(∆𝑡)) when ∆𝑡 < 1.• Approximating with a quadratic curve instead of a

line.• …can still drift off the function.

12

Midpoint Method

𝑥@

𝑥B/0𝑥′B

𝑥B

Page 13: Lecture 4 - Time Discretization 4... · • This is a differential equation • Often difficult to analytically solve in real-time applications. • Often ) = ) ,," dependence

Game Physics

• Considers the tangent lines to the solution curve at both ends of the interval.

• Velocity to the first point (Euler’s prediction):𝑣B = 𝑣 𝑡 + ∆𝑡 ∗ 𝑎(𝑡, 𝑣)

• Velocity to the second point (correction point):𝑣0 = 𝑣 𝑡 + ∆𝑡 ∗ 𝑎 𝑡 + ∆𝑡, 𝑣B

• Improved Euler’s velocity

𝑣 𝑡 + ∆𝑡 =𝑣B + 𝑣02

• Compute position similarly with 𝑣B, 𝑣0 instead of 𝑎.

13

Improved Euler’s Method

Page 14: Lecture 4 - Time Discretization 4... · • This is a differential equation • Often difficult to analytically solve in real-time applications. • Often ) = ) ,," dependence

Game Physics 14

Improved Euler’s Method

𝑣(𝑡)

𝑣B

𝑣0

𝑣 𝑡 + ∆𝑡 =𝑣B + 𝑣02

∆𝑡

The order of the error is 𝑂(∆𝑡0). The final derivative is still inaccurate.

Page 15: Lecture 4 - Time Discretization 4... · • This is a differential equation • Often difficult to analytically solve in real-time applications. • Often ) = ) ,," dependence

Game Physics

• There are methods that provide better than quadratic error.

• The Runge-Kutta order-four method (RK4) is 𝑂(∆𝑡I).

• A combination of the midpoint and modified Euler’s methods, with higher weights to the midpoint tangents than to the endpoints tangents.

15

Runge-Kutta Method

Page 16: Lecture 4 - Time Discretization 4... · • This is a differential equation • Often difficult to analytically solve in real-time applications. • Often ) = ) ,," dependence

Game Physics

• Computing the four following tangents (note dependence of acceleration on velocity):

𝑣B = ∆𝑡 ∗ 𝑎(𝑡, 𝑣(𝑡))

𝑣0 = ∆𝑡 ∗ 𝑎 𝑡 +∆𝑡2, 𝑣 𝑡 +

12𝑣B

𝑣J = ∆𝑡 ∗ 𝑎 𝑡 +∆𝑡2, 𝑣 𝑡 +

12𝑣0

𝑣I = ∆𝑡 ∗ 𝑎 𝑡 + ∆𝑡, 𝑣 𝑡 + 𝑣J• Blend as follows:

𝑣 𝑡 + ∆𝑡 = 𝑣 𝑡 +𝑣B + 2𝑣0 + 2𝑣J + 𝑣I

6• Compute position similarly with 𝑣 values.

16

RK4

Page 17: Lecture 4 - Time Discretization 4... · • This is a differential equation • Often difficult to analytically solve in real-time applications. • Often ) = ) ,," dependence

Game Physics 17

RK4

𝑣(𝑡)

𝑣B

𝑣 𝑡 + ∆𝑡

∆𝑡/2 ∆𝑡/2

𝑣0

𝑣J

𝑣I

𝑣B +2𝑣0 +2𝑣J +𝑣I6

𝑣B = ∆𝑡 ∗ 𝑎(𝑡, 𝑣(𝑡))

𝑣0 = ∆𝑡 ∗ 𝑎 𝑡 +∆𝑡2 , 𝑣 𝑡 +

12𝑣B

𝑣J = ∆𝑡 ∗ 𝑎 𝑡 +∆𝑡2 , 𝑣 𝑡 +

12𝑣0

𝑣I = ∆𝑡 ∗ 𝑎 𝑡 + ∆𝑡, 𝑣 𝑡 + 𝑣J

5.2

Page 18: Lecture 4 - Time Discretization 4... · • This is a differential equation • Often difficult to analytically solve in real-time applications. • Often ) = ) ,," dependence

Game Physics

• Based on the Taylor expansion series of the previous time step and the next one:

𝑝6 𝑡 + ∆𝑡 + 𝑝6 𝑡 − ∆𝑡

= 𝑝6 𝑡 + ∆𝑡 ∗ 𝑝6- 𝑡 +∆𝑡0

2∗ 𝑝6-- 𝑡 + ⋯

+𝑝6 𝑡 − ∆𝑡 ∗ 𝑝6- 𝑡 +∆𝑡0

2∗ 𝑝6-- 𝑡 − ⋯

18

Verlet integration

Page 19: Lecture 4 - Time Discretization 4... · • This is a differential equation • Often difficult to analytically solve in real-time applications. • Often ) = ) ,," dependence

Game Physics

• Approximating without velocity:

𝑝6 𝑡 + ∆𝑡 = 2𝑝6 𝑡 −𝑝6 𝑡 − ∆𝑡 + ∆𝑡0𝑝6-- 𝑡

+𝑂(∆𝑡I)

19

Verlet integration

𝑝6(𝑡)

∆𝑡∆𝑡

𝑝6(𝑡 − ∆𝑡)2 ∗ 𝑝6(𝑡) − 𝑝6(𝑡 − ∆𝑡)

𝑝6(𝑡 + ∆𝑡)

∆𝑡0 ∗ 𝑎 𝑡

Page 20: Lecture 4 - Time Discretization 4... · • This is a differential equation • Often difficult to analytically solve in real-time applications. • Often ) = ) ,," dependence

Game Physics

• An 𝑂(∆𝑡0)order of error.• Very stable and fast without the need to estimate

velocities.• We need an estimation of the first 𝑝6(𝑡 − ∆𝑡)

• Usually obtained from one step of Euler’s or RK4 method.

• Difficult to manage velocity related forces such as drag or collision.

20

Verlet integration

Page 21: Lecture 4 - Time Discretization 4... · • This is a differential equation • Often difficult to analytically solve in real-time applications. • Often ) = ) ,," dependence

Game Physics

• So far: computing current position 𝑝6(𝑡) and velocity 𝑣(𝑡) for the next position (forward).• Those are denoted as explicit methods.

• In implicit methods, we make use of the quantities from the next time step!

𝑝6 𝑡 = 𝑝6 𝑡 + ∆𝑡 − ∆𝑡 ∗ 𝑣(𝑡 + ∆𝑡)• this particular one is called backward Euler.• the goal: finding position 𝑝6 𝑡 + ∆𝑡 which produces 𝑝6 if

running the simulation backwards.

21

Implicit methods

Page 22: Lecture 4 - Time Discretization 4... · • This is a differential equation • Often difficult to analytically solve in real-time applications. • Often ) = ) ,," dependence

Game Physics

• Not more accurate than explicit methods, but more stable.

• Especially for a dampingof the position (e.g. drag force or kinetic friction).

22

Implicit methods

𝑥0

𝑥B𝑥@

Page 23: Lecture 4 - Time Discretization 4... · • This is a differential equation • Often difficult to analytically solve in real-time applications. • Often ) = ) ,," dependence

Game Physics

• How to compute the velocity at a position in the future?

• Given the forces applied, extracting from the formula:• Example: a drag force 𝐹L = −𝑏 ∗ 𝑣 is applied:

𝑣 𝑡 + ∆𝑡 = 𝑣 𝑡 − ∆𝑡 ∗ 𝑏 ∗ 𝑣(𝑡 + ∆𝑡)• And therefore

𝑣 𝑡 + ∆𝑡 =𝑣(𝑡)

1 + ∆𝑡 ∗ 𝑏

23

Backward Euler

Page 24: Lecture 4 - Time Discretization 4... · • This is a differential equation • Often difficult to analytically solve in real-time applications. • Often ) = ) ,," dependence

Game Physics

• Often not knowing the forces in advance (likely case in a game).

• Or that the backward equation is not easy to solve.• We use a predictor-corrector method:

• one step of explicit Euler’s method• use the predicted position to calculate 𝑣(𝑡 + ∆𝑡)

• More accurate than explicit method but twice the amount of computation.

24

Backward Euler

Page 25: Lecture 4 - Time Discretization 4... · • This is a differential equation • Often difficult to analytically solve in real-time applications. • Often ) = ) ,," dependence

Game Physics

• Combines the simplicity of explicit Euler and stability of implicit Euler.

• Runs an explicit Euler step for velocity and then an implicit Euler step for position:

𝑣 𝑡 + ∆𝑡 = 𝑣 𝑡 + ∆𝑡 ∗ 𝑎 𝑡 = 𝑣 𝑡 + ∆𝑡 ∗ 𝐹(𝑡)/𝑚

𝑝 𝑡 + ∆𝑡 = 𝑝(𝑡) + ∆𝑡 ∗ 𝑣 𝑡 = 𝑝(𝑡) + ∆𝑡 ∗ 𝑣(𝑡 + ∆𝑡)

25

Semi-Implicit Method

𝑣(𝑡)𝑎(𝑡)

𝑣(𝑡 + ∆𝑡)

𝑡

𝑡 + ∆𝑡

Page 26: Lecture 4 - Time Discretization 4... · • This is a differential equation • Often difficult to analytically solve in real-time applications. • Often ) = ) ,," dependence

Game Physics

• The position update in the second step uses the next velocity in the implicit scheme.• Good for position-dependent forces.• Conserves energy over time, and thus stable.

• Not as accurate as RK4 (order of error is still 𝑂(∆𝑡)), but cheaper and yet stable.

• Very popular choice for game physics engine.

26

Semi-Implicit Method

Page 27: Lecture 4 - Time Discretization 4... · • This is a differential equation • Often difficult to analytically solve in real-time applications. • Often ) = ) ,," dependence

Game Physics

• First-order methods• Implicit and Explicit Euler method, Semi-implicit Euler,

Exponential Euler• Second-order methods

• Verlet integration, Velocity Verlet, Trapezoidal rule, Beeman’s algorithm, Midpoint method, Improved Euler’s method, Heun’s method, Newmark-beta method, Leapfrog integration.

• Higher-order methods• Runge-Kutta family methods, Linear multistep method.

27

Summary

Page 28: Lecture 4 - Time Discretization 4... · • This is a differential equation • Often difficult to analytically solve in real-time applications. • Often ) = ) ,," dependence

Game Physics

• Dimension• Integration methods shown for 1D variables.• Generalization using vector based structures (e.g.,

Jacobians).

• Rotational motion• The integration methods work exactly the same for

angular displacement 𝜃, velocity 𝜔 and acceleration 𝛼.

• Evaluation of all dimensions and variables should be done for the same simulation time 𝑡.

28

Concluding remarks