Game Development Reference
In-Depth Information
if(Units[i].vPosition.x > _WINWIDTH) Units[i].vPosition.x = 0;
if(Units[i].vPosition.x < 0) Units[i].vPosition.x = _WINWIDTH;
if(Units[i].vPosition.y > _WINHEIGHT) Units[i].vPosition.y = 0;
if(Units[i].vPosition.y < 0) Units[i].vPosition.y = _WINHEIGHT;
// Render the scene if required
if(FrameCounter >= _RENDER_FRAME_COUNT) {
FrameCounter = 0;
} else
The two local variables in UpdateSimulation are dt and i . i is trivial and serves as a
counter variable. dt represents the small yet finite amount of time, in seconds, over
which each integration step is taken. The global define_TIMESTEP stores the time step,
which we have set to 0.1 seconds. This value is subject to tuning, which we'll discuss
toward the end of this chapter in the section “Tuning” on page 186 .
The next segment of code checks the value of the frame counter, and if the frame counter
has reached the defined number of frames, stored in _RENDER_FRAME_COUNT , then the
back buffer is cleared to prepare it for drawing upon and ultimately copying to the
The next section of code under the comment update the particles does just that by
calling the CalcLoads and UpdateBodyEuler methods of each Unit . These two lines are
responsible for updating all the forces acting on each particle and then integrating the
equation of motion for each particle.
The next few lines within the for loop draw each particle if required and wrap each
particle's position around the window extents should they progress beyond the edges
of the window. Note that we're using window coordinates in this example.
Implementing External Forces
We'll add a couple of simple external forces to start with—still air drag, and wind force.
We'll use the formulas presented in Chapter 3 to approximate these forces, treating them
in a similar manner. Recall that still air drag is the aerodynamic drag force acting against
an object moving at some speed through still air. Drag always acts to resist motion.
While we'll use the same formulas to compute a wind force, recall that wind force may
not necessarily act to impede motion. You could have a tailwind pushing an object along,
or the wind could come from any direction with components that push the object side‐
ways. In this example we'll assume a side wind from left to right, acting to push the
particles sideways, with the still air drag resisting their falling motion. When we add
collisions later, this same drag formulation will act to resist particle motion in any di‐
rection in which they travel as they bounce around.
Search Nedrilad ::

Custom Search