Game Development Reference
In-Depth Information
here overly complex, burying some key code that we need to point out to you. In practice,
you're better off using one of the other methods we discuss in Chapter 7 instead of Euler's
method. With that said, we've prepared a function called StepSimulation that handles
all of the integration necessary to actually propagate the simulation:
void StepSimulation(float dt)
{
// Take care of translation first:
// (If this body were a particle, this is all you would need to do.)
Vector Ae;
// calculate all of the forces and moments on the airplane:
// calculate the acceleration of the airplane in earth space:
Ae = Airplane.vForces / Airplane.fMass;
// calculate the velocity of the airplane in earth space:
Airplane.vVelocity += Ae * dt;
// calculate the position of the airplane in earth space:
Airplane.vPosition += Airplane.vVelocity * dt;
// Now handle the rotations:
float mag;
// calculate the angular velocity of the airplane in body space:
Airplane.vAngularVelocity += Airplane.mInertiaInverse *
(Airplane.vMoments -
(Airplane.vAngularVelocity^
(Airplane.mInertia *
Airplane.vAngularVelocity)))
* dt;
// calculate the new rotation quaternion:
Airplane.qOrientation += (Airplane.qOrientation *
Airplane.vAngularVelocity) *
(0.5f * dt);
// now normalize the orientation quaternion:
mag = Airplane.qOrientation.Magnitude();
if (mag != 0)
Airplane.qOrientation /= mag;
// calculate the velocity in body space:
// (we'll need this to calculate lift and drag forces)
Airplane.vVelocityBody = QVRotate(~Airplane.qOrientation,
Airplane.vVelocity);
// calculate the air speed: