Game Development Reference

In-Depth Information

The first order of business is to reset the
vForces
vector.
vForces
is the vector containing

the net force acting on the particle. All of these forces are aggregated in
CalcLoads
, as

shown by the line
vForces += vGravity
. Again, so far, the only force to aggregate is

that due to gravity.

Integrator

The
UpdateBodyEuler
method integrates the equations of motion for each particle.

Since we're dealing with particles, the only equation of motion we need concern our‐

selves with is that for translation; rotation does not matter for particles (at least not for

us here). The following code sample shows
UpdateBodyEuler
.

void Particle::UpdateBodyEuler(double dt)

{

Vector a;

Vector dv;

Vector ds;

// Integrate equation of motion:

a = vForces / fMass;

dv = a * dt;

vVelocity += dv;

ds = vVelocity * dt;

vPosition += ds;

// Misc. calculations:

fSpeed = vVelocity.Magnitude();

}

As the name of this method implies, we've implemented Euler's method of integration

as described in
Chapter 7
. Using this method, we simply need to divide the aggregate

forces acting on a particle by the mass of the particle to get the particle's acceleration.

The line of code
a = vForces / fMass
does just this. Notice here that
a
is a
Vector
, as

is
vForces
.
fMass
is a scalar, and the
/
operator defined in the
Vector
class takes care

of dividing each component of the
vForces
vector by
fMass
and setting the corre‐

sponding components in
a
. The change in velocity,
dv
, is equal to acceleration times the

change in time,
dt
. The particle's new velocity is then computed by the line
vVelocity

+= dv
. Here again,
vVelocity
and
dv
are
Vectors
and the
+=
operator takes care of the

vector arithmetic. This is the first actual integration.

The second integration takes place in the next few lines, where we determine the par‐

ticle's displacement and new position by integrating its velocity. The line
ds = vVeloc

ity * dt
determines the displacement, or change in the particle's position, and the line

vPosition += ds
computes the new position by adding the displacement to the parti‐

cle's old position.