views:

381

answers:

2

I have a collision resolution method in my physics engine, that goes like this:

Vector2 n1pos = n1.NonLinearSpace != null ? n1.NonLinearPosition : n1.Position;
Vector2 n2pos = n2.NonLinearSpace != null ? n2.NonLinearPosition : n2.Position;
Vector2 posDiff = n2pos - n1pos;
Vector2 posDiffNormal = posDiff;
posDiffNormal.Normalize();
float totalRadius = n1.Radius + n2.Radius;
float posDiffLength = posDiff.Length();
float interPenetration = totalRadius - posDiffLength;
float averageRestitution = (n1.RestitutionCoefficient + n2.RestitutionCoefficient) / 2;

Vector2 forceAmount = Vector2.Multiply(posDiffNormal, interPenetration);
Vector2 n1force =
    (
        (n1.Velocity * n1.Mass) +
        (n2.Velocity * n2.Mass) +
        n2.Mass * averageRestitution * (n2.Velocity - n1.Velocity)
    ) /
    (n1.Mass + n2.Mass);
Vector2 n2force =
    (
        (n1.Velocity * n1.Mass) +
        (n2.Velocity * n2.Mass) +
        n1.Mass * averageRestitution * (n2.Velocity - n1.Velocity)
    ) /
    (n1.Mass + n2.Mass);
n1.ApplyForce(???);
if (!n1.IsStatic)
{
    n1.Position += ???;
}
n2.ApplyForce(???);
if (!n2.IsStatic)
{
    n2.Position += ???;
}

Now, i can't figure out what to apply to the bodies in my engine in order to get proper coefficient of restitution working. (the ??? parts). Can someone help?

+4  A: 
Beta
Well see, that's the thing: I learn much more effectively by example than a lot of math equations and rules set down in a book. Now,that said, I did get a chance to study a physics engine and mostly mocked that architecture. The way collisions are calculated right now, all collisions are completely elastic, so I can't start with "putty" collisions. The method I tried for integrating the CoR calculations was incorrect, so your help is much appreciated.
RCIX
+1  A: 

I realize this is an old question, but I ran into the same issue and Google turned up this page. I figured I might share my findings. First, you must realize that the coefficient of restitution is a property of the collision, not of either of the bodies involved in the collision. That is, for n objects, you need to define n(n-1)/2 coefficients of restitution (one for each pair of bodies).

However, the physics engines I have looked into (Bullet, Chipmunk, and Box2d) all define the restitution as a property of the bodies. Upon the time of the collision, they combine the two bodies' coefficients of restitution into a single value and use that in the collision resolution. Obviously, this isn't physically correct. But that doesn't matter much for games: it just needs to behave in an intuitive manner. Here are the restitution functions that those physics engines use:

  • Bullet: restitution = body1->restitution * body2->restitution
  • Chipmunk: restitution = body1->restitution * body2->restitution
  • Box2d: restitution = max(body1->restitution, body2->restitution)

Box2d allows the users to customize the restitution function in a configuration file. Bullet and Chipmunk do not.

I recommend you select whatever restitution mixing function feels best. Just play around with it a bit and see what you like.

David