Game Development Reference
In-Depth Information
// (This inertia matrix (tensor) is in body coordinates)
Ixx = 0; Iyy = 0; Izz = 0;
Ixy = 0; Ixz = 0; Iyz = 0;
for (i = 0; i< 8; i++)
{
Ixx += Element[i].vLocalInertia.x + Element[i].fMass *
(Element[i].vCGCoords.y*Element[i].vCGCoords.y +
Element[i].vCGCoords.z*Element[i].vCGCoords.z);
Iyy += Element[i].vLocalInertia.y + Element[i].fMass *
(Element[i].vCGCoords.z*Element[i].vCGCoords.z +
Element[i].vCGCoords.x*Element[i].vCGCoords.x);
Izz += Element[i].vLocalInertia.z + Element[i].fMass *
(Element[i].vCGCoords.x*Element[i].vCGCoords.x +
Element[i].vCGCoords.y*Element[i].vCGCoords.y);
Ixy += Element[i].fMass * (Element[i].vCGCoords.x *
Element[i].vCGCoords.y);
Ixz += Element[i].fMass * (Element[i].vCGCoords.x *
Element[i].vCGCoords.z);
Iyz += Element[i].fMass * (Element[i].vCGCoords.y *
Element[i].vCGCoords.z);
}
// Finally set up the airplane's mass and its inertia matrix and take the
// inverse of the inertia matrix
Airplane.fMass = mass;
Airplane.mInertia.e11 = Ixx;
Airplane.mInertia.e12 = -Ixy;
Airplane.mInertia.e13 = -Ixz;
Airplane.mInertia.e21 = -Ixy;
Airplane.mInertia.e22 = Iyy;
Airplane.mInertia.e23 = -Iyz;
Airplane.mInertia.e31 = -Ixz;
Airplane.mInertia.e32 = -Iyz;
Airplane.mInertia.e33 = Izz;
Airplane.mInertiaInverse = Airplane.mInertia.Inverse();
}
The airplane is modeled by a number of elements, each representing a different part of
the airplane's structure—for example, the tail rudder, elevators, wings, and fuselage (see
Chapter 15 for more details). The code specified here takes the mass properties of each
element and combines them, using the techniques discussed in Chapter 7 to come up
with the combined inertia tensor for the entire aircraft. The important distinction be‐
tween these calculations in a 3D simulation and the 2D simulation is that here the inertia
is a tensor and in 2D it is a single scalar.
InitializeAirplane is called at the very start of the program. We found it convenient
to make the call right after the application's main window is created.
The final part of the flight model has to do with calculating the forces and moments
that act on the airplane at any given instant in time during the simulation. As in the 2D