Game Development Reference
In-Depth Information
The contact basis method was described in section 14.2.1. The relative posi-
tion calculation should be rather straightforward. The remaining two components—
swapping bodies and calculating relative velocity—deserve some comment.
Swapping Bodies
The first two lines make sure that, if there is only one object in the collision, it is in the
zero position of the array. So far we have assumed that this is true. If your collision
detector is guaranteed to only return single-object collisions in this way, then you can
ignore this code.
To swap the bodies we need to move the two body references and also reverse the
direction of the contact normal. The contact normal is always given from the first
object's point of view. If the bodies are swapped, then this needs to be flipped:
Excerpt from src/contacts.cpp
/**
* Swaps the bodies in the current contact, so body 0 is at body 1 and
* vice versa. This also changes the direction of the contact normal, but
* doesn't update any calculated internal data. If you are calling this
* method manually, then call calculateInternals afterward to make sure
* the internal data is up to date.
*/
void Contact::swapBodies()
{
contactNormal *= -1;
RigidBody *temp = body[0];
body[0] = body[1];
body[1] = temp;
}
Calculating Relative Velocity
The relative velocity we are interested in is the total closing velocity of both objects
at the contact point. This will be used to work out the desired final velocity after the
objects bounce.
The velocity needs to be given in contact coordinates. Its x valuewillgivethe
velocity in the direction of the contact normal, and its y and z values will give the
amount of sliding that is taking place at the contact. We'll use these two values in the
next chapter when we meet friction.
Velocity at a point, as we have seen, is made up of both linear and angular com-
ponents:
q rel = θ
q rel + ˙
˙
×
p