Game Development Reference
In-Depth Information
For the most part, each unit is initialized in accordance with the Particle constructor
shown earlier. However, their positions are all at the origin, so we make a call to the
following Initialize function to randomly distribute the particles in the upper-middle
portion of the screen within a rectangle of width _SPAWN_AREA_R*4 and a height of
_SPAWN_AREA_R , where _SPAWN_AREA_R is just a global define we made up.
bool Initialize(void)
{
int i;
GetRandomNumber(0, _WINWIDTH, true);
for(i=0; i<_MAX_NUM_UNITS; i++)
{
Units[i].vPosition.x = GetRandomNumber(_WINWIDTH/2-_SPAWN_AREA_R*2,
_WINWIDTH/2+_SPAWN_AREA_R*2, false);
Units[i].vPosition.y = _WINHEIGHT −
GetRandomNumber(_WINHEIGHT/2-_SPAWN_AREA_R,
_WINHEIGHT/2, false);
}
return true;
}
OK, now let's consider UpdateSimulation as shown in the code snippet that follows.
This function gets called every cycle through the program's main message loop and is
responsible for cycling through all the Units , making appropriate function calls to up‐
date their positions, and rendering the scene.
void UpdateSimulation(void)
{
double dt = _TIMESTEP;
int i;
// initialize the back buffer
if(FrameCounter >= _RENDER_FRAME_COUNT)
{
ClearBackBuffer();
}
// update the particles (Units)
for(i=0; i<_MAX_NUM_UNITS; i++)
{
Units[i].CalcLoads();
Units[i].UpdateBodyEuler(dt);
if(FrameCounter >= _RENDER_FRAME_COUNT)
{
Units[i].Draw();
}
Search Nedrilad ::




Custom Search