Game Development Reference
In-Depth Information
change in orientation from radians to degrees here since we're keeping track of orien‐
tation in degrees. You don't really have to, so long as you're consistent.
The last line in UpdateBodyEuler computes the body's linear speed by transforming the
magnitude of its velocity vector to local, body coordinates. Recall in CalcLoads that we
require the body's velocity in body-fixed coordinates in order to compute the drag force
on the body.
Rendering
In this simple example, rendering the virtual hovercraft is just a little more involved
than rendering the particles in the example from Chapter 8 . All we do is draw a few
connected lines using Windows API calls wrapped in our own functions to hide some
of the Windows-specific code. The following code snippet is all we need to render the
hovercraft:
void DrawCraft(RigidBody2D craft, COLORREF clr)
{
Vector vList[5];
double wd, lg;
int i;
Vector v1;
wd = craft.fWidth;
lg = craft.fLength;
vList[0].x = lg/2; vList[0].y = wd/2;
vList[1].x = -lg/2; vList[1].y = wd/2;
vList[2].x = -lg/2; vList[2].y = -wd/2;
vList[3].x = lg/2; vList[3].y = -wd/2;
vList[4].x = lg/2*1.5; vList[4].y = 0;
for(i=0; i<5; i++)
{
v1 = VRotate2D(craft.fOrientation, vList[i]);
vList[i] = v1 + craft.vPosition;
}
DrawLine(vList[0].x, vList[0].y, vList[1].x, vList[1].y, 2, clr);
DrawLine(vList[1].x, vList[1].y, vList[2].x, vList[2].y, 2, clr);
DrawLine(vList[2].x, vList[2].y, vList[3].x, vList[3].y, 2, clr);
DrawLine(vList[3].x, vList[3].y, vList[4].x, vList[4].y, 2, clr);
DrawLine(vList[4].x, vList[4].y, vList[0].x, vList[0].y, 2, clr);
}
You can use your own rendering code here, of course, and all you really need to pay
close attention to is transforming the coordinates for the outline of the hovercraft from
body to world coordinates. This involves rotating the vertex coordinates from body-
fixed space using the VRotate2D function and then adding the position of the center of
gravity of the hovercraft to each transformed vertex. These lines take care of this coor‐
dinate transformation: