Game Development Reference
In-Depth Information
// Apply impulses: they are applied in the direction of the contact,
// and are proportional to the inverse mass.
particle[0]->setVelocity(particle[0]->getVelocity() +
impulsePerIMass * particle[0]->getInverseMass()
);
if (particle[1])
{
// Particle 1 goes in the opposite direction.
particle[1]->setVelocity(particle[1]->getVelocity() +
impulsePerIMass * -particle[1]->getInverseMass()
);
}
}
This directly changes the velocities of each object to reflect the collision.
7.2.1
C OLLISION D ETECTION
Thecollisionpointswillnormallybefoundusing a collision detector. A collision
detector is a chunk of code responsible for finding pairs of objects that are colliding
or single objects that are colliding with some piece of immovable scenery.
In our engine the end result of the collision detection algorithm is a set of Contact
data structures, filled with the appropriate information. Collision detection obviously
needs to take the geometries of the objects into account: their shape and size. So far
in the physics engine, we've assumed we are dealing with particles, which lets us avoid
taking geometry into account at all.
This is a distinction we'll keep intact even with more complicated 3D objects:
the physics simulation system (that part of the engine that handles laws of motion,
collision resolution, and forces) will not need to know the details of the shape of the
objects it is dealing with. The collision detection system is responsible for calculating
any properties that are geometrical, such as when and where two objects are touching,
and the contact normal between them.
There is a whole range of algorithms used for working out contact points, and
we'll implement a range of useful collision detection routines for full 3D objects in
chapter 12. For now we'll assume this is a magic process hidden inside a black box.
There is one exception: I'll cover the simplest possible collision detection for par-
ticles represented as small spheres in the next chapter. This will allow us to build
some useful physics systems with just the mass-aggregate engine we are constructing.
Other than that I'll leave the details until after we've looked at full 3D rigid bodies in
chapter 10.
Some collision detection algorithms can take into account the way objects are
moving and try to predict likely collisions in the future. Most simply look through
the set of objects and check to see whether any two objects are interpenetrating.