Restitution calculation

When two bodies collide their restitution must be computed. The current formula is:
restitution = max( a.restitution, b.restitution )

I think this is wrong. Imagine a hackysack(0% restitution) and que ball(100% restitution) of equal weight hit each other, (ok imagine really hard ). What should happen? I imagine the bodies would have a inelastic collision and fall to floor together.

The current formula would give them a perfectly elastic collision. They would bounce off each other.

-nick
The other alternative I considered was the geometric mean:
restitution = sqrt(a.restitution * b.restitution)

This would work for the case you give. However, the problem with this is figuring out how to set the restitution for the ground. We can set it to 1, but then every object would have a restitution:

restitution = sqrt(1 * b.restitution) = sqrt(b.restitution)

So if implement that forum someone will complain that their objects are too bouncy.

sqrt(0.25) = 0.5
sqrt(0.5) = 0.71
sqrt(0.75) = 0.97

The problem is people usually don't think about the restitution of the ground, they just want to tune the restitution of their objects.

I suppose I could use the square of the specified value. I'm not sure.

Do you have a better solution?
In the real world restitution is really a property of the collision, not the object, so it's conceivable that it could require pairwise definition to be accurate. If you include chemical and other effects, this is certainly the case (a ping-pong ball bouncing off a land mine trigger will have a COR very slightly less than 1, but a rock falling on it will trigger the explosion, leading to a COR much greater than 1, and that has more to do with the mass than the material).

However, the interactions that cause the types of collisions we're interested in are more dependent on gross material properties than subtle ones (like the existence of gunpowder and a triggering mechanism, for instance!), so there probably is a quantity you could assign to each object that, when combined in some way, provides a reasonable estimate of the COR for the collision. It's probably worth noting that the per-body quantity might not have the same physical meaning as the coefficient of restitution we're used to using (the units will depend on the formula used to combine them). I have an idea how to prove what the "right" formula should be, I'll let you know if I crack it.

The right solution (that will satisfy people, i.e. if restitution for a body is 0.5, it will lose half its energy on ground impact) might be to convert the input restitution values internally to the pseudo-restitution values that they would need to have to have exactly that COR with the ground, so they could be appropriately combined. If sqrt(a.restitution * b.restitution) matches the physical result, that would just amount to using the square (as Erin mentioned), since you're essentially just mapping the specified restitution by a monotone function to match user expectations, which is fine, since you're literally redefining the single-body "restitution" to mean "this body should have such and such a COR with a solid wall." If it's a more complicated dependency, it could get nastier to implement, though.

Intuitively, the product of single-body restitutions seems like the right way to go to me, as I would expect two bodies, each with individual restitutions of 0.5, to lose more energy by colliding with each other than they would with the wall.
Thinking about this a little, I'm starting to think that the most appropriate calculation should be min(a.restitution,b.restitution).

See if this makes sense: based on a simple symmetry argument, a head on collision between a body and a "perfect" wall should be exactly equivalent (as far as that body is concerned) to a head on collision with a mirror image of itself coming from the other direction. Applying this logic, it's clear that if the COR with the wall is x, the COR with a mirror image of the body better damn well be x as well! Using the max of the two values doesn't make sense, since as Caspin suggested, something like a hackey sack is going to have a COR of near zero with everything it hits, and using any sort of function to combine the two values would make the body/wall vs. body/mirrored body thought experiment fail.

Following this logic through, the pseudo-COR (i.e. what is assigned to the body) of a "perfect" wall should actually be infinite, rather than zero or one, as what a perfect wall really does is allow the COR of the body to determine the collision characteristics in every case.

(edit: grammar)
I too didn't understand why the restitution was set to max(r1, r2) when simple models I've seen of elasticity were more like min(r1, r2).

Having a wall with r1=1 and a ball with r=0.6 makes a collision with resulting r=0.6 makes sense to me, but now I have to set all my walls with r=0 for the same effect.

Using sqrt(r1*r2) would make walls more bouncy (sqrt(1 * 0.6) = .77), while just using r1*r2 (I think elasticity is dimensionless so the sqrt is not needed for units) will always be less bouncy than the min for values of r < 1. R1*r2 might not be such a good model for values of r > 1, so perhaps min(r1, r2, r1*r2) might be a good model.
The problem with min/max is that wall restitution either needs to be infinite/0, resp, in order to match expectations.

My problem with involving r1*r2 at all is that I can't think of any cases where it turns out to be the right answer - the "obvious" one to me was if you had two balls, each with a bounce-on-floor restitution of .5, and I thought that against each other the restitution should be .25, but the fact is, that's wrong. It should absolutely be .5, since any other value would imply either a) that the bounce with the floor was imperfect (appreciable floor deformation), which by assumption is not true, or b) a mirrored collision of an object with itself will not be symmetrical, which can't be true either.

The only problem with the min() is that a perfect floor needs infinite COR (probably just some fairly large value in practice), but to me it still seems the only consistent way to handle it.
I'm fine with r = min(r1, r2) and the default restitution is 0. The only trouble is that people will have to set floor restitutions to be a large number. I can provide a constant:

Code: Select all

const float32 b2_floorRestitution = FLT_MAX;


Can we reach a consensus?
Looking at the literature, it seems that min() is the function most often used for rigid bodies in computer simulations. Restitution values are limited to from 0 .. 1. I think it is nice to have the ability to specify larger restitutions just to make game design easier, so defaulting the ground to 1 (not 0) is probably the right thing to do, but allowing someone to specify a larger value to allow superbouncing should be supported.

That said, the min() function makes some assumptions that are, in general, not true. The main one is that elasticity is linear (not a function of impact velocity) and the other is that the body is not deformed (also a non-linear effect).

The bottom line might be more about what makes games easier to design than it is what happens in the physics.
The problem with any default restitution is that it is set per-shape and not per-body, so there is no way for Box2d to set a default value for floors (static bodies) different from the default for dynamic bodies. With max() this worked okay, but assuming we agree that min() is a better combination function, we can't set any default value without causing some weird things to happen if you forget to change it. Yet it would be a shame to add another forced step to the creation of each static body to get the results you expect.

My solution would probably be to make the restitution value on shapes private, and insist that a b2ShapeDef::SetRestitution call be used which toggles an isRestitutionSet flag to true. That way as the shapes are added to bodies, the flag could be checked; if it's set, use the user's value no matter what, if not, use the default value appropriate to the b2Body it's being added to (0 if dynamic, b2_floorRestitution if static). This could cause some real trouble if people go around changing static bodies into dynamic ones, but I've yet to see anyone do such a thing, so I'm not too worried about it.

While we're talking about this type of thing, do we all agree that sqrt(a.friction*b.friction) is the "proper" way to combine friction constants? I'm okay with this, and it seems to matter less since the values people put in don't directly mean as much to people (restitution = fraction of height regained on a single bounce, so it's pretty easy to see). But if anyone has a problem with it, now would probably be a good time...
If you want dynamic and static bodies to have different defaults then you can set the value in the def constructor to "use default" (perhaps a negative value) and then check for "use_default" when the shape is added to the body, assigning the default then.

Also, if you use FLOAT32_MAX instead of FLT_MAX then the fixed point code will work too 🙂

One other issue here is that making this change will have a big impact on all current users. There is a big difference between min and max.

Original thread: https://box2d.org/forum/viewtopic.php?t=659