34
Lecture V: The game-engine loop & Time Integration

Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

Lecture V: The game-engine loop & Time Integration

Page 2: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

The Basic Game-Engine Loop

Integrate velocities and positions

Resolve Interpenetrations

Resolve Collisions

Previous state: "⃗ # , %(#)(⃗ # , )(#)

Per-body change "⃗ # + ∆# , %(# + ∆#)(⃗ # + ∆# , ,(# + ∆#)

Position correction

Velocity correction

Forces -⃗(#)

Page 3: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

• Kinematics: continuous motion in continuous time.• Events are local and always valid.

• Computer simulation:• Discrete time steps ∆".• Discrete Space (mesh, particles, grids)

Challenges

Page 4: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

• Force induces acceleration.• When mass is constant:

! ", $ = & ' ((", $)• Derivatives: +, $ = (($) and ", $ = +($)• Thus: ! ", $ = & ' ",,($).

• A differential equation.• Often impossible to solve analytically.• More often, ( = ( +, $ (velocity dependence).

• Discretization: stability and convergence issues.

4

Updating Position

Page 5: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

• A function at ! + ∆! can be approximated by a polynomial centered at ! with arbitrary precision:

$ ! + ∆!≈ $ ! + ∆t ' $( ! + ∆!

)

2 $(( ! + ⋯+ ∆!,

-! $, !

• We do not usually use (or have) more than 2nd

derivatives.

5

Taylor Approximation

Page 6: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

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

# " + ∆" ≈ # " + ∆t ' #( "

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

) " + ∆" = ) " + + " ∆" = ) " + ,(")/ ∆"#(" + ∆") = #(") + )(")∆"

6

First-Order Approximation

Assumed known for this time stepUnknown for next time step

Page 7: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

• 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:)* " + ∆" = )* " + &(")∆"

8

Euler’s Method

Page 8: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

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

bouncing.

• Trajectories are piecewise-linear.• Constant velocity and

acceleration in-between frames.

9

Issues with Linear Dynamics

Page 9: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

• When ∆" → 0, we converge to % " = ∫() * + ,+

• (Im)possible solution: reducing ∆" to convergence levels?

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

• Our objective: make the most with every ∆" you get.

10

Time Step

Page 10: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

• 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.

11

Time Step

#$

#%#&errors

Page 11: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

• Estimating tangent in Half step:

! " + ∆"2 = ! " + ' ", ! ∆"2

• Full step:

! " + ∆" = ! " + ' " + ∆)* , ! " + ∆)

* ∆"

• 2nd-order approximation.• Compute position similarly with !.

12

Midpoint Method

Page 12: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

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

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

line.• …can still drift off the function.

13

Midpoint Method

)*

)+/$)′+

)+

Page 13: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

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

• Velocity to the first point (Euler’s prediction):!" = ! $ + ∆$ ' (($, !)

• Velocity to the second point (correction point):!, = ! $ + ∆$ ' ( $ + ∆$, !"

• Improved Euler’s velocity

! $ + ∆$ = !" + !,2

• Compute position similarly with !", !, instead of (.

14

Improved Euler’s Method

Page 14: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

15

Improved Euler’s Method

!(#)!%

!&

! # + ∆# = !% + !&2

∆#

The order of the error is +(∆#&). The final derivative is still inaccurate.

Page 15: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

• There are methods that provide better than quadratic error.

• The Runge-Kutta order-four method (RK4) is !(∆$%).

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

16

Runge-Kutta Method

Page 16: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

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

!" = ∆% & '(%, !(%))!+ = ∆% & ' % + ∆%2 , ! % + 12 !"!/ = ∆% & ' % + ∆%2 , ! % + 12 !+!0 = ∆% & ' % + ∆%, ! % + !/

• Blend as follows:

! % + ∆% = ! % + !" + 2!+ + 2!/ + !06• Compute position similarly with ! values.

17

RK4

Page 17: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

18

RK4

!(#)

!%

! # + ∆#

∆#/2 ∆#/2

!*

!+

!,

!% + 2!* + 2!+ + !,6

!% = ∆# / 0(#, !(#))!* = ∆# / 0 # + ∆#2 , ! # + 12 !%!+ = ∆# / 0 # + ∆#2 , ! # + 12 !*!, = ∆# / 0 # + ∆#, ! # + !+

Exercise!

Page 18: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

Leapfrog Integration (“a cavallina”)

pos

vel

Page 19: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

Leapfrog Integration

t (in dt)0.0 0.5 1.0 1.5 2.0 2.5

Pos Vel Pos Vel Pos Vel

Page 20: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

Leapfrog Integrationfirst step

t (in dt)0.0 0.5 1.0 1.5 2.0 2.5

Pos0

Vel0

Vel

Page 21: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

Leapfrog Integration

t (in ∆")0.0 0.5 1.0 1.5 2.0 2.5

Pos Vel Pos Vel Vel PosPos

$⃗ 1 = $⃗ 0 + )⃗(0.5)∆" $⃗ 2 = $⃗ 1 + )⃗(1.5)∆" $⃗ 3 = $⃗ 2 + )⃗(2.5)∆"

)⃗ 1.5 = )⃗ 0.5 + 0⃗∆" )⃗ 2.5 = )⃗ 1.5 + 0⃗∆"

Page 22: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

• More accurate than Euler-based methods• Residue of !(∆$%)

• But at the same cost as Euler’s method!• Major advantage: fully reversible!

Leapfrog Method

Page 23: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

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

!" # + ∆# + !" # − ∆#≈ !" # + ∆# ( !") # + ∆#

*

2 ( !")) # + ⋯

+ !" # − ∆# ( !") # + ∆#*

2 ∗ !")) # − ⋯

24

Verlet integration

Cancels out!

Page 24: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

• Approximating without velocity:

!" # + ∆# = 2!" # −!" # − ∆# + ∆#)!"** #

++(∆#-)

25

Verlet integration

!"(#)

∆#∆#

!"(# − ∆#)2 ∗ !"(#) − !"(# − ∆#)

!"(# + ∆#)

∆#) ∗ 0 #

Page 25: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

• An !(∆$%) order of error.• Very stable and fast without the need to estimate

velocities.• We need an estimation of the first '(($ − ∆$)

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

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

• Introduction to position-based dynamics.

26

Verlet integration

Page 26: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

• So far: computing current position !(#) 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!

! # = ! # + ∆# − ∆# * %(# + ∆#)• This particular one: backward Euler.

• Computing in inverse: • Finding position ! # + ∆# which produces ! # if

simulation is run backwards.

27

Implicit methods

Page 27: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

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

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

28

Implicit methods

!"

!#!$

Page 28: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

• How to compute the velocity from the future?

• Given the forces applied, extracting from the formula:• Example: a drag force !" = −% & ' is applied:

' ( + ∆( − ' (∆( = −% & '(( + ∆()

• And therefore

' ( + ∆( = '(()1 + ∆( & %

29

Backward Euler

Page 29: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

• 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.

30

Backward Euler

Page 30: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

• 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:

! " + ∆" = ! " + ∆" ∗ ' " = ! " + ∆" ∗ ((")/,- " + ∆" = -(") + ∆" ∗ ! " = -(") + ∆" ∗ !(" + ∆")

31

Semi-Implicit Method

!(")'(")

!(" + ∆")

"

" + ∆"

Page 31: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

• 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.

32

Semi-Implicit Method

Page 32: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

• Examples: Forward Euler!"!# =

12 0, ) "

becomes:" # + ∆# − " #

∆# = 12 0, ) # " # ⇒

" # + ∆# = " # + 12∆# 0, )(#) "(#)• The rest of the formulations follow suit for angular

displacement 0, velocity ) and acceleration 1.

33

Angular Integration

Page 33: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

• 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.

• Position-based methods• Leapfrog, Verlet.

34

Summary

Page 34: Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ... First-Order Approximation Assumed known for this time step Unknown for next time step

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

Jacobians).• Evaluation of all dimensions and variables should

be done for the same simulation time ∆".

35

Concluding remarks