A PARTICLE

The first law

The problem arises because the processor that performs the physics calculations isn’t completely accurate. This inaccuracy can lead to objects getting faster of their own accord.

A better solution is to incorporate a rough approximation of drag directly into the engine. This allows us to make sure objects aren’t being accelerated by numerical inaccuracy, and it can allow us to simulate some kinds of drag. If we need complicated drag (such as aerodynamic drag in a flight simulator or racing game), we can still do it the long way by creating a special drag force. We call the simple form of drag “damping” to avoid confusion.

class Particle
{
    // ... Other Particle code as before ...
    /**
    * Holds the amount of damping applied to linear
    * motion. Damping is required to remove energy added
    * through numerical instability in the integrator.
    */
    real damping;
};

When we come to perform the integration, we will remove a proportion of the object’s velocity at each update. The damping parameter controls how velocity is left after the update. If the damping is zero, then the velocity will be reduced to nothing: this would mean that the object couldn’t sustain any motion without a force and would look odd to the player. A value of 1 means that the object keeps all its velocity (equivalent to no damping). If you don’t want the object to look like it is experiencing drag, then values near but less than 1 are optimal—0.995, for example.

The Force Equations

The formula relating the force to the acceleration is the famous:

\[f = ma = m\ddot{p} \qquad \qquad \qquad [3.1] \]

\[\ddot{p} = \frac{1}{m}f \qquad \qquad \qquad \qquad \qquad [3.2] \]

MOMENTUM AND VELOCITY

Although Newton 1 is often introduced in terms of velocity, that is a misrepresentation. It is not velocity that is constant in the absence of any forces, but momentum. Momentum is the product of velocity and mass. Since mass is normally constant, we can assume that velocity is therefore constant by Newton 1. In the event that a traveling object were changing mass, then its velocity would also be changing, even with no forces.It will be an important distinction when we come to look at rotations later, however, because rotating objects can change the way their mass is distributed. Under the rotational form of Newton 1, that means a change in rotational speed, with no other forces acting.

THE COMPLETE INTEGRATOR

void Particle::integrate(real duration)
{
    // We don't integrate things with zero mass.
    if (inverseMass <= 0.0f) return;

    assert(duration > 0.0);

    // Update linear position.
    position.addScaledVector(velocity, duration);

    // Work out the acceleration from the force
    Vector3 resultingAcc = acceleration;
    resultingAcc.addScaledVector(forceAccum, inverseMass);

    // Update linear velocity from the acceleration.
    velocity.addScaledVector(resultingAcc, duration);

    // Impose drag.
    velocity *= real_pow(damping, duration);

    // Clear the forces.
    clearAccumulator();
}