Game Development Reference
In-Depth Information
// Integrate the equations of motion:
for(i=0; i<NUMBODIES; i++)
{
// Calculate the acceleration in earth space:
Ae = Bodies[i].vForces / Bodies[i].fMass;
Bodies[i].vAcceleration = Ae;
// Calculate the velocity in earth space:
Bodies[i].vVelocity += Ae * dt;
// Calculate the position in earth space:
Bodies[i].vPosition += Bodies[i].vVelocity * dt;
// Now handle the rotations:
float mag;
Bodies[i].vAngularAcceleration = Bodies[i].mInertiaInverse *
(Bodies[i].vMoments -
(Bodies[i].vAngularVelocity^
(Bodies[i].mInertia *
Bodies[i].vAngularVelocity)));
Bodies[i].vAngularVelocity += Bodies[i].vAngularAcceleration * dt;
// Calculate the new rotation quaternion:
Bodies[i].qOrientation += (Bodies[i].qOrientation *
Bodies[i].vAngularVelocity) *
(0.5f * dt);
// Now normalize the orientation quaternion:
mag = Bodies[i].qOrientation.Magnitude();
if (mag != 0)
Bodies[i].qOrientation /= mag;
// Calculate the velocity in body space:
Bodies[i].vVelocityBody = QVRotate(~Bodies[i].qOrientation,
Bodies[i].vVelocity);
// Get the angular velocity in global coords:
Bodies[i].vAngularVelocityGlobal = QVRotate(Bodies[i].qOrientation,
Bodies[i].vAngularVelocity);
// Get the angular acceleration in global coords:
Bodies[i].vAngularAccelerationGlobal = QVRotate(Bodies[i].qOrientation,
Bodies[i].vAngularAcceleration);
// Get the inverse intertia tensor in global coordinates
Matrix3x3 R, RT;
R = MakeMatrixFromQuaternion(Bodies[i].qOrientation);
RT = R.Transpose();
Bodies[i].mIeInverse = R * Bodies[i].mInertiaInverse * RT;