Game Development Reference
In-Depth Information
Aside from properties, you'll notice several methods in the Particle class. The con‐
structor is trivial. It sets everything to 0 except the particle's mass, radius, and the gravity
force vector. The following code illustrates how everything is initialized:
Particle::Particle(void)
{
fMass = 1.0;
vPosition.x = 0.0;
vPosition.y = 0.0;
vPosition.z = 0.0;
vVelocity.x = 0.0;
vVelocity.y = 0.0;
vVelocity.z = 0.0;
fSpeed = 0.0;
vForces.x = 0.0;
vForces.y = 0.0;
vForces.z = 0.0;
fRadius = 0.1;
vGravity.x = 0;
vGravity.y = fMass * _GRAVITYACCELERATION;
}
Now is probably a good time to explain the coordinate system we've assumed. Our world
origin is located at the lower-left corner of the example program's window with positive
x pointing to the right and positive y pointing up. The acceleration due to gravity acts
downward (i.e., in the negative y-direction). We're using the SI system of units and have
defined the acceleration due to gravity as follows:
#define _GRAVITYACCELERATION −9.8f
That's 9.8 m/s 2 in the negative y-direction. We've set the mass of each particle to 1 kg by
default, which means the force due to gravity is 1 kg times 9.8 m/s 2 , or 9.8 newtons of
force. We've set the radius of each particle to one-tenth of a meter. These masses and
radii are really arbitrary; you can set them to anything suitable for what you're modeling.
The CalcLoads method is responsible for computing all the loads—forces—acting on
the particle, with the exception of impact forces (we'll handle those later). For now, the
only force acting on the particles is that due to gravity, or simply, the weight of each
particle. CalcLoads is very simple at this point:
void Particle::CalcLoads(void)
{
// Reset forces:
vForces.x = 0.0f;
vForces.y = 0.0f;
// Aggregate forces:
vForces += vGravity;
}
Search Nedrilad ::




Custom Search