Game Development Reference
In-Depth Information
previously there would have been one or two contacts with obvious interpenetration,
this method shares the interpenetration among all the conflicting contacts, which
may be less noticeable.
Unfortunately relaxation also makes it more likely that interpenetration is notice-
able at the end of an update when there are few collisions. It is beneficial to have all
contacts share a small degree of interpenetration when the alternative is having one
very bad contact, but in most cases it is undesirable and a full-strength resolution step
is more visually pleasing.
It is relatively simple to add relaxation to your engine (you just multiply the pen-
etration to resolve by a fixed proportion before performing the normal resolution
algorithm). I'd advise you to build the basic system without relaxation, and then add
it only if you find that you need to.
14.3.2
I MPLEMENTING N ONLINEAR P ROJECTION
Let's look in more detail at the nonlinear projection method and get it working in our
code. We start knowing the penetration depth of the contact: this is the total amount
of movement we'll need to resolve the interpenetration. Our goal is to find the pro-
portion of this movement that will be contributed by linear and angular motion for
each object.
We first make an assumption: we imagine the objects we are simulating were
pushed together so that they deformed. Rather than the amount of interpenetration,
we treat the penetration depth of the contact as if it were the amount of deforma-
tion in both bodies. As we saw in the section on resolving velocities, this deformation
causes a force that pushes the objects apart. This force, according to D'Alembert's
principle, has both linear and angular effects. The amount of each depends on the
inverse mass and the inverse inertia tensor of each object.
Treating interpenetration in this way allows us to use the same mathematics we
saw in section 14.2. We are effectively modeling how the two objects would be pushed
apart by the deformation forces: we are using a physically realistic method to find the
linear and angular components we need.
Calculating the Components
For velocity we were interested in the amount of velocity change caused by the rota-
tion change from a single unit of impulse. This quantity is a measure of how resistant
the object is to being rotated when an impulse or force is applied at the contact point.
To resolve penetration we use exactly the same sequence of equations. We find the
resistance of the object to being moved in both a linear and angular way.
Recall that the resistance of an object to being moved is called its “inertia.” So
we are interested in finding the inertia of each object in the direction of the contact
normal. This inertia will have a linear component and a rotational component.
The linear component of inertia is, as before, simply the inverse mass. The an-
gular component is calculated using the same sequence of operations that we used
previously. Together the code looks like this: