Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ......
Transcript of Lecture 5 - Time Integration 5... · velocity and position within the same step:)"+∆" =) ......
Lecture V: The game-engine loop & Time Integration
The Basic Game-Engine Loop
Integrate velocities and positions
Resolve Interpenetrations
Resolve Collisions
Previous state: "⃗ # , %(#)(⃗ # , )(#)
Per-body change "⃗ # + ∆# , %(# + ∆#)(⃗ # + ∆# , ,(# + ∆#)
Position correction
Velocity correction
Forces -⃗(#)
• Kinematics: continuous motion in continuous time.• Events are local and always valid.
• Computer simulation:• Discrete time steps ∆".• Discrete Space (mesh, particles, grids)
Challenges
• 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
• 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
• 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
• 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
• 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
• 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
• 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
• Estimating tangent in Half step:
! " + ∆"2 = ! " + ' ", ! ∆"2
• Full step:
! " + ∆" = ! " + ' " + ∆)* , ! " + ∆)
* ∆"
• 2nd-order approximation.• Compute position similarly with !.
12
Midpoint Method
• 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
)*
)+/$)′+
)+
• 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
15
Improved Euler’s Method
!(#)!%
!&
! # + ∆# = !% + !&2
∆#
The order of the error is +(∆#&). The final derivative is still inaccurate.
• 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
• 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
18
RK4
!(#)
!%
! # + ∆#
∆#/2 ∆#/2
!*
!+
!,
!% + 2!* + 2!+ + !,6
!% = ∆# / 0(#, !(#))!* = ∆# / 0 # + ∆#2 , ! # + 12 !%!+ = ∆# / 0 # + ∆#2 , ! # + 12 !*!, = ∆# / 0 # + ∆#, ! # + !+
Exercise!
Leapfrog Integration (“a cavallina”)
pos
vel
Leapfrog Integration
t (in dt)0.0 0.5 1.0 1.5 2.0 2.5
Pos Vel Pos Vel Pos Vel
Leapfrog Integrationfirst step
t (in dt)0.0 0.5 1.0 1.5 2.0 2.5
Pos0
Vel0
Vel
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⃗∆"
• More accurate than Euler-based methods• Residue of !(∆$%)
• But at the same cost as Euler’s method!• Major advantage: fully reversible!
Leapfrog Method
• Based on the Taylor expansion series of the previous time step and the next one:
!" # + ∆# + !" # − ∆#≈ !" # + ∆# ( !") # + ∆#
*
2 ( !")) # + ⋯
+ !" # − ∆# ( !") # + ∆#*
2 ∗ !")) # − ⋯
24
Verlet integration
Cancels out!
• Approximating without velocity:
!" # + ∆# = 2!" # −!" # − ∆# + ∆#)!"** #
++(∆#-)
25
Verlet integration
!"(#)
∆#∆#
!"(# − ∆#)2 ∗ !"(#) − !"(# − ∆#)
!"(# + ∆#)
∆#) ∗ 0 #
• 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
• 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
• 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
!"
!#!$
• 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
• 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
• 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
!(")'(")
!(" + ∆")
"
" + ∆"
• 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
• 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
• 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
• 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