Game Development Reference
In-Depth Information
the acceleration in advance using this equation, without having to repeat it at every
update. In the remainder of the topic, however, it will be a crucial step.
So far all the equations have been given in their mathematics textbook form, ap-
plied to scalar values. As we saw in the last chapter on calculus, we can convert them
easily to use vectors:
1
m f
sotheforceisavector,aswellasacceleration.
p
¨
=
3.2.4
A DDING M ASS TO P ARTICLES
Added to the position and velocity we have stored per particle, we need to store its
mass so that we can correctly calculate its response to forces. Many physics engines
simply add a scalar mass value for each object. There is a better way to get the same
effect, however.
First there is an important thing to notice about equation 3.2. If the mass of an
object is zero, then the acceleration will be infinite, as long as the force is not zero. This
is not a situation that should ever occur: no particle that we can simulate should ever
have zero mass. If we try to simulate a zero mass particle, it will cause divide-by-zero
errors in the code.
It is often useful, however, to simulate infinite masses. These are objects that no
force of any magnitude can move. They are very useful for immovable objects in a
game: the walls or floor, for example, cannot be moved during the game. If we feed
an infinite mass into equation 3.2, then the acceleration will be zero, as we expect.
Unfortunately we cannot represent a true infinity in most computer languages, and
the optimized mathematics instructions on all common game processors do not cope
well with infinities. We have to get slightly creative. Ideally we want a solution where
it is easy to get infinite masses but impossible to get zero masses.
Notice in equation 3.2 that we use 1 over the mass each time. Because we never
use the 3.1 form of the equation, we can speed up our calculations by not storing the
mass for each object, but 1 over the mass. We call this the “inverse mass.” This solves
our problem for representing objects of zero or infinite mass: infinite mass objects
have a zero inverse mass, which is easy to set. Objects of zero mass would have an
infinite inverse mass, which cannot be specified in most programming languages.
We update our particle class to include the inverse mass in this way:
Excerpt from include/cyclone/particle.h
class Particle
{
// ... Other Particle code as before ...
/**
* Holds the inverse of the mass of the particle. It
* is more useful to hold the inverse mass because
Search Nedrilad ::




Custom Search