SlideShare a Scribd company logo
Numerical Methods
Erin Catto
Blizzard Entertainment
Sometimes the mathematical problems we are faced with in game physics are too difficult to solve exactly. Numerical
methods provide a set of tools to get approximate solutions to these difficult problems. In particular, numerical methods
help us get approximate solutions to the differential equations that arise in rigid body and point mass physics.

There are many numerical methods available and it isn’t always clear which method to use in which situation. What is the
right performance versus accuracy trade-off? Will my simulation blow up? What are the common methods used in game
physics?

I hope this presentation helps you better understand numerical methods and their application.
First, a little review …
Differential Calculus
2
The derivative of a function is the slope
f x( )
f x + h( )
x x + h
df
dx
= lim
h→0
f x + h( )− f x( )
hsecant
3
The derivative of a function f(x) is the slope at position x. The derivative is defined as the limit of the secant as the increment
h goes to zero.
Line: the derivative is constant
x
0 2 4 6 8 10
y
0
1
2
3
4
5
6
7
8
9
10
run = 2
rise = 1
y-intercept b = 1
slope m = rise/run = 0.5
y = mx + bfunction:
dy
dx
= mderivative:
4
The derivative of a line is the slope, which is the rise over the run. If the line is horizontal, y is constant and the derivative are
zero. In other words, y does not change with x.
Parabola: the derivative is a line
y = ax2
function:
dy
dx
= 2axderivative:
x
-1 -0.5 0 0.5 1
y
0
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
2
tangent
5
We can also think of the derivative as the tangent. It is the slope at the current point on the curve. In general the slope
changes with the x value.
Exponential: the derivative is another
exponential
y = eax
dy
dx
= aeax
x
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
y
0
0.5
1
1.5
2
2.5
3
3.5
4
function:
derivative:
6
The exponential shows up a lot in physics especially when there is damping. It has the neat property that its derivative is
another exponential.
7
Now a bit of Pokemon math.
Sine wave: the derivative is another
sine wave
y = sin x
dy
dx
= cosx
x
-10 -5 0 5 10y
-2
-1.5
-1
-0.5
0
0.5
1
1.5
2
sine
cosine
function:
derivative:
8
The derivative of a sine wave is a cosine wave.
Functions of time: the derivative is the
rate of change
!x =
dx
dt
!!x =
d2
x
dt2
velocity:
acceleration:
9
In physics we usually deal with functions of time. If x is position, then x dot is velocity and x double dot is acceleration. I’ll
be using this dot notation a lot.
More review: differential equations
I have the slope
I want the original function
10
I’ve showed a few examples of you can compute a derivative given a function. What if we have the derivative and want to
get the function back?
A differential equation is a
formula for the slope
!x = f (t,x)
11
(first order differential equation)
This is the standard form of a first order differential equation in time.
Solving the differential equation
means getting the original function
!x = f (t,x) x(t)solve
12
Solving a differential equation means getting the original function back. If our function f depends only on t, we only need to
perform integration. However, if f depends on x, then solution becomes more challenging.
The simplest differential equation
!x = 0
x = x0
solution:
(constant)
13
So lets start solving some differential equations!

Here’s is the simplest differential equation I could find and its solution.
Another differential equation
!x = a
x = at + x0
solution:
14
This is just a line.
What if x is on the right?
!x = ax
x = x0eat
solution:
15
d
dt
(x0eat
) = ax0eat
= a x0eat
( )= axproof:
The exponential is needed to solve this differential equation.

Remember that the derivative of an exponential is another exponential.
Newton’s 2nd Law
ma = F
m!!x = F
or
16
(second order differential equation)
Newton’s law is a differential equation for the acceleration. Because it involves the second derivative, it is a second order
differential equation.
Gravity
m!!y = −mg
17
m
F=-mg
y
Under gravity the acceleration is constant.
Solution
!!y = −g
!y = v0 − gt
y = y0 + v0t −
1
2
gt2
18
Solving for a constant force just requires the integration (anti-derivative). I’m also accounting for the initial position y0 and
velocity v0.
Parabolic motion
This is why projectiles follow a parabolic path.
Mass Spring
m!!x + kx = 0
20
spring k
ground
x
mass m
The mass spring system has a force that depends on position. The more the spring is stretched, the harder it pulls.
Angular frequency
ω =
k
m
radians per second
!!x +ω2
x = 0
22
I can divide through by the mass to go from two parameters (m,k) to one parameter (omega). 

Omega is the angular frequency of the mass-spring system. Notice that the angular frequency increases with the spring
stiffness and decreases with the mass.
Solution
x =
v0
ω
sinωt + x0 cosωt
23
The solution is a sine wave with angular frequency omega. We need to combine a sine wave and cosine wave to account for
initial conditions.
Position and velocity versus time
time
0 2 4 6 8 10 12 14 16 18 20
position
-1
-0.5
0
0.5
1
time
0 2 4 6 8 10 12 14 16 18 20
velocity
-1
-0.5
0
0.5
1
x0 = 1
v0 = 0
ω = 1
24
These graphs show position and velocity versus time. Notice that position and velocity are 90 degrees out of phase. So the
position is a cosine wave and the velocity is a sine wave.
Phase Portrait: velocity versus postion
position
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
velocity
-2
-1.5
-1
-0.5
0
0.5
1
1.5
2
start
x0 = 1
v0 = 0
ω = 1
25
A nice way to visualize the oscillation is to plot velocity versus position. You can view this as a parametric curve with time as
the curve parameter. This is drawn here as a unit circle, but keep in mind that position and velocity have different units. The
arrows show the direction of increasing time. The position and velocity start at the green circle and progressing clockwise
around the circle. And the state continues around the circle forever. The angular frequency controls how fast the state goes
around the circle as time moves forward.

This graph is sometimes called the phase plot or phase portrait. This plot shows all possible states (phases) of the system
as time goes forward.
Phase portrait: velocity versus postion
26
This animation shows position and velocity versus time along with the phase portrait.
System of differential equations
solution???
27
So far I have showed some simple physical systems, such as a projectile and a mass-spring system.

The simulations in modern games are more complex. Often there are multiple rigid bodies colliding with friction. This means
that there are multiple mutually dependent differential equations. We have little hope of finding the exact solutions for these
complex systems of differential equations.

Therefore we need to turn to numerical methods.
Numerical solution
• Sacrifice accuracy
• Time stepping
• Generic simulation
28
We can simulate systems of differential equations using numerical methods. This means we are abandoning exact answers.
But we are making games, so this is okay. You will see that numerical solution works well with typical game loops.
Numerical solution also creates a nice abstraction that allows us to solve differential equations in a generic manner. These
methods can handle any number of rigid bodies and any number of forces (as memory and processor power allow).
Generic differential equation
!x = f t,x( )x =
x1
!
xn
⎧
⎨
⎪
⎩
⎪
⎫
⎬
⎪
⎭
⎪
first order system of differential equations
29
Numerical methods like to have differential equations in a standard form. We can have multiple differential equations
assembled into an array. That way we can solve them all at once. This also allows us to account for objects that are
interacting and have mutually dependent solutions, such as stacking of rigid bodies.
Handling second order differential
equations
m!!x = Fhave:
!x = f x( )want:
30
I will be dealing with Newton’s law and I need a way to get this into the first order form needed by numerical methods.
Split into two differential equations
!v =
F
m
!x = v
!v
!x
⎡
⎣
⎢
⎤
⎦
⎥ =
F
m
v
⎡
⎣
⎢
⎢
⎢
⎤
⎦
⎥
⎥
⎥
group
!x f x( )
31
The idea is to convert a single second order differential equation into two first order differential equations. This is
accomplished by introducing the velocity as an independent state. Then I get a first order DE for the velocity and a first
order DE for the position.
Finite Difference
dx
dt
≈
x2 − x1
h
forward difference
x
tt1
x1
x2
h
32
The basic approach of numerical solution is to use the finite difference. This allows me to convert a differential equation into
an algebraic equation. There are many kinds of finite difference formulas. This one is the forward difference. It approximates
the derivative as the next value x2 minus the current value x1 over the time step h.

You can see by the green line that this really is an approximation of the slope at x1. The approximation gets better by
decreasing the time step h. However, taking smaller time steps is more expensive because you need to take more of them.
Backwards difference
x
tt1
x0
x1
dx
dt
≈
x1 − x0
h
h
33
We can also flip things around and approximate the derivative based on the previous value. This is called the backwards
difference.
Numerical solution via explicit Euler
!x = f t,x( ) x2 − x1
h
= f t1,x1( )forward difference
x2 = x1 + hf t1,x1( ) explicit Euler
34
Lets take our standard first order differential equation and apply the forward difference formula to remove the derivative.
Notice that this converts the differential equation into an algebraic equation. This leads to the explicit Euler method: we can
simply plug in the current value for x1 and t1 to compute x2.
Explicit Euler for Newton’s law
v2 = v1 + h
F1
m
x2 = x1 + hv1
35
I can apply explicit Euler to Newton’s law. Velocity is extrapolated based on the current force and position is extrapolated
based on the current velocity.
Pseudocode
struct State
{
float t, x, v;
}
State ExplicitEuler(float h, State state1)
{
float force = ComputeForce(state1);
State state2;
state2.t = state1.t + h;
state2.v = state1.v + h * force / mass;
state2.x = state1.x + h * state1.v;
return state2;
}
36
Explicit Euler for mass-spring system
v2 = v1 − hω2
x1
x2 = x1 + hv1
37
Here is explicit Euler for the mass-spring system. So here omega is the angular frequency in radians per second. Notice how
explicit Euler gives us a way to simulate an oscillation without using trig functions.
Explicit Euler phase portrait
x0 = 1
v0 = 0
ω = 1
h = 0.1
position
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
velocity
-2
-1.5
-1
-0.5
0
0.5
1
1.5
2
explicit
exact
start
38
Here is a Matlab simulation of the mass-spring system using explicit Euler. As before it is a plot of velocity versus position.
The exact solution is a circle. But explicit Euler is diverging and the amplitude is growing unbounded as time progresses.
Explicit Euler is extrapolation
39
t
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
1
2
3
4
5
6
7
8
et
1+t
error
Explicit Euler is essentially extrapolation. Intuitively extrapolation is bad. If the function is rapidly changing, the extrapolation
has poor accuracy and can lead to a numerical instability.
Stability of explicit Euler
v2
x2
⎡
⎣
⎢
⎢
⎤
⎦
⎥
⎥
=
v1
x1
⎡
⎣
⎢
⎢
⎤
⎦
⎥
⎥
+ h
−ω2
x1
v1
⎡
⎣
⎢
⎢
⎤
⎦
⎥
⎥
v2
x2
⎡
⎣
⎢
⎢
⎤
⎦
⎥
⎥
= 1 −hω2
h 1
⎡
⎣
⎢
⎤
⎦
⎥
v1
x1
⎡
⎣
⎢
⎢
⎤
⎦
⎥
⎥ x2 = Ax1
40
The Matlab simulation showed that explicit Euler is unstable. We can also understand the instability by looking at the explicit
Euler equations in detail. For the mass-spring system, the time stepping process is equivalent to multiplying the previous
state vector by a matrix A.
Stability is determined by growth
x2 = Ax1
x3 = A2
x1
!
xn = An−1
x1
A ≤1stable if:
41
As time marches forward, I am just multiply the previous state by the same matrix over and over. The magnitude of this
matrix must be less than one, otherwise the solution will blow up.
Use eigenvalues to determine
the magnitude of the matrix
Au = λu
eigenvalue
eigenvector
A = λmax
42
The magnitude of a matrix can be represent by its eigenvalue lambda, which is a scalar. Please see Wikipedia for more
information on eigenvalues.
Use the determinant to compute the
eigenvalue
λI − A( )u = 0
det λI − A( )= 0
43
I can compute the eigenvalue of a matrix using the determinant.
Eigenvalue for explicit Euler
det λ
1 0
0 1
⎡
⎣
⎢
⎤
⎦
⎥ − 1 −hω2
h 1
⎡
⎣
⎢
⎤
⎦
⎥
⎛
⎝
⎜
⎞
⎠
⎟ = 0
λ2
− 2λ +1+ h2
ω2
= 0 characteristic polynomial
44
The determinant produces a polynomial in lambda. This is called the characteristic polynomial. You can compute the roots
of the polynomial to determine the values of lambda. In this case there are two values of lambda, but I just care about the
value with the largest magnitude.
Eigenvalue for explicit Euler
λ = 1± ihω λ = 1+ h2
ω2
λ ≥1
(unstable if h and omega > 0)
45
The eigenvalue is a complex number, but its magnitude is greater than one if the spring stiffness is positive. This shows that
explicit Euler is unstable. You can get explicit Euler to be stable by adding some damping, but as you will see, there are
better alternatives.
Numerical solution via implicit Euler
!x = f t,x( ) x1 − x0
h
= f x1( )backwards difference
x2 = x1 + hf x2( )
46
Now lets use the backwards difference instead of the forward difference. After shifting the indices, this leads to an implicit
function for x2 and the method is called implicit Euler. To actually compute x2 we have to solve an algebraic equation. It may
be nonlinear, so we may have to use numerical root finding, such as the Newton-Raphson method.
Implicit Euler for mass-spring system
v2 = v1 − hω2
x2
x2 = x1 + hv2
47
Here is implicit Euler for the mass-spring system. Notice that x2 and v2 appear on both sides of the formulas. This means I
have to solve for x2 and v2 to advance time.

I can solve these equations easily since they are linear and only have two variables. Larger simulations and non-linearities
can make implicit Euler much more challenging to implement.
Implicit Euler phase portrait
position
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
velocity
-2
-1.5
-1
-0.5
0
0.5
1
1.5
2
implicit
exact
start
x0 = 1
v0 = 0
ω = 1
h = 0.1
48
The phase portrait shows that implicit Euler is stable. Also, implicit Euler is damped. So even though the mass should
oscillate forever, implicit Euler damps the motion and brings the mass to rest. I call this numerical damping.

If you compare this figure to explicit Euler, you should see that they look they look similar, except that explicit Euler is
growing. This is because implicit Euler gives the same result as running explicit Euler in reverse.
Implicit Euler eigenvalue
λ =
1
1+ h2
ω2
λ ≤1
(always stable)
49
I performed a similar computation for implicit Euler and the result shows that implicit Euler is stable for any positive value of
omega.
Semi-implicit Euler: cheap and stable
v2 = v1 + h
F1
m
x2 = x1 + hv1
v2 = v1 + h
F1
m
x2 = x1 + hv2
Explicit Semi-implicit
50
Explicit Euler is efficient but has poor stability. Implicit Euler has great stability but can be expensive. There is a 3rd method
that strikes a balance between these two methods, giving good stability and performance.

If I use the current velocity in the position update, then explicit Euler becomes semi-implicit Euler. This small difference
makes a huge difference in stability. Yet the computational cost is just as good as explicit Euler.

Semi-implicit Euler is often called symplectic Euler because it has some good energy conservation properties. You’ll see that
shortly.
λ2
+ h2
ω2
− 2( )λ +1= 0
λ = 1 if hω ≤ 2
Semi-implicit Euler eigenvalue
characteristic polynomial
conditionally stable
51
An eigenvalue analysis shows that semi-implicit Euler is conditionally stable. This is much better than explicit Euler and in
practice we can push semi-implicit Euler quite hard before it goes unstable.
Semi-implicit Euler is conditionally stable
position
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
velocity
-2
-1.5
-1
-0.5
0
0.5
1
1.5
2
semi-implicit
exact
start
x0 = 1
v0 = 0
ω = 1
h = 0.1
52
At omega = 1, semi-implicit Euler is stable and accurate. Additionally it conserves energy on average.
Increasing the frequency
ω = 4
h = 0.1
position
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
velocity
-8
-6
-4
-2
0
2
4
6
8
semi-implicit
exact
53
Increasing the angular frequency reveals some deviation from the exact solution. But it is still stable and still conserves
energy on average.
ω = 4
Increasing the frequency
ω = 10
h = 0.1
position
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
velocity
-20
-15
-10
-5
0
5
10
15
20
semi-implicit
exact
hω = 1
55
Half the stability limit, things get strange, but it is still stable.
ω = 10
Increasing the frequency
ω = 16
h = 0.1
position
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
velocity
-30
-20
-10
0
10
20
30
semi-implicit
exact
57
This get really wacky at larger frequencies. Yet it didn’t blow up yet.
Increasing the frequency
ω = 24
h = 0.1
position
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
velocity
-40
-30
-20
-10
0
10
20
30
40
semi-implicit
exact
hω = 2.4
58
This shows that eventually semi-implicit Euler will blow up.

Rule of thumb: aim for at least 4 time steps per spring oscillation.
Your first choice should be
semi-implicit Euler
59
Runge Kutta Implicit Euler
Explicit Euler
Semi-implicit Euler
Mid-point
Verlet
So far I have shown 3 numerical methods for differential equations: explicit Euler, implicit Euler, and semi-implicit Euler.
There are many more algorithms out there, such as Runge-Kutta, mid-point, predictor-corrector, and more. For games, your
first choice should be semi-implicit Euler. It is fast, reasonably stable, and easy to implement.

The increased accuracy of Runge-Kutta, such as RK4, may seem appealing. However, any simulation with rigid body
collisions is already wildly inaccurate. What matters more is stability and performance. In this department semi-implicit Euler
delivers.
Application to games
Gyroscopic Motion
60
Now I would like to show an example of applying numerical methods to solve a difficult problem in rigid body dynamics. In
particular we are going to look at gyroscopic motion of rigid bodies.
Rigid body rotation
I !ω +ω × Iω = T
I : inertia tensor
ω : angular velocity
T : applied torque
ω × Iω : gyroscopic torque
ω
L
angular momentum
L = Iω
61
This equation is known as Euler’s equations of motion. You can view it as Newton’s law for rotation. The inertia tensor is
represented by a 3-by-3 matrix. The angular velocity and the applied torque are both 3D column vectors. Note that now
omega represents angular velocity, not angular frequency! Curiously, they both have the same units: radians per second.

This equation looks like ma = F, except there is a middle term that depends on angular velocity. This is the gyroscopic
torque. It accounts for non-spherical shapes that may wobble as they spin freely in the air. Specifically, it arises because
angular velocity and angular momentum may point in different directions.
The gyroscopic torque is due
to a non-uniform inertia tensor
I =
Ixx 0 0
0 Iyy 0
0 0 Izz
⎡
⎣
⎢
⎢
⎢
⎢
⎤
⎦
⎥
⎥
⎥
⎥
L = Iω =
Ixxωx
Iyyωy
Izzωz
⎡
⎣
⎢
⎢
⎢
⎢
⎤
⎦
⎥
⎥
⎥
⎥
ω × Iω = stuff
Ixx ≠ Iyy ≠ Izz
Ixx
Iyy
Izz
The inertia tensor for a box with non-equal sides is non-uniform. So apply the inertia tensor to the angular velocity results in
an angular momentum that points in a different direction than the angular velocity. So the cross product between the angular
velocity and the angular momentum is non-zero.
Semi-implicit Euler
ω2 = ω1 + hI1
−1
T1 −ω1 × I1ω1( )
q2 = q1 +
h
2
ω2q1
63
This is semi-implicit Euler applied to the rotational equation. I’m using a special formula to update the quaternion. See the
references for details.
Capsule spin test
64
Here is a simulation of a spinning capsule using semi-implicit Euler. This video was created using the Blizzard physics
engine called Domino.
Why is this unstable?
ω2 = ω1 + hI1
−1
T1 −ω1 × I1ω1( )
quadratic in omega
65
Here is the velocity update from semi-implicit Euler. This blows up because semi-implicit Euler extrapolates velocity. Since
the gyroscopic torque is quadratic in the angular velocity, the extrapolation easily diverges.
Typical trickery
Drop gyroscopic term: I !ω = T
Angular momentum as state: L = Iω
!L = T
66
There are a couple tricks used to avoid the gyroscopic term. Many physics engines simply drop the gyroscopic term. This is
what I have done for many years.

Some folks like to avoid the gyroscopic term by using the angular momentum L rather than the angular velocity. This makes
the gyroscopic term disappear from the rigid body equations of motion.
The problem with angular momentum
L2 = L1 + hT
ω2 = I1
−1
L2
q2 = q1 +
h
2
ω2q1
old inertia tensor : incorrect angular velocity
67
I can propagate the angular momentum with good stability using semi-implicit Euler. I need the updated angular velocity to
compute the new orientation (quaternion). To compute the new angular velocity I need to apply the inverse inertia tensor to
the angular momentum. In general the inertia tensor depends on orientation, so I am forced to use the old inertia tensor. This
loses the correct rotational behavior.
Lack of gyroscopic term in Diablo 3
68
In the past I’ve run into real game scenarios where dropping the gyroscopic term caused some odd behavior. In this case
this creature’s staff is a slender box that spins a bit too much.
Dropping the gyroscopic term
69
The capsule test is now stable. But it is too stable! The capsule spins forever.
… adding rolling resistance
70
Adding rolling resistance results in a simulation that is still stable, but not plausible.
Superposition Principle
Δω1 = hI1
−2
T1
Δω2 = −hI2
−1
ω2 × I2ω2( )
stage 1 explicit:
stage 2 implicit:
ω2 = ω1 + Δω1 + Δω2
71
I can include the gyroscopic torque by using superposition principle which says that the effect of multiple separate torques
is additive. This lets me use implicit Euler on the gyroscopic torque while using explicit Euler on all the torques.
Solving the implicit equation
I2 ω2 −ω1( )+ hω2 × I2ω2 = 0
f ω2( )= 0
72
For implicit solution of the gyroscopic term, I need to solve for omega. However, this is a nonlinear equation in 3 variables
(xyz rotation). So I solve this using Newton-Raphson iteration. Therefore I put the problem into a standard form of some
function of omega equals zero.
Problem: inertia tensor
I don’t have I2
Solution: work in local coordinates
The inertia tensor is constant in local coordinates
73
The inertia tensor in world coordinates depends on the orientation of the rigid body. So we cannot compute I2. By working
in local coordinates, I can side-step this problem.
Newton-Raphson solver
f (x) = 0
xn+1 = xn −
fn
fn '
f x( )= 0
J xn+1 − xn( )= fn
J =
∂f
∂x1
…
∂f
∂xn
⎡
⎣
⎢
⎢
⎤
⎦
⎥
⎥
single variable multiple variables
74
Here is a summary of Newton-Raphson. Typically it is described as a method for solving a single scalar equation. However,
it can be extended to solve multiple simultaneous equations. To do this I need to compute the Jacobian of the function.
Build Jacobian in local coordinates
J = Ib + h skew(ωb )Ib − skew Ibωb( )⎡⎣ ⎤⎦
skew a( )=
0 −az ay
az 0 −ax
−ay ax 0
⎡
⎣
⎢
⎢
⎢
⎢
⎤
⎦
⎥
⎥
⎥
⎥
75
Normally the inertia tensor is a function of orientation. However, I can build the implicit function for omega in local
coordinates (b for body coordinates). The inertia tensor is constant in local coordinates so it does not need to be
differentiated when I compute the Jacobian. Here the skew function takes a vector and builds the corresponding skew
symmetric matrix. This vastly simplifies the solution.
Gyrocopic torque solver
Vec3 SolveGyroscopic(Quat q, Matrix Ib, Vec3 omega1, float h)
{
// Convert to body coordinates
Vec3 omegab = InverseRotate(q, omega1);
// Residual vector
Vec3 f = h * Cross(omegab, Multiply33(Ib, omegab));
// Jacobian
Matrix J = Ib + h *(Multiply33(Skew(omegab), Ib) - Skew(Multiply33(Ib, omegab)));
// Single Newton-Raphson update
omegab = omegab - Solve33(J, f);
// Back to world coordinates
Vec3 omega2 = Rotate(q, omegab);
return omega2;
}
76
Here is the implicit gyroscopic force solver. It uses body coordinates because I do not have the new world inertia tensor yet.
Capsule implicit solution
77
Here is the result of using implicit solution of the gyroscopic torque. I only used one Newton-Raphson iteration.
With rolling resistance
78
Adding rolling resistance makes the solution even better.
The Dzhanibekov Effect
79
Here is a video of the Dzhanibekov effect shown on the ISS. Similar effects can be seen in tennis rackets, white board
erasers, and mobile phones (when they slip out of our hands, that is why they are so hard to catch).
Explicit gyroscopic integration
80
I can the experiment using explicit Euler. Unfortunately the kinetic energy grows unbounded.
Dropping gyroscopic term
81
Here I dropped the gyroscopic term and you can see there is no tumbling motion.
Implicit solution
82
In this video, I have included the implicit gyroscopic solver using one Newton-Raphson iteration. The tumbling motion is
reproduced. You can see that the kinetic energy dissipates over time.

This sort of tumbling motion can make destruction and explosions look better in games, especially when the are long skinny
objects.
Conclusion: qualities of
numerical methods
• Convergence
• Accuracy
• Stability
83
There are few qualities of numerical methods that are used to find the best match for what our games need.

Convergence: as the time step gets smaller, the numerical result approaches the exact solution. For example, a numerical
method that always returns 42 is not convergent for a harmonic oscillator.

Accuracy: how well the solution matches the solution for a given time step. In games we can set the accuracy bar low. We
aren’t building satellites.

Stability: the numerical result grows unbounded even though the exact solution is unbounded
References
http://en.wikipedia.org/wiki/Semi-implicit_Euler_method
http://en.wikipedia.org/wiki/Euler's_equations_(rigid_body_dynamics)
https://fgiesen.wordpress.com/2012/08/24/quaternion-differentiation/
http://en.wikipedia.org/wiki/Tennis_racket_theorem
Stabilizing Gyroscopic Forces In Rigid Multibody Simulations, Claude Lacoursie`re
http://www.av8n.com/physics/symplectic-integrator.htm
box2d.org @erin_catto
84
Appendix
Here is stuff that didn’t fit into a one hour presentation.
Mass-Spring-Damper
m!!x + c!x + kx = 0
86
spring k
damper cground
x
mass m
It is also interesting to add damping to see the effect on the phase portrait.
Rewrite
m!!x + c!x + kx = 0
!!x + 2ζω !x +ω2
x = 0
ω =
k
m
ζ =
c
2 mk
angular frequency
damping ratio
87
We can reduced from 3 parameters (m,k,c) to 2 parameters (omega, zeta) by dividing through by the mass. The new
parameter zeta is called the damping ratio.

If zeta is zero, there is no damping. Increasing zeta increases damping. Once zeta reaches 1, the damping is so strong that
all oscillation is eliminated.
Solution
x = e−ζωt
C1 sinωdt + C2 cosωdt( )
ωd = ω 1−ζ 2
damped frequency
88
Here is the oscillatory solution to the mass-spring-damper. The constants C1 and C2 depend on the initial conditions.

When zeta is larger than one, then this solution no longer works. In that case the solution is just a decaying exponential.
Phase portrait
x0 = 1
v0 = 0
ω = 1
ζ = 0.1
position
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
velocity
-2
-1.5
-1
-0.5
0
0.5
1
1.5
2
start
89
Here is the phase portrait with a small amount of damping. As the time moves forward the position and velocity both decay
to zero until the mass comes to rest.
Phase portrait: velocity versus postion
90
Phase portrait animation for mass-spring-damper system
Similarity of explicit and implicit Euler
position
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
velocity
-2
-1.5
-1
-0.5
0
0.5
1
1.5
2
implicit
exact
start
position
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2velocity
-2
-1.5
-1
-0.5
0
0.5
1
1.5
2
explicit
exact
start
explicit implicit
91
The phase portrait shows that implicit Euler is stable. Also, implicit Euler is damped. So even though the mass should
oscillate forever, implicit Euler damps the motion and brings the mass to rest. I call this numerical damping.

If you compare this figure to explicit Euler, you should see that they look they look similar, except that explicit Euler is
growing. This is because implicit Euler gives the same result as running explicit Euler in reverse.
Duality of explicit and implicit Euler
xi+1 = xi + hfi xi−1 = xi − hfi
xi = xi−1 + hfi
explicit
reverse time
implicit
shift xi+1 = xi + hfi+1
rearrange
92
This shows that we can run time backwards on explicit Euler and get the same formula that is used for implicit Euler.
Taylor Series
x2 = x1 + h!x1 +
1
2
h2
!!x1 +
1
6
h3
!!!x1 +…
93
Finite difference formulas come from the Taylor Series, which states that any function can be extrapolated by using its
current value and derivatives. The more terms we use, the more accurate the extrapolation. If we include the first derivative
of x, we have first order accuracy. Including the second derivative gives second order accuracy, and so on. Notice that when
h is small, high order derivatives contribute decreasing amounts to the approximation.
Taylor Series: sine wave
x ≈ t
x ≈ t −
1
6
t3
x ≈ t −
1
6
t3
+
1
120
t5
x ≈ t −
1
6
t3
+
1
120
t5
−
1
5040
t7
x = sin(t) increasing
accuracy
Here is the Taylor Series approximation of the sine function with increasing accuracy as the number of terms increases. Note
that the approximations are polynomials in time.

The time could be just our time step h because we can start the Taylor Series extrapolation at any point of time.
Taylor Series: sine wave
95
Here is how the Taylor Series can approximate a sine wave with increasing order.
First Order Approximation
x2 ≈ x1 + h!x1 !x1 =
x2 − x1
h
truncated Taylor Series forward difference
rearrange
96
The first order approximation of the Taylor Series only keeps the first derivative. This is what I used already for the forward
difference.
Second Order
x2 ≈ x1 + h!x1 +
1
2
h2
!!x1
problem
97
Keeping the second derivative results in a second order approximation. I can use this to derive a second order finite
difference approximation. However, it may not be easy to compute the second derivative of x when x defined by a
differential equation. Fortunately, there is a trick for eliminating the second derivative.
Central Difference
x0 = x1 − h!x1 +
1
2
h2
!!x1
x2 = x1 + h!x1 +
1
2
h2
!!x1
x2 − x0 = 2h!x1
!x1 =
x2 − x0
2h
TS2
TS1
TS2 - TS1
98
Here I have two second order Taylor Series approximations. By subtracting the first equation from the second equation, I
can eliminate the second derivative. This results in the second order finite difference formula called the central difference.

This shows the power of the Taylor Series. You can combine more Taylor Series instances to generate ever more accurate
finite difference formulas. Although this is exciting, it is not really necessary for games. We can get by on first order accuracy
most of the time.
Central Difference
x
tt1
!x1 =
x2 − x0
2h
x0
x2
h h
99
Here’s how the central difference looks graphically. It really does look like a better approximation of the slope at t1.

More Related Content

What's hot

Ordinary differential equation
Ordinary differential equationOrdinary differential equation
Ordinary differential equation
JUGAL BORAH
 
Simultaneous differential equations
Simultaneous differential equationsSimultaneous differential equations
Simultaneous differential equationsShubhi Jain
 
Bounded variables new
Bounded variables newBounded variables new
Bounded variables newSamo Alwatan
 
Newton's forward difference
Newton's forward differenceNewton's forward difference
Newton's forward difference
Raj Parekh
 
Linear regression
Linear regressionLinear regression
Linear regression
Zoya Bylinskii
 
application of first order ordinary Differential equations
application of first order ordinary Differential equationsapplication of first order ordinary Differential equations
application of first order ordinary Differential equations
Emdadul Haque Milon
 
2 Dimensional Wave Equation Analytical and Numerical Solution
2 Dimensional Wave Equation Analytical and Numerical Solution2 Dimensional Wave Equation Analytical and Numerical Solution
2 Dimensional Wave Equation Analytical and Numerical Solution
Amr Mousa
 
Numerical
NumericalNumerical
Numerical1821986
 
Higher Differential Equation
Higher Differential EquationHigher Differential Equation
Higher Differential Equation
gtuautonomous
 
Lec1 01
Lec1 01Lec1 01
Partial
Partial Partial
Partial
Neha Bhogra
 
Engineering Mathematics-IV_B.Tech_Semester-IV_Unit-IV
Engineering Mathematics-IV_B.Tech_Semester-IV_Unit-IVEngineering Mathematics-IV_B.Tech_Semester-IV_Unit-IV
Engineering Mathematics-IV_B.Tech_Semester-IV_Unit-IV
Rai University
 
Numarical values highlighted
Numarical values highlightedNumarical values highlighted
Numarical values highlighted
AmanSaeed11
 
Application of Differential Equation
Application of Differential EquationApplication of Differential Equation
Application of Differential Equation
Tanzila Islam
 
Numarical values
Numarical valuesNumarical values
Numarical values
AmanSaeed11
 
Eigenvalue eigenvector slides
Eigenvalue eigenvector slidesEigenvalue eigenvector slides
Eigenvalue eigenvector slides
AmanSaeed11
 
Partial differentiation
Partial differentiationPartial differentiation
Partial differentiation
Tanuj Parikh
 
honey jose presentation
honey jose presentationhoney jose presentation
honey jose presentationHoney jose
 

What's hot (20)

Ordinary differential equation
Ordinary differential equationOrdinary differential equation
Ordinary differential equation
 
Simultaneous differential equations
Simultaneous differential equationsSimultaneous differential equations
Simultaneous differential equations
 
Bounded variables new
Bounded variables newBounded variables new
Bounded variables new
 
Newton's forward difference
Newton's forward differenceNewton's forward difference
Newton's forward difference
 
Linear regression
Linear regressionLinear regression
Linear regression
 
application of first order ordinary Differential equations
application of first order ordinary Differential equationsapplication of first order ordinary Differential equations
application of first order ordinary Differential equations
 
2 Dimensional Wave Equation Analytical and Numerical Solution
2 Dimensional Wave Equation Analytical and Numerical Solution2 Dimensional Wave Equation Analytical and Numerical Solution
2 Dimensional Wave Equation Analytical and Numerical Solution
 
Numerical
NumericalNumerical
Numerical
 
Higher Differential Equation
Higher Differential EquationHigher Differential Equation
Higher Differential Equation
 
Lec1 01
Lec1 01Lec1 01
Lec1 01
 
Partial
Partial Partial
Partial
 
Engineering Mathematics-IV_B.Tech_Semester-IV_Unit-IV
Engineering Mathematics-IV_B.Tech_Semester-IV_Unit-IVEngineering Mathematics-IV_B.Tech_Semester-IV_Unit-IV
Engineering Mathematics-IV_B.Tech_Semester-IV_Unit-IV
 
Numarical values highlighted
Numarical values highlightedNumarical values highlighted
Numarical values highlighted
 
Application of Differential Equation
Application of Differential EquationApplication of Differential Equation
Application of Differential Equation
 
Numarical values
Numarical valuesNumarical values
Numarical values
 
Eigenvalue eigenvector slides
Eigenvalue eigenvector slidesEigenvalue eigenvector slides
Eigenvalue eigenvector slides
 
Partial differentiation
Partial differentiationPartial differentiation
Partial differentiation
 
honey jose presentation
honey jose presentationhoney jose presentation
honey jose presentation
 
patel
patelpatel
patel
 
Bounded var
Bounded varBounded var
Bounded var
 

Viewers also liked

The Youtube of Dutch digital publications - one national platform for full-te...
The Youtube of Dutch digital publications - one national platform for full-te...The Youtube of Dutch digital publications - one national platform for full-te...
The Youtube of Dutch digital publications - one national platform for full-te...
Olaf Janssen
 
Using mobile phones to enhance interaction in didactic teaching approaches
Using mobile phones to enhance interaction in didactic teaching approaches Using mobile phones to enhance interaction in didactic teaching approaches
Using mobile phones to enhance interaction in didactic teaching approaches
Dick Ng'ambi
 
Coke Live Fresh Noise 2007
Coke Live Fresh Noise 2007Coke Live Fresh Noise 2007
Coke Live Fresh Noise 2007
Next
 
Comm Network10 16 09v2
Comm Network10 16 09v2Comm Network10 16 09v2
Comm Network10 16 09v2
Northwest Area Foundation
 
Commercial Property Refinancing
Commercial Property RefinancingCommercial Property Refinancing
Commercial Property Refinancing
Capital Line Commercial Financing
 
The dangers of payday loans
The dangers of payday loansThe dangers of payday loans
The dangers of payday loans
American Debt Counseling Inc
 
A Journey To The Stars
A Journey To The StarsA Journey To The Stars
A Journey To The Starsamr hassaan
 
Customer service 2012 bfit
Customer service 2012 bfitCustomer service 2012 bfit
Customer service 2012 bfitSuzanne Markham
 
Speaking notes nov13_2010
Speaking notes  nov13_2010Speaking notes  nov13_2010
Speaking notes nov13_2010jamesgarofalo
 
2001 Pacifica Foundation Timeline
2001 Pacifica Foundation Timeline2001 Pacifica Foundation Timeline
2001 Pacifica Foundation Timeline
Paul DeRienzo
 
Ih2010 future
Ih2010 futureIh2010 future
Ih2010 future
Konrad Berenyi
 
You Can Dance Konkurs W Kreacji 2007
You Can Dance Konkurs W Kreacji 2007You Can Dance Konkurs W Kreacji 2007
You Can Dance Konkurs W Kreacji 2007
Next
 
Vijay Mewada June 11
Vijay Mewada June 11Vijay Mewada June 11
Vijay Mewada June 11vsa177
 
Las Hayas Hotel en Fotos
Las Hayas Hotel en FotosLas Hayas Hotel en Fotos
Las Hayas Hotel en Fotos
Las Hayas
 

Viewers also liked (20)

The Youtube of Dutch digital publications - one national platform for full-te...
The Youtube of Dutch digital publications - one national platform for full-te...The Youtube of Dutch digital publications - one national platform for full-te...
The Youtube of Dutch digital publications - one national platform for full-te...
 
Using mobile phones to enhance interaction in didactic teaching approaches
Using mobile phones to enhance interaction in didactic teaching approaches Using mobile phones to enhance interaction in didactic teaching approaches
Using mobile phones to enhance interaction in didactic teaching approaches
 
Coke Live Fresh Noise 2007
Coke Live Fresh Noise 2007Coke Live Fresh Noise 2007
Coke Live Fresh Noise 2007
 
Comm Network10 16 09v2
Comm Network10 16 09v2Comm Network10 16 09v2
Comm Network10 16 09v2
 
Awakush
AwakushAwakush
Awakush
 
Commercial Property Refinancing
Commercial Property RefinancingCommercial Property Refinancing
Commercial Property Refinancing
 
The dangers of payday loans
The dangers of payday loansThe dangers of payday loans
The dangers of payday loans
 
Band
BandBand
Band
 
A Journey To The Stars
A Journey To The StarsA Journey To The Stars
A Journey To The Stars
 
Customer service 2012 bfit
Customer service 2012 bfitCustomer service 2012 bfit
Customer service 2012 bfit
 
Speaking notes nov13_2010
Speaking notes  nov13_2010Speaking notes  nov13_2010
Speaking notes nov13_2010
 
How to use an atm
How to use an atmHow to use an atm
How to use an atm
 
my first time
my first timemy first time
my first time
 
2001 Pacifica Foundation Timeline
2001 Pacifica Foundation Timeline2001 Pacifica Foundation Timeline
2001 Pacifica Foundation Timeline
 
Anschp36
Anschp36Anschp36
Anschp36
 
Anschp29
Anschp29Anschp29
Anschp29
 
Ih2010 future
Ih2010 futureIh2010 future
Ih2010 future
 
You Can Dance Konkurs W Kreacji 2007
You Can Dance Konkurs W Kreacji 2007You Can Dance Konkurs W Kreacji 2007
You Can Dance Konkurs W Kreacji 2007
 
Vijay Mewada June 11
Vijay Mewada June 11Vijay Mewada June 11
Vijay Mewada June 11
 
Las Hayas Hotel en Fotos
Las Hayas Hotel en FotosLas Hayas Hotel en Fotos
Las Hayas Hotel en Fotos
 

Similar to Erin catto numericalmethods

What are free particles in quantum mechanics
What are free particles in quantum mechanicsWhat are free particles in quantum mechanics
What are free particles in quantum mechanics
bhaskar chatterjee
 
Lesson all about Derivatives PPT (with examples)
Lesson all about Derivatives PPT (with examples)Lesson all about Derivatives PPT (with examples)
Lesson all about Derivatives PPT (with examples)
DorothyJuliaDelaCruz
 
DerivativesXP.ppt
DerivativesXP.pptDerivativesXP.ppt
DerivativesXP.ppt
SnehSinha6
 
Derivatie class 12
Derivatie class 12Derivatie class 12
Derivatie class 12
Sadiq Hussain
 
On Application of Power Series Solution of Bessel Problems to the Problems of...
On Application of Power Series Solution of Bessel Problems to the Problems of...On Application of Power Series Solution of Bessel Problems to the Problems of...
On Application of Power Series Solution of Bessel Problems to the Problems of...
BRNSS Publication Hub
 
Continuity and differentiability
Continuity and differentiability Continuity and differentiability
Continuity and differentiability
Seyid Kadher
 
Derivative rules.docx
Derivative rules.docxDerivative rules.docx
Derivative rules.docx
Rose Mary Tania Arini
 
Graphs of the Sine and Cosine Functions Lecture
Graphs of the Sine and Cosine Functions LectureGraphs of the Sine and Cosine Functions Lecture
Graphs of the Sine and Cosine Functions Lecture
Froyd Wess
 
Week 6
Week 6Week 6
Week 6
EasyStudy3
 
Mathematical blog #1
Mathematical blog #1Mathematical blog #1
Mathematical blog #1
Steven Pauly
 
Week 7
Week 7Week 7
Week 7
EasyStudy3
 
Statistical Method In Economics
Statistical Method In EconomicsStatistical Method In Economics
Statistical Method In Economics
Economics Homework Helper
 
160280102051 c3 aem
160280102051 c3 aem160280102051 c3 aem
160280102051 c3 aem
L.D. COLLEGE OF ENGINEERING
 
Calculus the way to do it | Free Sample eBook | Mathslearning.com | Mathemati...
Calculus the way to do it | Free Sample eBook | Mathslearning.com | Mathemati...Calculus the way to do it | Free Sample eBook | Mathslearning.com | Mathemati...
Calculus the way to do it | Free Sample eBook | Mathslearning.com | Mathemati...
Mathslearning.com
 
Project in Calcu
Project in CalcuProject in Calcu
Project in Calcupatrickpaz
 
Applications of Differential Calculus in real life
Applications of Differential Calculus in real life Applications of Differential Calculus in real life
Applications of Differential Calculus in real life
OlooPundit
 

Similar to Erin catto numericalmethods (20)

What are free particles in quantum mechanics
What are free particles in quantum mechanicsWhat are free particles in quantum mechanics
What are free particles in quantum mechanics
 
Lesson all about Derivatives PPT (with examples)
Lesson all about Derivatives PPT (with examples)Lesson all about Derivatives PPT (with examples)
Lesson all about Derivatives PPT (with examples)
 
DerivativesXP.ppt
DerivativesXP.pptDerivativesXP.ppt
DerivativesXP.ppt
 
Derivatie class 12
Derivatie class 12Derivatie class 12
Derivatie class 12
 
On Application of Power Series Solution of Bessel Problems to the Problems of...
On Application of Power Series Solution of Bessel Problems to the Problems of...On Application of Power Series Solution of Bessel Problems to the Problems of...
On Application of Power Series Solution of Bessel Problems to the Problems of...
 
03_AJMS_209_19_RA.pdf
03_AJMS_209_19_RA.pdf03_AJMS_209_19_RA.pdf
03_AJMS_209_19_RA.pdf
 
03_AJMS_209_19_RA.pdf
03_AJMS_209_19_RA.pdf03_AJMS_209_19_RA.pdf
03_AJMS_209_19_RA.pdf
 
Continuity and differentiability
Continuity and differentiability Continuity and differentiability
Continuity and differentiability
 
Derivative rules.docx
Derivative rules.docxDerivative rules.docx
Derivative rules.docx
 
Graphs of the Sine and Cosine Functions Lecture
Graphs of the Sine and Cosine Functions LectureGraphs of the Sine and Cosine Functions Lecture
Graphs of the Sine and Cosine Functions Lecture
 
Week 6
Week 6Week 6
Week 6
 
Mathematical blog #1
Mathematical blog #1Mathematical blog #1
Mathematical blog #1
 
Week 7
Week 7Week 7
Week 7
 
Statistical Method In Economics
Statistical Method In EconomicsStatistical Method In Economics
Statistical Method In Economics
 
160280102051 c3 aem
160280102051 c3 aem160280102051 c3 aem
160280102051 c3 aem
 
Diffy Q Paper
Diffy Q PaperDiffy Q Paper
Diffy Q Paper
 
Cs jog
Cs jogCs jog
Cs jog
 
Calculus the way to do it | Free Sample eBook | Mathslearning.com | Mathemati...
Calculus the way to do it | Free Sample eBook | Mathslearning.com | Mathemati...Calculus the way to do it | Free Sample eBook | Mathslearning.com | Mathemati...
Calculus the way to do it | Free Sample eBook | Mathslearning.com | Mathemati...
 
Project in Calcu
Project in CalcuProject in Calcu
Project in Calcu
 
Applications of Differential Calculus in real life
Applications of Differential Calculus in real life Applications of Differential Calculus in real life
Applications of Differential Calculus in real life
 

Recently uploaded

一比一原版(DU毕业证)迪肯大学毕业证成绩单
一比一原版(DU毕业证)迪肯大学毕业证成绩单一比一原版(DU毕业证)迪肯大学毕业证成绩单
一比一原版(DU毕业证)迪肯大学毕业证成绩单
zvaywau
 
Memory Rental Store - The Ending(Storyboard)
Memory Rental Store - The Ending(Storyboard)Memory Rental Store - The Ending(Storyboard)
Memory Rental Store - The Ending(Storyboard)
SuryaKalyan3
 
一比一原版(GU毕业证)格里菲斯大学毕业证成绩单
一比一原版(GU毕业证)格里菲斯大学毕业证成绩单一比一原版(GU毕业证)格里菲斯大学毕业证成绩单
一比一原版(GU毕业证)格里菲斯大学毕业证成绩单
zvaywau
 
Memory Rental Store - The Chase (Storyboard)
Memory Rental Store - The Chase (Storyboard)Memory Rental Store - The Chase (Storyboard)
Memory Rental Store - The Chase (Storyboard)
SuryaKalyan3
 
A Brief Introduction About Hadj Ounis
A Brief  Introduction  About  Hadj OunisA Brief  Introduction  About  Hadj Ounis
A Brief Introduction About Hadj Ounis
Hadj Ounis
 
2137ad - Characters that live in Merindol and are at the center of main stories
2137ad - Characters that live in Merindol and are at the center of main stories2137ad - Characters that live in Merindol and are at the center of main stories
2137ad - Characters that live in Merindol and are at the center of main stories
luforfor
 
一比一原版(UniSA毕业证)南澳大学毕业证成绩单如何办理
一比一原版(UniSA毕业证)南澳大学毕业证成绩单如何办理一比一原版(UniSA毕业证)南澳大学毕业证成绩单如何办理
一比一原版(UniSA毕业证)南澳大学毕业证成绩单如何办理
zeyhe
 
Caffeinated Pitch Bible- developed by Claire Wilson
Caffeinated Pitch Bible- developed by Claire WilsonCaffeinated Pitch Bible- developed by Claire Wilson
Caffeinated Pitch Bible- developed by Claire Wilson
ClaireWilson398082
 
ashokathegreat project class 12 presentation
ashokathegreat project class 12 presentationashokathegreat project class 12 presentation
ashokathegreat project class 12 presentation
aditiyad2020
 
The Last Polymath: Muntadher Saleh‎‎‎‎‎‎‎‎‎‎‎‎
The Last Polymath: Muntadher Saleh‎‎‎‎‎‎‎‎‎‎‎‎The Last Polymath: Muntadher Saleh‎‎‎‎‎‎‎‎‎‎‎‎
The Last Polymath: Muntadher Saleh‎‎‎‎‎‎‎‎‎‎‎‎
iraqartsandculture
 
Codes n Conventionss copy (2).pptx new new
Codes n Conventionss copy (2).pptx new newCodes n Conventionss copy (2).pptx new new
Codes n Conventionss copy (2).pptx new new
ZackSpencer3
 
Fed by curiosity and beauty - Remembering Myrsine Zorba
Fed by curiosity and beauty - Remembering Myrsine ZorbaFed by curiosity and beauty - Remembering Myrsine Zorba
Fed by curiosity and beauty - Remembering Myrsine Zorba
mariavlachoupt
 
一比一原版(qut毕业证)昆士兰科技大学毕业证如何办理
一比一原版(qut毕业证)昆士兰科技大学毕业证如何办理一比一原版(qut毕业证)昆士兰科技大学毕业证如何办理
一比一原版(qut毕业证)昆士兰科技大学毕业证如何办理
taqyed
 
Inter-Dimensional Girl Boards Segment (Act 3)
Inter-Dimensional Girl Boards Segment (Act 3)Inter-Dimensional Girl Boards Segment (Act 3)
Inter-Dimensional Girl Boards Segment (Act 3)
CristianMestre
 
acting board rough title here lolaaaaaaa
acting board rough title here lolaaaaaaaacting board rough title here lolaaaaaaa
acting board rough title here lolaaaaaaa
angelicafronda7
 
一比一原版(QUT毕业证)昆士兰科技大学毕业证成绩单如何办理
一比一原版(QUT毕业证)昆士兰科技大学毕业证成绩单如何办理一比一原版(QUT毕业证)昆士兰科技大学毕业证成绩单如何办理
一比一原版(QUT毕业证)昆士兰科技大学毕业证成绩单如何办理
zeyhe
 
ART FORMS OF KERALA: TRADITIONAL AND OTHERS
ART FORMS OF KERALA: TRADITIONAL AND OTHERSART FORMS OF KERALA: TRADITIONAL AND OTHERS
ART FORMS OF KERALA: TRADITIONAL AND OTHERS
Sandhya J.Nair
 
2137ad Merindol Colony Interiors where refugee try to build a seemengly norm...
2137ad  Merindol Colony Interiors where refugee try to build a seemengly norm...2137ad  Merindol Colony Interiors where refugee try to build a seemengly norm...
2137ad Merindol Colony Interiors where refugee try to build a seemengly norm...
luforfor
 
IrishWritersCtrsPersonalEssaysMay29.pptx
IrishWritersCtrsPersonalEssaysMay29.pptxIrishWritersCtrsPersonalEssaysMay29.pptx
IrishWritersCtrsPersonalEssaysMay29.pptx
Aine Greaney Ellrott
 

Recently uploaded (19)

一比一原版(DU毕业证)迪肯大学毕业证成绩单
一比一原版(DU毕业证)迪肯大学毕业证成绩单一比一原版(DU毕业证)迪肯大学毕业证成绩单
一比一原版(DU毕业证)迪肯大学毕业证成绩单
 
Memory Rental Store - The Ending(Storyboard)
Memory Rental Store - The Ending(Storyboard)Memory Rental Store - The Ending(Storyboard)
Memory Rental Store - The Ending(Storyboard)
 
一比一原版(GU毕业证)格里菲斯大学毕业证成绩单
一比一原版(GU毕业证)格里菲斯大学毕业证成绩单一比一原版(GU毕业证)格里菲斯大学毕业证成绩单
一比一原版(GU毕业证)格里菲斯大学毕业证成绩单
 
Memory Rental Store - The Chase (Storyboard)
Memory Rental Store - The Chase (Storyboard)Memory Rental Store - The Chase (Storyboard)
Memory Rental Store - The Chase (Storyboard)
 
A Brief Introduction About Hadj Ounis
A Brief  Introduction  About  Hadj OunisA Brief  Introduction  About  Hadj Ounis
A Brief Introduction About Hadj Ounis
 
2137ad - Characters that live in Merindol and are at the center of main stories
2137ad - Characters that live in Merindol and are at the center of main stories2137ad - Characters that live in Merindol and are at the center of main stories
2137ad - Characters that live in Merindol and are at the center of main stories
 
一比一原版(UniSA毕业证)南澳大学毕业证成绩单如何办理
一比一原版(UniSA毕业证)南澳大学毕业证成绩单如何办理一比一原版(UniSA毕业证)南澳大学毕业证成绩单如何办理
一比一原版(UniSA毕业证)南澳大学毕业证成绩单如何办理
 
Caffeinated Pitch Bible- developed by Claire Wilson
Caffeinated Pitch Bible- developed by Claire WilsonCaffeinated Pitch Bible- developed by Claire Wilson
Caffeinated Pitch Bible- developed by Claire Wilson
 
ashokathegreat project class 12 presentation
ashokathegreat project class 12 presentationashokathegreat project class 12 presentation
ashokathegreat project class 12 presentation
 
The Last Polymath: Muntadher Saleh‎‎‎‎‎‎‎‎‎‎‎‎
The Last Polymath: Muntadher Saleh‎‎‎‎‎‎‎‎‎‎‎‎The Last Polymath: Muntadher Saleh‎‎‎‎‎‎‎‎‎‎‎‎
The Last Polymath: Muntadher Saleh‎‎‎‎‎‎‎‎‎‎‎‎
 
Codes n Conventionss copy (2).pptx new new
Codes n Conventionss copy (2).pptx new newCodes n Conventionss copy (2).pptx new new
Codes n Conventionss copy (2).pptx new new
 
Fed by curiosity and beauty - Remembering Myrsine Zorba
Fed by curiosity and beauty - Remembering Myrsine ZorbaFed by curiosity and beauty - Remembering Myrsine Zorba
Fed by curiosity and beauty - Remembering Myrsine Zorba
 
一比一原版(qut毕业证)昆士兰科技大学毕业证如何办理
一比一原版(qut毕业证)昆士兰科技大学毕业证如何办理一比一原版(qut毕业证)昆士兰科技大学毕业证如何办理
一比一原版(qut毕业证)昆士兰科技大学毕业证如何办理
 
Inter-Dimensional Girl Boards Segment (Act 3)
Inter-Dimensional Girl Boards Segment (Act 3)Inter-Dimensional Girl Boards Segment (Act 3)
Inter-Dimensional Girl Boards Segment (Act 3)
 
acting board rough title here lolaaaaaaa
acting board rough title here lolaaaaaaaacting board rough title here lolaaaaaaa
acting board rough title here lolaaaaaaa
 
一比一原版(QUT毕业证)昆士兰科技大学毕业证成绩单如何办理
一比一原版(QUT毕业证)昆士兰科技大学毕业证成绩单如何办理一比一原版(QUT毕业证)昆士兰科技大学毕业证成绩单如何办理
一比一原版(QUT毕业证)昆士兰科技大学毕业证成绩单如何办理
 
ART FORMS OF KERALA: TRADITIONAL AND OTHERS
ART FORMS OF KERALA: TRADITIONAL AND OTHERSART FORMS OF KERALA: TRADITIONAL AND OTHERS
ART FORMS OF KERALA: TRADITIONAL AND OTHERS
 
2137ad Merindol Colony Interiors where refugee try to build a seemengly norm...
2137ad  Merindol Colony Interiors where refugee try to build a seemengly norm...2137ad  Merindol Colony Interiors where refugee try to build a seemengly norm...
2137ad Merindol Colony Interiors where refugee try to build a seemengly norm...
 
IrishWritersCtrsPersonalEssaysMay29.pptx
IrishWritersCtrsPersonalEssaysMay29.pptxIrishWritersCtrsPersonalEssaysMay29.pptx
IrishWritersCtrsPersonalEssaysMay29.pptx
 

Erin catto numericalmethods

  • 1. Numerical Methods Erin Catto Blizzard Entertainment Sometimes the mathematical problems we are faced with in game physics are too difficult to solve exactly. Numerical methods provide a set of tools to get approximate solutions to these difficult problems. In particular, numerical methods help us get approximate solutions to the differential equations that arise in rigid body and point mass physics. There are many numerical methods available and it isn’t always clear which method to use in which situation. What is the right performance versus accuracy trade-off? Will my simulation blow up? What are the common methods used in game physics? I hope this presentation helps you better understand numerical methods and their application.
  • 2. First, a little review … Differential Calculus 2
  • 3. The derivative of a function is the slope f x( ) f x + h( ) x x + h df dx = lim h→0 f x + h( )− f x( ) hsecant 3 The derivative of a function f(x) is the slope at position x. The derivative is defined as the limit of the secant as the increment h goes to zero.
  • 4. Line: the derivative is constant x 0 2 4 6 8 10 y 0 1 2 3 4 5 6 7 8 9 10 run = 2 rise = 1 y-intercept b = 1 slope m = rise/run = 0.5 y = mx + bfunction: dy dx = mderivative: 4 The derivative of a line is the slope, which is the rise over the run. If the line is horizontal, y is constant and the derivative are zero. In other words, y does not change with x.
  • 5. Parabola: the derivative is a line y = ax2 function: dy dx = 2axderivative: x -1 -0.5 0 0.5 1 y 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 tangent 5 We can also think of the derivative as the tangent. It is the slope at the current point on the curve. In general the slope changes with the x value.
  • 6. Exponential: the derivative is another exponential y = eax dy dx = aeax x -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 y 0 0.5 1 1.5 2 2.5 3 3.5 4 function: derivative: 6 The exponential shows up a lot in physics especially when there is damping. It has the neat property that its derivative is another exponential.
  • 7. 7 Now a bit of Pokemon math.
  • 8. Sine wave: the derivative is another sine wave y = sin x dy dx = cosx x -10 -5 0 5 10y -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 sine cosine function: derivative: 8 The derivative of a sine wave is a cosine wave.
  • 9. Functions of time: the derivative is the rate of change !x = dx dt !!x = d2 x dt2 velocity: acceleration: 9 In physics we usually deal with functions of time. If x is position, then x dot is velocity and x double dot is acceleration. I’ll be using this dot notation a lot.
  • 10. More review: differential equations I have the slope I want the original function 10 I’ve showed a few examples of you can compute a derivative given a function. What if we have the derivative and want to get the function back?
  • 11. A differential equation is a formula for the slope !x = f (t,x) 11 (first order differential equation) This is the standard form of a first order differential equation in time.
  • 12. Solving the differential equation means getting the original function !x = f (t,x) x(t)solve 12 Solving a differential equation means getting the original function back. If our function f depends only on t, we only need to perform integration. However, if f depends on x, then solution becomes more challenging.
  • 13. The simplest differential equation !x = 0 x = x0 solution: (constant) 13 So lets start solving some differential equations! Here’s is the simplest differential equation I could find and its solution.
  • 14. Another differential equation !x = a x = at + x0 solution: 14 This is just a line.
  • 15. What if x is on the right? !x = ax x = x0eat solution: 15 d dt (x0eat ) = ax0eat = a x0eat ( )= axproof: The exponential is needed to solve this differential equation. Remember that the derivative of an exponential is another exponential.
  • 16. Newton’s 2nd Law ma = F m!!x = F or 16 (second order differential equation) Newton’s law is a differential equation for the acceleration. Because it involves the second derivative, it is a second order differential equation.
  • 17. Gravity m!!y = −mg 17 m F=-mg y Under gravity the acceleration is constant.
  • 18. Solution !!y = −g !y = v0 − gt y = y0 + v0t − 1 2 gt2 18 Solving for a constant force just requires the integration (anti-derivative). I’m also accounting for the initial position y0 and velocity v0.
  • 19. Parabolic motion This is why projectiles follow a parabolic path.
  • 20. Mass Spring m!!x + kx = 0 20 spring k ground x mass m The mass spring system has a force that depends on position. The more the spring is stretched, the harder it pulls.
  • 21.
  • 22. Angular frequency ω = k m radians per second !!x +ω2 x = 0 22 I can divide through by the mass to go from two parameters (m,k) to one parameter (omega). Omega is the angular frequency of the mass-spring system. Notice that the angular frequency increases with the spring stiffness and decreases with the mass.
  • 23. Solution x = v0 ω sinωt + x0 cosωt 23 The solution is a sine wave with angular frequency omega. We need to combine a sine wave and cosine wave to account for initial conditions.
  • 24. Position and velocity versus time time 0 2 4 6 8 10 12 14 16 18 20 position -1 -0.5 0 0.5 1 time 0 2 4 6 8 10 12 14 16 18 20 velocity -1 -0.5 0 0.5 1 x0 = 1 v0 = 0 ω = 1 24 These graphs show position and velocity versus time. Notice that position and velocity are 90 degrees out of phase. So the position is a cosine wave and the velocity is a sine wave.
  • 25. Phase Portrait: velocity versus postion position -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 velocity -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 start x0 = 1 v0 = 0 ω = 1 25 A nice way to visualize the oscillation is to plot velocity versus position. You can view this as a parametric curve with time as the curve parameter. This is drawn here as a unit circle, but keep in mind that position and velocity have different units. The arrows show the direction of increasing time. The position and velocity start at the green circle and progressing clockwise around the circle. And the state continues around the circle forever. The angular frequency controls how fast the state goes around the circle as time moves forward. This graph is sometimes called the phase plot or phase portrait. This plot shows all possible states (phases) of the system as time goes forward.
  • 26. Phase portrait: velocity versus postion 26 This animation shows position and velocity versus time along with the phase portrait.
  • 27. System of differential equations solution??? 27 So far I have showed some simple physical systems, such as a projectile and a mass-spring system. The simulations in modern games are more complex. Often there are multiple rigid bodies colliding with friction. This means that there are multiple mutually dependent differential equations. We have little hope of finding the exact solutions for these complex systems of differential equations. Therefore we need to turn to numerical methods.
  • 28. Numerical solution • Sacrifice accuracy • Time stepping • Generic simulation 28 We can simulate systems of differential equations using numerical methods. This means we are abandoning exact answers. But we are making games, so this is okay. You will see that numerical solution works well with typical game loops. Numerical solution also creates a nice abstraction that allows us to solve differential equations in a generic manner. These methods can handle any number of rigid bodies and any number of forces (as memory and processor power allow).
  • 29. Generic differential equation !x = f t,x( )x = x1 ! xn ⎧ ⎨ ⎪ ⎩ ⎪ ⎫ ⎬ ⎪ ⎭ ⎪ first order system of differential equations 29 Numerical methods like to have differential equations in a standard form. We can have multiple differential equations assembled into an array. That way we can solve them all at once. This also allows us to account for objects that are interacting and have mutually dependent solutions, such as stacking of rigid bodies.
  • 30. Handling second order differential equations m!!x = Fhave: !x = f x( )want: 30 I will be dealing with Newton’s law and I need a way to get this into the first order form needed by numerical methods.
  • 31. Split into two differential equations !v = F m !x = v !v !x ⎡ ⎣ ⎢ ⎤ ⎦ ⎥ = F m v ⎡ ⎣ ⎢ ⎢ ⎢ ⎤ ⎦ ⎥ ⎥ ⎥ group !x f x( ) 31 The idea is to convert a single second order differential equation into two first order differential equations. This is accomplished by introducing the velocity as an independent state. Then I get a first order DE for the velocity and a first order DE for the position.
  • 32. Finite Difference dx dt ≈ x2 − x1 h forward difference x tt1 x1 x2 h 32 The basic approach of numerical solution is to use the finite difference. This allows me to convert a differential equation into an algebraic equation. There are many kinds of finite difference formulas. This one is the forward difference. It approximates the derivative as the next value x2 minus the current value x1 over the time step h. You can see by the green line that this really is an approximation of the slope at x1. The approximation gets better by decreasing the time step h. However, taking smaller time steps is more expensive because you need to take more of them.
  • 33. Backwards difference x tt1 x0 x1 dx dt ≈ x1 − x0 h h 33 We can also flip things around and approximate the derivative based on the previous value. This is called the backwards difference.
  • 34. Numerical solution via explicit Euler !x = f t,x( ) x2 − x1 h = f t1,x1( )forward difference x2 = x1 + hf t1,x1( ) explicit Euler 34 Lets take our standard first order differential equation and apply the forward difference formula to remove the derivative. Notice that this converts the differential equation into an algebraic equation. This leads to the explicit Euler method: we can simply plug in the current value for x1 and t1 to compute x2.
  • 35. Explicit Euler for Newton’s law v2 = v1 + h F1 m x2 = x1 + hv1 35 I can apply explicit Euler to Newton’s law. Velocity is extrapolated based on the current force and position is extrapolated based on the current velocity.
  • 36. Pseudocode struct State { float t, x, v; } State ExplicitEuler(float h, State state1) { float force = ComputeForce(state1); State state2; state2.t = state1.t + h; state2.v = state1.v + h * force / mass; state2.x = state1.x + h * state1.v; return state2; } 36
  • 37. Explicit Euler for mass-spring system v2 = v1 − hω2 x1 x2 = x1 + hv1 37 Here is explicit Euler for the mass-spring system. So here omega is the angular frequency in radians per second. Notice how explicit Euler gives us a way to simulate an oscillation without using trig functions.
  • 38. Explicit Euler phase portrait x0 = 1 v0 = 0 ω = 1 h = 0.1 position -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 velocity -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 explicit exact start 38 Here is a Matlab simulation of the mass-spring system using explicit Euler. As before it is a plot of velocity versus position. The exact solution is a circle. But explicit Euler is diverging and the amplitude is growing unbounded as time progresses.
  • 39. Explicit Euler is extrapolation 39 t 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 1 2 3 4 5 6 7 8 et 1+t error Explicit Euler is essentially extrapolation. Intuitively extrapolation is bad. If the function is rapidly changing, the extrapolation has poor accuracy and can lead to a numerical instability.
  • 40. Stability of explicit Euler v2 x2 ⎡ ⎣ ⎢ ⎢ ⎤ ⎦ ⎥ ⎥ = v1 x1 ⎡ ⎣ ⎢ ⎢ ⎤ ⎦ ⎥ ⎥ + h −ω2 x1 v1 ⎡ ⎣ ⎢ ⎢ ⎤ ⎦ ⎥ ⎥ v2 x2 ⎡ ⎣ ⎢ ⎢ ⎤ ⎦ ⎥ ⎥ = 1 −hω2 h 1 ⎡ ⎣ ⎢ ⎤ ⎦ ⎥ v1 x1 ⎡ ⎣ ⎢ ⎢ ⎤ ⎦ ⎥ ⎥ x2 = Ax1 40 The Matlab simulation showed that explicit Euler is unstable. We can also understand the instability by looking at the explicit Euler equations in detail. For the mass-spring system, the time stepping process is equivalent to multiplying the previous state vector by a matrix A.
  • 41. Stability is determined by growth x2 = Ax1 x3 = A2 x1 ! xn = An−1 x1 A ≤1stable if: 41 As time marches forward, I am just multiply the previous state by the same matrix over and over. The magnitude of this matrix must be less than one, otherwise the solution will blow up.
  • 42. Use eigenvalues to determine the magnitude of the matrix Au = λu eigenvalue eigenvector A = λmax 42 The magnitude of a matrix can be represent by its eigenvalue lambda, which is a scalar. Please see Wikipedia for more information on eigenvalues.
  • 43. Use the determinant to compute the eigenvalue λI − A( )u = 0 det λI − A( )= 0 43 I can compute the eigenvalue of a matrix using the determinant.
  • 44. Eigenvalue for explicit Euler det λ 1 0 0 1 ⎡ ⎣ ⎢ ⎤ ⎦ ⎥ − 1 −hω2 h 1 ⎡ ⎣ ⎢ ⎤ ⎦ ⎥ ⎛ ⎝ ⎜ ⎞ ⎠ ⎟ = 0 λ2 − 2λ +1+ h2 ω2 = 0 characteristic polynomial 44 The determinant produces a polynomial in lambda. This is called the characteristic polynomial. You can compute the roots of the polynomial to determine the values of lambda. In this case there are two values of lambda, but I just care about the value with the largest magnitude.
  • 45. Eigenvalue for explicit Euler λ = 1± ihω λ = 1+ h2 ω2 λ ≥1 (unstable if h and omega > 0) 45 The eigenvalue is a complex number, but its magnitude is greater than one if the spring stiffness is positive. This shows that explicit Euler is unstable. You can get explicit Euler to be stable by adding some damping, but as you will see, there are better alternatives.
  • 46. Numerical solution via implicit Euler !x = f t,x( ) x1 − x0 h = f x1( )backwards difference x2 = x1 + hf x2( ) 46 Now lets use the backwards difference instead of the forward difference. After shifting the indices, this leads to an implicit function for x2 and the method is called implicit Euler. To actually compute x2 we have to solve an algebraic equation. It may be nonlinear, so we may have to use numerical root finding, such as the Newton-Raphson method.
  • 47. Implicit Euler for mass-spring system v2 = v1 − hω2 x2 x2 = x1 + hv2 47 Here is implicit Euler for the mass-spring system. Notice that x2 and v2 appear on both sides of the formulas. This means I have to solve for x2 and v2 to advance time. I can solve these equations easily since they are linear and only have two variables. Larger simulations and non-linearities can make implicit Euler much more challenging to implement.
  • 48. Implicit Euler phase portrait position -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 velocity -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 implicit exact start x0 = 1 v0 = 0 ω = 1 h = 0.1 48 The phase portrait shows that implicit Euler is stable. Also, implicit Euler is damped. So even though the mass should oscillate forever, implicit Euler damps the motion and brings the mass to rest. I call this numerical damping. If you compare this figure to explicit Euler, you should see that they look they look similar, except that explicit Euler is growing. This is because implicit Euler gives the same result as running explicit Euler in reverse.
  • 49. Implicit Euler eigenvalue λ = 1 1+ h2 ω2 λ ≤1 (always stable) 49 I performed a similar computation for implicit Euler and the result shows that implicit Euler is stable for any positive value of omega.
  • 50. Semi-implicit Euler: cheap and stable v2 = v1 + h F1 m x2 = x1 + hv1 v2 = v1 + h F1 m x2 = x1 + hv2 Explicit Semi-implicit 50 Explicit Euler is efficient but has poor stability. Implicit Euler has great stability but can be expensive. There is a 3rd method that strikes a balance between these two methods, giving good stability and performance. If I use the current velocity in the position update, then explicit Euler becomes semi-implicit Euler. This small difference makes a huge difference in stability. Yet the computational cost is just as good as explicit Euler. Semi-implicit Euler is often called symplectic Euler because it has some good energy conservation properties. You’ll see that shortly.
  • 51. λ2 + h2 ω2 − 2( )λ +1= 0 λ = 1 if hω ≤ 2 Semi-implicit Euler eigenvalue characteristic polynomial conditionally stable 51 An eigenvalue analysis shows that semi-implicit Euler is conditionally stable. This is much better than explicit Euler and in practice we can push semi-implicit Euler quite hard before it goes unstable.
  • 52. Semi-implicit Euler is conditionally stable position -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 velocity -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 semi-implicit exact start x0 = 1 v0 = 0 ω = 1 h = 0.1 52 At omega = 1, semi-implicit Euler is stable and accurate. Additionally it conserves energy on average.
  • 53. Increasing the frequency ω = 4 h = 0.1 position -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 velocity -8 -6 -4 -2 0 2 4 6 8 semi-implicit exact 53 Increasing the angular frequency reveals some deviation from the exact solution. But it is still stable and still conserves energy on average.
  • 55. Increasing the frequency ω = 10 h = 0.1 position -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 velocity -20 -15 -10 -5 0 5 10 15 20 semi-implicit exact hω = 1 55 Half the stability limit, things get strange, but it is still stable.
  • 57. Increasing the frequency ω = 16 h = 0.1 position -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 velocity -30 -20 -10 0 10 20 30 semi-implicit exact 57 This get really wacky at larger frequencies. Yet it didn’t blow up yet.
  • 58. Increasing the frequency ω = 24 h = 0.1 position -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 velocity -40 -30 -20 -10 0 10 20 30 40 semi-implicit exact hω = 2.4 58 This shows that eventually semi-implicit Euler will blow up. Rule of thumb: aim for at least 4 time steps per spring oscillation.
  • 59. Your first choice should be semi-implicit Euler 59 Runge Kutta Implicit Euler Explicit Euler Semi-implicit Euler Mid-point Verlet So far I have shown 3 numerical methods for differential equations: explicit Euler, implicit Euler, and semi-implicit Euler. There are many more algorithms out there, such as Runge-Kutta, mid-point, predictor-corrector, and more. For games, your first choice should be semi-implicit Euler. It is fast, reasonably stable, and easy to implement. The increased accuracy of Runge-Kutta, such as RK4, may seem appealing. However, any simulation with rigid body collisions is already wildly inaccurate. What matters more is stability and performance. In this department semi-implicit Euler delivers.
  • 60. Application to games Gyroscopic Motion 60 Now I would like to show an example of applying numerical methods to solve a difficult problem in rigid body dynamics. In particular we are going to look at gyroscopic motion of rigid bodies.
  • 61. Rigid body rotation I !ω +ω × Iω = T I : inertia tensor ω : angular velocity T : applied torque ω × Iω : gyroscopic torque ω L angular momentum L = Iω 61 This equation is known as Euler’s equations of motion. You can view it as Newton’s law for rotation. The inertia tensor is represented by a 3-by-3 matrix. The angular velocity and the applied torque are both 3D column vectors. Note that now omega represents angular velocity, not angular frequency! Curiously, they both have the same units: radians per second. This equation looks like ma = F, except there is a middle term that depends on angular velocity. This is the gyroscopic torque. It accounts for non-spherical shapes that may wobble as they spin freely in the air. Specifically, it arises because angular velocity and angular momentum may point in different directions.
  • 62. The gyroscopic torque is due to a non-uniform inertia tensor I = Ixx 0 0 0 Iyy 0 0 0 Izz ⎡ ⎣ ⎢ ⎢ ⎢ ⎢ ⎤ ⎦ ⎥ ⎥ ⎥ ⎥ L = Iω = Ixxωx Iyyωy Izzωz ⎡ ⎣ ⎢ ⎢ ⎢ ⎢ ⎤ ⎦ ⎥ ⎥ ⎥ ⎥ ω × Iω = stuff Ixx ≠ Iyy ≠ Izz Ixx Iyy Izz The inertia tensor for a box with non-equal sides is non-uniform. So apply the inertia tensor to the angular velocity results in an angular momentum that points in a different direction than the angular velocity. So the cross product between the angular velocity and the angular momentum is non-zero.
  • 63. Semi-implicit Euler ω2 = ω1 + hI1 −1 T1 −ω1 × I1ω1( ) q2 = q1 + h 2 ω2q1 63 This is semi-implicit Euler applied to the rotational equation. I’m using a special formula to update the quaternion. See the references for details.
  • 64. Capsule spin test 64 Here is a simulation of a spinning capsule using semi-implicit Euler. This video was created using the Blizzard physics engine called Domino.
  • 65. Why is this unstable? ω2 = ω1 + hI1 −1 T1 −ω1 × I1ω1( ) quadratic in omega 65 Here is the velocity update from semi-implicit Euler. This blows up because semi-implicit Euler extrapolates velocity. Since the gyroscopic torque is quadratic in the angular velocity, the extrapolation easily diverges.
  • 66. Typical trickery Drop gyroscopic term: I !ω = T Angular momentum as state: L = Iω !L = T 66 There are a couple tricks used to avoid the gyroscopic term. Many physics engines simply drop the gyroscopic term. This is what I have done for many years. Some folks like to avoid the gyroscopic term by using the angular momentum L rather than the angular velocity. This makes the gyroscopic term disappear from the rigid body equations of motion.
  • 67. The problem with angular momentum L2 = L1 + hT ω2 = I1 −1 L2 q2 = q1 + h 2 ω2q1 old inertia tensor : incorrect angular velocity 67 I can propagate the angular momentum with good stability using semi-implicit Euler. I need the updated angular velocity to compute the new orientation (quaternion). To compute the new angular velocity I need to apply the inverse inertia tensor to the angular momentum. In general the inertia tensor depends on orientation, so I am forced to use the old inertia tensor. This loses the correct rotational behavior.
  • 68. Lack of gyroscopic term in Diablo 3 68 In the past I’ve run into real game scenarios where dropping the gyroscopic term caused some odd behavior. In this case this creature’s staff is a slender box that spins a bit too much.
  • 69. Dropping the gyroscopic term 69 The capsule test is now stable. But it is too stable! The capsule spins forever.
  • 70. … adding rolling resistance 70 Adding rolling resistance results in a simulation that is still stable, but not plausible.
  • 71. Superposition Principle Δω1 = hI1 −2 T1 Δω2 = −hI2 −1 ω2 × I2ω2( ) stage 1 explicit: stage 2 implicit: ω2 = ω1 + Δω1 + Δω2 71 I can include the gyroscopic torque by using superposition principle which says that the effect of multiple separate torques is additive. This lets me use implicit Euler on the gyroscopic torque while using explicit Euler on all the torques.
  • 72. Solving the implicit equation I2 ω2 −ω1( )+ hω2 × I2ω2 = 0 f ω2( )= 0 72 For implicit solution of the gyroscopic term, I need to solve for omega. However, this is a nonlinear equation in 3 variables (xyz rotation). So I solve this using Newton-Raphson iteration. Therefore I put the problem into a standard form of some function of omega equals zero.
  • 73. Problem: inertia tensor I don’t have I2 Solution: work in local coordinates The inertia tensor is constant in local coordinates 73 The inertia tensor in world coordinates depends on the orientation of the rigid body. So we cannot compute I2. By working in local coordinates, I can side-step this problem.
  • 74. Newton-Raphson solver f (x) = 0 xn+1 = xn − fn fn ' f x( )= 0 J xn+1 − xn( )= fn J = ∂f ∂x1 … ∂f ∂xn ⎡ ⎣ ⎢ ⎢ ⎤ ⎦ ⎥ ⎥ single variable multiple variables 74 Here is a summary of Newton-Raphson. Typically it is described as a method for solving a single scalar equation. However, it can be extended to solve multiple simultaneous equations. To do this I need to compute the Jacobian of the function.
  • 75. Build Jacobian in local coordinates J = Ib + h skew(ωb )Ib − skew Ibωb( )⎡⎣ ⎤⎦ skew a( )= 0 −az ay az 0 −ax −ay ax 0 ⎡ ⎣ ⎢ ⎢ ⎢ ⎢ ⎤ ⎦ ⎥ ⎥ ⎥ ⎥ 75 Normally the inertia tensor is a function of orientation. However, I can build the implicit function for omega in local coordinates (b for body coordinates). The inertia tensor is constant in local coordinates so it does not need to be differentiated when I compute the Jacobian. Here the skew function takes a vector and builds the corresponding skew symmetric matrix. This vastly simplifies the solution.
  • 76. Gyrocopic torque solver Vec3 SolveGyroscopic(Quat q, Matrix Ib, Vec3 omega1, float h) { // Convert to body coordinates Vec3 omegab = InverseRotate(q, omega1); // Residual vector Vec3 f = h * Cross(omegab, Multiply33(Ib, omegab)); // Jacobian Matrix J = Ib + h *(Multiply33(Skew(omegab), Ib) - Skew(Multiply33(Ib, omegab))); // Single Newton-Raphson update omegab = omegab - Solve33(J, f); // Back to world coordinates Vec3 omega2 = Rotate(q, omegab); return omega2; } 76 Here is the implicit gyroscopic force solver. It uses body coordinates because I do not have the new world inertia tensor yet.
  • 77. Capsule implicit solution 77 Here is the result of using implicit solution of the gyroscopic torque. I only used one Newton-Raphson iteration.
  • 78. With rolling resistance 78 Adding rolling resistance makes the solution even better.
  • 79. The Dzhanibekov Effect 79 Here is a video of the Dzhanibekov effect shown on the ISS. Similar effects can be seen in tennis rackets, white board erasers, and mobile phones (when they slip out of our hands, that is why they are so hard to catch).
  • 80. Explicit gyroscopic integration 80 I can the experiment using explicit Euler. Unfortunately the kinetic energy grows unbounded.
  • 81. Dropping gyroscopic term 81 Here I dropped the gyroscopic term and you can see there is no tumbling motion.
  • 82. Implicit solution 82 In this video, I have included the implicit gyroscopic solver using one Newton-Raphson iteration. The tumbling motion is reproduced. You can see that the kinetic energy dissipates over time. This sort of tumbling motion can make destruction and explosions look better in games, especially when the are long skinny objects.
  • 83. Conclusion: qualities of numerical methods • Convergence • Accuracy • Stability 83 There are few qualities of numerical methods that are used to find the best match for what our games need. Convergence: as the time step gets smaller, the numerical result approaches the exact solution. For example, a numerical method that always returns 42 is not convergent for a harmonic oscillator. Accuracy: how well the solution matches the solution for a given time step. In games we can set the accuracy bar low. We aren’t building satellites. Stability: the numerical result grows unbounded even though the exact solution is unbounded
  • 85. Appendix Here is stuff that didn’t fit into a one hour presentation.
  • 86. Mass-Spring-Damper m!!x + c!x + kx = 0 86 spring k damper cground x mass m It is also interesting to add damping to see the effect on the phase portrait.
  • 87. Rewrite m!!x + c!x + kx = 0 !!x + 2ζω !x +ω2 x = 0 ω = k m ζ = c 2 mk angular frequency damping ratio 87 We can reduced from 3 parameters (m,k,c) to 2 parameters (omega, zeta) by dividing through by the mass. The new parameter zeta is called the damping ratio. If zeta is zero, there is no damping. Increasing zeta increases damping. Once zeta reaches 1, the damping is so strong that all oscillation is eliminated.
  • 88. Solution x = e−ζωt C1 sinωdt + C2 cosωdt( ) ωd = ω 1−ζ 2 damped frequency 88 Here is the oscillatory solution to the mass-spring-damper. The constants C1 and C2 depend on the initial conditions. When zeta is larger than one, then this solution no longer works. In that case the solution is just a decaying exponential.
  • 89. Phase portrait x0 = 1 v0 = 0 ω = 1 ζ = 0.1 position -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 velocity -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 start 89 Here is the phase portrait with a small amount of damping. As the time moves forward the position and velocity both decay to zero until the mass comes to rest.
  • 90. Phase portrait: velocity versus postion 90 Phase portrait animation for mass-spring-damper system
  • 91. Similarity of explicit and implicit Euler position -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 velocity -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 implicit exact start position -2 -1.5 -1 -0.5 0 0.5 1 1.5 2velocity -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 explicit exact start explicit implicit 91 The phase portrait shows that implicit Euler is stable. Also, implicit Euler is damped. So even though the mass should oscillate forever, implicit Euler damps the motion and brings the mass to rest. I call this numerical damping. If you compare this figure to explicit Euler, you should see that they look they look similar, except that explicit Euler is growing. This is because implicit Euler gives the same result as running explicit Euler in reverse.
  • 92. Duality of explicit and implicit Euler xi+1 = xi + hfi xi−1 = xi − hfi xi = xi−1 + hfi explicit reverse time implicit shift xi+1 = xi + hfi+1 rearrange 92 This shows that we can run time backwards on explicit Euler and get the same formula that is used for implicit Euler.
  • 93. Taylor Series x2 = x1 + h!x1 + 1 2 h2 !!x1 + 1 6 h3 !!!x1 +… 93 Finite difference formulas come from the Taylor Series, which states that any function can be extrapolated by using its current value and derivatives. The more terms we use, the more accurate the extrapolation. If we include the first derivative of x, we have first order accuracy. Including the second derivative gives second order accuracy, and so on. Notice that when h is small, high order derivatives contribute decreasing amounts to the approximation.
  • 94. Taylor Series: sine wave x ≈ t x ≈ t − 1 6 t3 x ≈ t − 1 6 t3 + 1 120 t5 x ≈ t − 1 6 t3 + 1 120 t5 − 1 5040 t7 x = sin(t) increasing accuracy Here is the Taylor Series approximation of the sine function with increasing accuracy as the number of terms increases. Note that the approximations are polynomials in time. The time could be just our time step h because we can start the Taylor Series extrapolation at any point of time.
  • 95. Taylor Series: sine wave 95 Here is how the Taylor Series can approximate a sine wave with increasing order.
  • 96. First Order Approximation x2 ≈ x1 + h!x1 !x1 = x2 − x1 h truncated Taylor Series forward difference rearrange 96 The first order approximation of the Taylor Series only keeps the first derivative. This is what I used already for the forward difference.
  • 97. Second Order x2 ≈ x1 + h!x1 + 1 2 h2 !!x1 problem 97 Keeping the second derivative results in a second order approximation. I can use this to derive a second order finite difference approximation. However, it may not be easy to compute the second derivative of x when x defined by a differential equation. Fortunately, there is a trick for eliminating the second derivative.
  • 98. Central Difference x0 = x1 − h!x1 + 1 2 h2 !!x1 x2 = x1 + h!x1 + 1 2 h2 !!x1 x2 − x0 = 2h!x1 !x1 = x2 − x0 2h TS2 TS1 TS2 - TS1 98 Here I have two second order Taylor Series approximations. By subtracting the first equation from the second equation, I can eliminate the second derivative. This results in the second order finite difference formula called the central difference. This shows the power of the Taylor Series. You can combine more Taylor Series instances to generate ever more accurate finite difference formulas. Although this is exciting, it is not really necessary for games. We can get by on first order accuracy most of the time.
  • 99. Central Difference x tt1 !x1 = x2 − x0 2h x0 x2 h h 99 Here’s how the central difference looks graphically. It really does look like a better approximation of the slope at t1.