Game Development Reference
In-Depth Information
We can carry out this procedure of taking on more Taylor terms even further. The
popular Runge-Kutta method takes such an approach to reduce the truncation error to
the order of (∆ t ) 5 . The integration formulas for this method are as follows:
k 1 = (∆t) y'(t, y)
k 2 = (∆t) y'(t + ∆t/2, y + k 1 /2)
k 3 = (∆t) y'(t + ∆t/2, y + k 2 /2)
k 4 = (∆t) y'(t + ∆t, y + k 3 )
y(t + ∆t) = y(t) + 1/6 (k 1 + 2 (k 2 ) + 2 (k 3 ) + k 4 )
Applying these formulas to our ship example yields:
k 1 = ∆t [1/m (T - C v t )]
k 2 = ∆t [1/m (T - C (v t + k 1 /2))]
k 3 = ∆t [1/m (T - C (v t + k 2 /2))]
k 4 = ∆t [1/m (T - C (v t + k 3 ))]
v t+∆t = v t + 1/6 (k 1 + 2 (k 2 ) + 2 (k 3 ) + k 4 )
For our example, the Runge-Kutta method is implemented as follows:
// This function progresses the simulation by dt seconds using
// the Runge-Kutta method
void StepSimulation(float dt)
{
float F; // total force
float A; // acceleration
float Vnew; // new velocity at time t + dt
float Snew; // new position at time t + dt
float k1, k2, k3, k4;
F = (T - (C * V));
A = F/M;
k1 = dt * A;
F = (T - (C * (V + k1/2)));
A = F/M;
k2 = dt * A;
F = (T - (C * (V + k2/2)));
A = F/M;
k3 = dt * A;
F = (T - (C * (V + k3)));
A = F/M;
k4 = dt * A;