Game Development Reference
The last line in UpdateBodyEuler computes the particle's speed by taking the magnitude
of its velocity vector.
For demonstration purposes, using Euler's method is just fine. In an actual game, the
more robust method described in Chapter 7 is advised.
In this example, rendering the particles is rather trivial. All we do is draw little circles
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 particles.
float drawRadius = max(2, fRadius);
SetRect(&r, vPosition.x − drawRadius,
_WINHEIGHT − (vPosition.y − drawRadius),
vPosition.x + drawRadius,
_WINHEIGHT − (vPosition.y + drawRadius));
DrawEllipse(&r, 2, RGB(0,0,0));
You can use your own rendering code here, of course, and all you really need to pay
close attention to is converting from world coordinates to window coordinates. Re‐
member, we've assumed our world coordinate system origin is in the lower-left corner
of the window, whereas the window drawing coordinate system has its origin in the
upper-left corner of the window. To transform coordinates in this example, all you need
to do is subtract the particle's y-position from the height of the window.
The Basic Simulator
The heart of this simulation is handled by the Particle class described earlier. However,
we need to show you how that class is used in the context of the main program.
First, we define a few global variables as follows:
// Global Variables:
int FrameCounter = 0;
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. Units is an array of Particle types. These will rep‐
resent moving particles in the simulation—the ones that fall from above and bounce off
the circular objects we'll add later.