Game Development Reference
In-Depth Information
for(i=0; i<5; i++)
{
v1 = VRotate2D(craft.fOrientation, vList[i]);
vList[i] = v1 + craft.vPosition;
}
The Basic Simulator
The heart of this simulation is handled by the RigidBody2D class described earlier.
However, we need to show you how that class is used in the context of the main program.
This simulator is very similar to that shown in Chapter 8 for particles, so if you've read
that chapter already you can breeze through this section.
First, we define a few global variables as follows:
// Global Variables:
int FrameCounter = 0;
RigidBody2D Craft;
FrameCounter counts the number of time steps integrated before the graphics display
is updated. How many time steps you allow the simulation to integrate before updating
the display is a matter of tuning. You'll see how this is used momentarily when we discuss
the UpdateSimulation function. Craft is a RigidBody2D type that will represent our
virtual hovercraft.
For the most part, Craft is initialized in accordance with the RigidBody2D constructor
shown earlier. However, its position is at the origin, so we make a call to the following
Initialize function to locate the Craft in the middle of the screen vertically and on
the left side. We set its orientation to 0 degrees so it points toward the right side of the
screen:
bool Initialize(void)
{
Craft.vPosition.x = _WINWIDTH/10;
Craft.vPosition.y = _WINHEIGHT/2;
Craft.fOrientation = 0;
return true;
}
OK, now let's consider UpdateSimulation as shown in the code snippet below. This
function gets called every cycle through the program's main message loop and is re‐
sponsible for making appropriate function calls to update the hovercraft's position and
orientation, as well as rendering the scene. It also checks the states of the keyboard arrow
keys and makes appropriate function calls:
void UpdateSimulation(void)
{
double dt = _TIMESTEP;
RECT r;
Search Nedrilad ::




Custom Search