Game Development Reference
In-Depth Information
Although Euler integration is very simple, it does not really exhibit very much
accuracy. One big issue is that the position calculation is using the old velocity,
not the new velocity after the time step. This results in a propagation of error that
causes the approximation to diverge further and further as time continues.
A simple modification to basic Euler integration is to swap the order of the posi-
tion andvelocity calculations. What this means isthat the position isnowupdating
based on the new velocity, not the old velocity. This is
semi-implicit Euler integ-
ration
, and it ends up being reasonably more stable, to the point that respectable
physics engines such as Box2D utilize it. However, if we want further accuracy
we have to explore more complex numerical integration methods.
Velocity Verlet Integration
In
velocity Verlet integration
, first the velocity at the time step's midpoint is cal-
culated. This average velocity is then used to integrate the position over the en-
tire time step. Next, the acceleration is updated based on the force and mass, and
finally that new acceleration is applied to calculate the velocity at the end of the
time step. The implementation of velocity Verlet is shown in
Listing 7.8
.
Listing 7.8
Velocity Verlet Integration
function
Update(
float
deltaTime)
Vector3
sumOfForces
= sum of forces in
forces
// Velocity Verlet Integration
Vector3
avgVelocity
=
velocity
+
acceleration
*
deltaTime
/ 2.0f
// Position is integrated with the average velo-
city
position
+=
avgVelocity
*
deltaTime
// Calculate new acceleration and velocity
acceleration
=
sumOfForces
/
mass
velocity
=
avgVelocity
+
acceleration
*
deltaTime
/ 2.0f
end