Game Development Reference
In-Depth Information
ParticleGravity(const Vector3 &gravity);
/** Applies the gravitational force to the given particle. */
virtual void updateForce(Particle *particle, real duration);
};
Excerpt from src/pfgen.cpp
void ParticleGravity::updateForce(Particle* particle, real duration)
{
// Check that we do not have infinite mass.
if (!particle->hasFiniteMass()) return;
// Apply the mass-scaled force to the particle.
}
Notice that the force is calculated based on the mass of the object passed into the
updateForce method. The only piece of data stored by the class is the acceleration due
to gravity. One instance of this class could be shared among any number of objects.
5.2.4
AD RAG F ORCE G ENERATOR
We could also implement a force generator for drag. Drag is a force that acts on a
body and depends on its velocity. A full model of drag involves more complex math-
ematics that we can easily perform in real time. Typically in game applications we use
a simplified model of drag where the drag acting on a body is given by
p k 1 | ˙
2
f drag =− ˙
k 2 | ˙
p
|+
p
|
[5.1]
where k 1 and k 2 are two constants that characterize how strong the drag force is. They
are usually called the “drag coefficients,” and they depend on both the object and the
type of drag being simulated.
The formula looks complex but is simple in practice. It says that the force acts in
the opposite direction to the velocity of the object (this is the
˙
p part of the equation:
˙
p is the normalized velocity of the particle), with a strength that depends on both the
speed of the object and the square of the speed.
Drag that has a k 2 value will grow faster at higher speeds. This is the case with
the aerodynamic drag that keeps a car from accelerating indefinitely. At slow speeds
the car feels almost no drag from the air, but for every doubling of the speed the drag
almost quadruples. The implementation for the drag generator looks like this:
Excerpt from include/cyclone/pfgen.h
/**
* A force generator that applies a drag force. One instance