Game Development Reference
In-Depth Information
We can make it easier to manage these long-term forces by creating a registry.
A force registers itself with a particle and then will be asked to provide a force at each
frame. I call these “force generators.”
5.2
F ORCE G ENERATORS
We have a mechanism for applying multiple forces to an object. We now need to work
out where these forces come from. The force of gravity is fairly intuitive: it is always
present for all objects in the game.
Some forces arise because of the behavior of an object—a dedicated drag force,
for example. Other forces are a consequence of the environment that an object finds
itself in: a buoyancy force for a floating object and the blast force from an explosion
are examples. Still other types of force are a result of the way objects are connected to-
gether; we will look at forces that behave like springs in the next chapter. Finally there
are forces that exist because the player (or an AI-controlled character) has requested
them: the acceleration force in a car or the thrust from a jetpack, for example.
Another complication is the dynamic nature of some forces. The force of gravity
is easy because it is always constant. We can calculate it once and leave it set for the
rest of the game. Most other forces are constantly changing. Some change as a result
of the position or velocity of an object: drag is stronger at higher speeds, and a spring's
force is greater the more it is compressed. Others change because of external factors:
an explosion dissipates, or a player's jetpack burst will come to a sudden end when he
releases the thrust button.
We need to be able to deal with a range of different forces with very different
mechanics for their calculation. Some might be constant, others might apply some
function to the current properties of the object (such as position and velocity), some
might require user input, and others might be time based.
If we simply programmed all these types of force into the physics engine, and set
parameters to mix and match them for each object, the code would rapidly become
unmanageable. Ideally we would like to be able to abstract away the details of how a
force is calculated and allow the physics engine to simply work with forces in general.
This would allow us to apply any number of forces to an object, without the object
knowing the details of how those forces are calculated.
I will do this through a structure called a “force generator.” There can be as many
different types of force generator as there are types of force, but each object doesn't
need to know how a generator works. The object simply uses a consistent interface to
find the force associated with each generator: these forces can then be accumulated
and applied in the integration step. This allows us to apply any number of forces, of
any type we choose, to the object. It also allows us to create new types of force for
new games or levels, as we need to, without having to rewrite any code in the physics
engine.
Not every physics engine has the concept of force generators: many require hand-
written code to add forces or else limit the possible forces to a handful of common