Game Development Reference
In-Depth Information
// Calculate the new separating velocity.
real newSepVelocity = -separatingVelocity * restitution;
// Check the velocity build-up due to acceleration only.
Vector3 accCausedVelocity = particle[0]->getAcceleration();
if (particle[1]) accCausedVelocity -= particle[1]->getAcceleration();
real accCausedSepVelocity = accCausedVelocity *
contactNormal * duration;
// If we've got a closing velocity due to acceleration build-up,
// remove it from the new separating velocity.
if (accCausedSepVelocity < 0)
{
newSepVelocity += restitution * accCausedSepVelocity;
// Make sure we haven't removed more than was
// there to remove.
if (newSepVelocity < 0) newSepVelocity = 0;
}
real deltaVelocity = newSepVelocity - separatingVelocity;
// We apply the change in velocity to each object in proportion to
// its inverse mass (i.e., those with lower inverse mass [higher
// actual mass] get less change in velocity).
real totalInverseMass = particle[0]->getInverseMass();
if (particle[1]) totalInverseMass += particle[1]->getInverseMass();
// If all particles have infinite mass, then impulses have no effect.
if (totalInverseMass <= 0) return;
// Calculate the impulse to apply.
real impulse = deltaVelocity / totalInverseMass;
// Find the amount of impulse per unit of inverse mass.
Vector3 impulsePerIMass = contactNormal * impulse;
// 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])

Custom Search