Game Development Reference
In-Depth Information
which is implemented as
Excerpt from src/contacts.cpp
// Convert impulse to world coordinates.
Vector3 impulse = contactToWorld.transform(impulseContact);
With the impulse calculated in world coordinates we can go ahead and apply it to
the objects in the collision.
14.2.6
A PPLYING THE I MPULSE
To apply the impulse, we use equations 7.5 and 14.1. The first tells us that linear
impulses change the linear velocity of the object according to the formula
g
m
˙
p
=
So the velocity change for the first object in the collision will be
Vector3 velocityChange = impulse * body[0]->getInverseMass();
Therotationchangeisgivenbyequation14.1as
θ
I 1 u
=
We first need to calculate the impulsive torque, u , using equation 14.2 again:
u
=
q rel ×
g
In code this looks like
Vector3 impulsiveTorque = impulse % relativeContactPosition;
Vector3 rotationChange =
inverseInertiaTensor.transform(impulsiveTorque);
These calculations work for the first object in the collision but not for the second,
if there is one. To apply the impulse to the second object, we first need to make an
observation. We have calculated a single value for the impulse, but there may be two
objects involved in the collision.
Just as in chapter 7, both objects involved in a collision will receive the same sized
impulse, but in opposite directions. And as we saw in chapter 2, changing the direc-
tion of a vector to its opposite is equivalent to changing the sign of all its components.
We have worked so far using the contact normal as it was generated by the colli-
sion detector. By convention the collision detector generates a contact normal from