Game Development Reference
In-Depth Information
ds = vVelocity * dt;
vPosition += ds;
// Integrate angular equation of motion:
aa = vMoment.z / fInertia;
dav = aa * dt;
vAngularVelocity.z += dav;
dr = RadiansToDegrees(vAngularVelocity.z * dt);
fOrientation += dr;
// Misc. calculations:
fSpeed = vVelocity.Magnitude();
vVelocityBody = VRotate2D(-fOrientation, vVelocity);
}
As the name of this method implies, we've implemented Euler's method of integration
as described in Chapter 7 . Integrating the linear equation of motion for a rigid body
follows exactly the same steps we used for integrating the linear equation of motion for
particles. All that's required is to divide the aggregate forces acting on a body by the
mass of the body to get the body'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 corresponding components in a . The change
in velocity, dv , is equal to acceleration times the change in time, dt . The body's new
velocity is then computed by the line vVelocity += dv . Here again, vVelocity and dv
are Vector s and the += operator takes care of the vector arithmetic. This is the first actual
integration for translation.
The second integration takes place in the next few lines, where we determine the body's
displacement and new position by integrating its velocity. The line ds = vVelocity *
dt determines the displacement, or change in the body's position, and the line vPosition
+= ds computes the new position by adding the displacement to the body's old position.
That's it for translation.
The next order of business is to integrate the angular equation of motion to find the
body's new orientation. The line aa = vMoment.z / fInertia; computes the body's
angular acceleration by dividing the aggregate moment acting on the body by its mass
moment of inertia. aa is a scalar, as is fInertia since this is a 2D problem. In 3D, things
are a bit more complicated, and we'll get to that in Chapter 11 .
We compute the change in angular velocity, dav , a scalar, by multiplying aa by the time
step size, dt . The new angular velocity is simply the old velocity plus the change: vAn
gularVelocity.z += dav . The change in orientation is equal to the new angular velocity
multiplied by the time step: vAngularVelocity.z * dt . Notice that we convert the
Search Nedrilad ::




Custom Search