Game Development Reference
In-Depth Information
*/
class ParticleForceGenerator
{
public:
/**
* Overload this in implementations of the interface to calculate
* and update the force applied to the given particle.
*/
virtual void updateForce(Particle *particle, real duration) = 0;
};
The updateForce method is passed for the duration of the frame for which the
force is needed and a pointer to the particle requesting the force. The duration of the
frame is needed for some force generators (we will meet a spring-force generator that
depends critically on this value in chapter 6).
We pass the pointer of the object into the function so that a force generator does
not need to keep track of an object itself. This also allows us to create force generators
that can be attached to several objects at the same time. As long as the generator
instance does not contain any data that is specific to a particular object, it can simply
use the object passed in to calculate the force. Both of the example force generators
described in sections 5.2.3 and 5.2.4 have this property.
Theforcegeneratordoesnotreturnanyvalue.Wecouldhaveitreturnaforceto
add to the force accumulator, but then force generators would have to return some
force (even if it were zero), and that would remove the flexibility we'll use later in the
book when we support wholly different kinds of force. Instead, if a force generator
wants to apply a force, it can call the addForce method to the object it is passed.
As well as the interface for force generators, we need to be able to register which
force generators affect which particles. We could add this into each particle, with a
data structure such as a linked list or a growable array of generators. This would be
a valid approach, but it has performance implications: either each particle needs to
have lots of wasted storage (using a growable array) or new registrations will cause
lots of memory operations (creating elements in linked lists). For performance and
modularity I think it is better to decouple the design and have a central registry of
particles and force generators. The one I have provided looks like this:
Excerpt from include/cyclone/pfgen.h
#include <vector>
namespace cyclone {
/**
* Holds all the force generators and the particles they apply to.
*/
class ParticleForceRegistry
Search Nedrilad ::




Custom Search