Game Development Reference
In-Depth Information
5.3
B UILT - IN G RAVITY AND D AMPING
Using the generators discussed earlier we can replace both the damping and the accel-
eration due to gravity with force generators. This is a valid approach and one used by
many different engines. It allows us to remove the special code that processes damp-
ing, and it means we don't need to store an acceleration due to gravity with the object;
it can be calculated among all the other forces during transient force accumulation.
Although it has some advantages in simplicity, this is not the approach I will use.
Directly applying the damping and acceleration due to gravity, in the way we did in
chapter 3, is fast. If we have to calculate forces for them each time, we waste extra time
To avoid this I keep damping and acceleration unchanged. If we need more com-
plex drag, we can set a damping value nearer to 1 and add a drag force generator.
Similarly, if we need some exotic form of gravity (for an orbiting space ship, for ex-
ample), we could create a gravity force generator that provides the correct behavior
and set the acceleration due to gravity to be 0.
5.4
S UMMARY
Forces are easily combined by adding their vectors together, and the total acts as if it
were the only force applied to an object. This is D'Alembert's principle, and it allows
us to support any number of general forces without having to know anything about
how the forces are generated.
Throughout this topic we'll see various kinds of force generators that simulate
some kind of physical property by calculating a force to apply to an object. The code
we've created in this chapter allows us to manage those forces, combining them and
applying them before integrating.
Drag and gravity are important force generators, but they only replicate the func-
tionality we had in our particle physics engine. To move toward a mass-aggregate
physics engine we need to start linking particles together. Chapter 6 introduces
springs, and other springlike connections, using the force generator structure we've
built in this chapter.