Game Development Reference
In-Depth Information
There are several particle attributes we must consider even for this simple example. Our
model assumes that each particle has mass, and a set diameter (we're assuming our
particles are circles in 2D or spheres in 3D), occupies some position in space, and is
traveling at some velocity. Additionally, each particle is acted upon by some net external
force, which is the aggregate of all forces acting on the particle. These forces will be
gravity alone to start with, but will eventually include drag and impact forces. We set
up a Particle class to encapsulate these attributes as follows:
class Particle {
public:
float fMass; // Total mass
Vector vPosition; // Position
Vector vVelocity; // Velocity
float fSpeed; // Speed (magnitude of the velocity)
Vector vForces; // Total force acting on the particle
Vector vGravity; // Gravity force vector
Particle(void); // Constructor
void CalcLoads(void); // Aggregates forces acting on the particle
void UpdateBodyEuler(double dt); // Integrates one time step
void Draw(void); // Draws the particle
};
Most of these attributes are self-explanatory given the comments we've included. Notice
that several of these attributes are Vector types. These vectors are defined in the custom
math library we've included in Appendix A . This type makes managing vectors and
performing arithmetic operations with them a breeze. Take a look at Appendix A to see
what this custom type does. We'll just remind you of the data structure Vector uses:
three scalars called x , y , and z representing the three dimensions of a location or of a
movement in some direction. The z component will always be set to 0 in this chapter's
examples.
You should have noticed the fSpeed property in the Particle class. This property stores
the magnitude of the velocity vector, the particle's speed. We'll use this later when com‐
puting aerodynamic drag forces. We've also included a Vector type property called
vGravity , which stores the gravity force vector defining the magnitude and the direction
in which the gravity force acts. This is not really necessary, as you could hardcode the
gravity force vector or use a global variable; however, we've included it here to illustrate
some creative flexibility. For example, you could redefine the gravity vector in a game
that uses accelerometer input to determine gravity's direction with respect to a particular
device's orientation (see Chapter 21 ). And you may have a game where some particles
react to different gravities depending on their type, which can be of your own concoc‐
tion.