Game Development Reference
In-Depth Information
With CheckForCollision out of the way, turn your attention to ApplyImpulse , which
also has to be revised to include angular effects. Specifically, you need to use the impulse
formula that includes angular as well as linear effects (see Chapter 5 ), and you also have
to apply the impulse to the hovercraft's angular velocities in addition to their linear
velocities. Here's how the new ApplyImpulse function looks:
void ApplyImpulse(pRigidBody2D body1, pRigidBody2D body2)
{
float j;
j = (-(1+fCr) * (vRelativeVelocity*vCollisionNormal)) /
( (1/body1->fMass + 1/body2->fMass) +
(vCollisionNormal * (((body1->vCollisionPoint ^
vCollisionNormal)/body1->fInertia)^body1->vCollisionPoint)) +
(vCollisionNormal * (((body2->vCollisionPoint ^
vCollisionNormal)/body2->fInertia)^body2->vCollisionPoint))
);
body1->vVelocity += (j * vCollisionNormal) / body1->fMass;
body1->vAngularVelocity += (body1->vCollisionPoint ^
(j * vCollisionNormal)) /
body1->fInertia;
body2->vVelocity -= (j * vCollisionNormal) / body2->fMass;
body2->vAngularVelocity -= (body2->vCollisionPoint ^
(j * vCollisionNormal)) /
body2->fInertia;
}
Remember, the impulse is applied to one hovercraft while its negative is applied to the
other.
That does it for this new version of the hovercraft simulation. If you run the program
now, you'll see that you can crash the hovercraft into each other and they bounce and
rotate accordingly. This makes for a much more realistic simulation than the simple,
linear collision response approach of the last section. Here again, you can play with the
mass of each hovercraft and the coefficient of restitution to see how these parameters
affect the collision response between the hovercraft.