Game Development Reference
In-Depth Information
// Euler's basic 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
// Calculate the total force
F = (T − (C * V));
// Calculate the acceleration
A = F / M;
// Calculate the new velocity at time t + dt
// where V is the velocity at time t
Vnew = V + A * dt;
// Calculate the new displacement at time t + dt
// where S is the displacement at time t
Snew = S + Vnew * dt;
// Update old velocity and displacement with the new ones
V = Vnew;
S = Snew;
}
Although Euler's method is stable in this example, that isn't always so, depending on the
problem you're trying to solve. This is something that you must keep in mind when
implementing any numerical integration scheme. What we mean by stable here is that,
in this case, the Euler solution converges with the exact solution. An unstable solution
could manifest errors in two ways. First, successive values could oscillate above and
below the exact solution, never quite converging on it. Second, successive values could
diverge from the exact solution, creating a greater and greater error over time.
Take a look at Figure 7-5 . This figure shows how Euler's method can become very un‐
stable. What you see in the graph represents the vibratory motion of a spring-mass
system. This is a simple dynamical system that should exhibit regular sinusoidal motion.