Yeah, instead of making restitution private, I can just use a default value of -1, which means use 0 for dynamic bodies and FLOAT32_MAX for static bodies.
Restitution calculation
Bugs, Requests, and FeedbackSounds good to me, my irrational Java guilt over public fields notwithstanding. 🙂
Think of defs as plain old data and you'll be fine. 🙂
I though it would help sort this out if we could see what the functions do.
I hope we all agree with the following table. I've included a 2.00 restitution to show what could be done.
rest = a.rest * b.rest;
I think this is the best function. The first table looks a lot like a multiplication table. This table make sense. My only compliant is it tends to produce low CoR's. If you integrate this function from 0 to 1 you get 1/4. Which means on average this function will return 1/4. Kinda low. This easily corrected by the use however, for instance only use restitutions above .5 unless it should be 0.
rest = sqrt( a.rest * b.rest )
This function still obeys that 1.00 against 1.00 is 1.00 and anything against 0.00 is 0.00. Also it exhibits the behavior that Eric suggested would be correct, where mirror objects will have a CoR that is the same as their individual restitution. The average value returns by this function is 4/9. that's pretty close to .5 which i believe would be ideal. The main disadvantages of this function are it has a sqrt (slow ... maybe) and object colliding with a perfectly elastic wall (1.00) will bounce with a CoR that is slightly higher than the objects CoR.
Out of curiosity I had Maxima solve for the root that would give a 1/2 average. It is (a.rest * b.rest) ^ (sqrt(2)-1).
I think the best function would be a blending of these two. An average close to .5 and the behavior outlined in the first table. I fear that a function that does would be prohibitive to compute. Sort of a lerp between the two functions that depends on how close either of the restitutions is to 1.00. Ideally i would
To fix all this it might be wise to have Box2D provide a callback interface similar to listeners that can calculate a restitution for two bodies. The callback could accept the shapes and the restitutions as parameters, so to provide the ultimate in flexibility.
I hope we all agree with the following table. I've included a 2.00 restitution to show what could be done.
Code: Select all
x.xx | 0.00 | 0.25 | 0.50 | 0.75 | 1.00 | 2.00
0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00
0.25 | 0.00 | ?.?? | ?.?? | ?.?? | 0.25 | ?.??
0.50 | 0.00 | ?.?? | ?.?? | ?.?? | 0.50 | ?.??
0.75 | 0.00 | ?.?? | ?.?? | ?.?? | 0.75 | ?.??
1.00 | 0.00 | 0.25 | 0.50 | 0.75 | 1.00 | 2.00
2.00 | 0.00 | ?.?? | ?.?? | ?.?? | 2.00 | ?.??
rest = a.rest * b.rest;
I think this is the best function. The first table looks a lot like a multiplication table. This table make sense. My only compliant is it tends to produce low CoR's. If you integrate this function from 0 to 1 you get 1/4. Which means on average this function will return 1/4. Kinda low. This easily corrected by the use however, for instance only use restitutions above .5 unless it should be 0.
Code: Select all
x.xx | 0.00 | 0.10 | 0.20 | 0.30 | 0.40 | 0.50 | 0.60 | 0.70 | 0.80 | 0.90 | 1.00 | 1.50 | 2.00
0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00
0.10 | 0.00 | 0.01 | 0.02 | 0.03 | 0.04 | 0.05 | 0.06 | 0.07 | 0.08 | 0.09 | 0.10 | 0.15 | 0.20
0.20 | 0.00 | 0.02 | 0.04 | 0.06 | 0.08 | 0.10 | 0.12 | 0.14 | 0.16 | 0.18 | 0.20 | 0.30 | 0.40
0.30 | 0.00 | 0.03 | 0.06 | 0.09 | 0.12 | 0.15 | 0.18 | 0.21 | 0.24 | 0.27 | 0.30 | 0.45 | 0.60
0.40 | 0.00 | 0.04 | 0.08 | 0.12 | 0.16 | 0.20 | 0.24 | 0.28 | 0.32 | 0.36 | 0.40 | 0.60 | 0.80
0.50 | 0.00 | 0.05 | 0.10 | 0.15 | 0.20 | 0.25 | 0.30 | 0.35 | 0.40 | 0.45 | 0.50 | 0.75 | 1.00
0.60 | 0.00 | 0.06 | 0.12 | 0.18 | 0.24 | 0.30 | 0.36 | 0.42 | 0.48 | 0.54 | 0.60 | 0.90 | 1.20
0.70 | 0.00 | 0.07 | 0.14 | 0.21 | 0.28 | 0.35 | 0.42 | 0.49 | 0.56 | 0.63 | 0.70 | 1.05 | 1.40
0.80 | 0.00 | 0.08 | 0.16 | 0.24 | 0.32 | 0.40 | 0.48 | 0.56 | 0.64 | 0.72 | 0.80 | 1.20 | 1.60
0.90 | 0.00 | 0.09 | 0.18 | 0.27 | 0.36 | 0.45 | 0.54 | 0.63 | 0.72 | 0.81 | 0.90 | 1.35 | 1.80
1.00 | 0.00 | 0.10 | 0.20 | 0.30 | 0.40 | 0.50 | 0.60 | 0.70 | 0.80 | 0.90 | 1.00 | 1.50 | 2.00
1.50 | 0.00 | 0.15 | 0.30 | 0.45 | 0.60 | 0.75 | 0.90 | 1.05 | 1.20 | 1.35 | 1.50 | 2.25 | 3.00
2.00 | 0.00 | 0.20 | 0.40 | 0.60 | 0.80 | 1.00 | 1.20 | 1.40 | 1.60 | 1.80 | 2.00 | 3.00 | 4.00
rest = sqrt( a.rest * b.rest )
This function still obeys that 1.00 against 1.00 is 1.00 and anything against 0.00 is 0.00. Also it exhibits the behavior that Eric suggested would be correct, where mirror objects will have a CoR that is the same as their individual restitution. The average value returns by this function is 4/9. that's pretty close to .5 which i believe would be ideal. The main disadvantages of this function are it has a sqrt (slow ... maybe) and object colliding with a perfectly elastic wall (1.00) will bounce with a CoR that is slightly higher than the objects CoR.
Out of curiosity I had Maxima solve for the root that would give a 1/2 average. It is (a.rest * b.rest) ^ (sqrt(2)-1).
Code: Select all
x.xx | 0.00 | 0.10 | 0.20 | 0.30 | 0.40 | 0.50 | 0.60 | 0.70 | 0.80 | 0.90 | 1.00 | 1.50 | 2.00
0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00
0.10 | 0.00 | 0.10 | 0.14 | 0.17 | 0.20 | 0.22 | 0.24 | 0.26 | 0.28 | 0.30 | 0.32 | 0.39 | 0.45
0.20 | 0.00 | 0.14 | 0.20 | 0.24 | 0.28 | 0.32 | 0.35 | 0.37 | 0.40 | 0.42 | 0.45 | 0.55 | 0.63
0.30 | 0.00 | 0.17 | 0.24 | 0.30 | 0.35 | 0.39 | 0.42 | 0.46 | 0.49 | 0.52 | 0.55 | 0.67 | 0.77
0.40 | 0.00 | 0.20 | 0.28 | 0.35 | 0.40 | 0.45 | 0.49 | 0.53 | 0.57 | 0.60 | 0.63 | 0.77 | 0.89
0.50 | 0.00 | 0.22 | 0.32 | 0.39 | 0.45 | 0.50 | 0.55 | 0.59 | 0.63 | 0.67 | 0.71 | 0.87 | 1.00
0.60 | 0.00 | 0.24 | 0.35 | 0.42 | 0.49 | 0.55 | 0.60 | 0.65 | 0.69 | 0.73 | 0.77 | 0.95 | 1.10
0.70 | 0.00 | 0.26 | 0.37 | 0.46 | 0.53 | 0.59 | 0.65 | 0.70 | 0.75 | 0.79 | 0.84 | 1.02 | 1.18
0.80 | 0.00 | 0.28 | 0.40 | 0.49 | 0.57 | 0.63 | 0.69 | 0.75 | 0.80 | 0.85 | 0.89 | 1.10 | 1.26
0.90 | 0.00 | 0.30 | 0.42 | 0.52 | 0.60 | 0.67 | 0.73 | 0.79 | 0.85 | 0.90 | 0.95 | 1.16 | 1.34
1.00 | 0.00 | 0.32 | 0.45 | 0.55 | 0.63 | 0.71 | 0.77 | 0.84 | 0.89 | 0.95 | 1.00 | 1.22 | 1.41
1.50 | 0.00 | 0.39 | 0.55 | 0.67 | 0.77 | 0.87 | 0.95 | 1.02 | 1.10 | 1.16 | 1.22 | 1.50 | 1.73
2.00 | 0.00 | 0.45 | 0.63 | 0.77 | 0.89 | 1.00 | 1.10 | 1.18 | 1.26 | 1.34 | 1.41 | 1.73 | 2.00
I think the best function would be a blending of these two. An average close to .5 and the behavior outlined in the first table. I fear that a function that does would be prohibitive to compute. Sort of a lerp between the two functions that depends on how close either of the restitutions is to 1.00. Ideally i would
To fix all this it might be wise to have Box2D provide a callback interface similar to listeners that can calculate a restitution for two bodies. The callback could accept the shapes and the restitutions as parameters, so to provide the ultimate in flexibility.
Code: Select all
if( ! m_restitution_cb )
{
restitution = a.m_restitution * b.m_restitution;
}
else
{
restitution = m_restitution_cb( a, a.m_restitution, b, b.m_restitution );
}
I think a callback would be overkill for most users. If we can't come to an agreement about a simple function, I will just make some inline function in b2Settings that people can override.
First off, let me preface this by saying that as far as I'm concerned, this is a very minor issue - I don't think anyone is having trouble getting the effects they want out of this engine as it is, and I don't think any change in the specific function used will make a huge difference to most people. There are arguments in favor of each of min, max, and a product (or root of product), and when it really comes down to it I don't care very much which one wins, though I'm slightly biased towards min because I prefer to err on the side of less violent reactions.
[If you don't want to read this whole post, the quick version is that I ran some tests and it turns out that none of those formulas are right in general, and there's probably no such formula with any sort of simple proof behind it (though there might be a decent formula for objects satisfying some reasonable constraints, it would have to be inferred from experiment, simulation, or statistical mechanics, not from a simple physical argument).]
That said, I am a physics dork, so I'm curious - shockingly, I could not find much hard science on this particular issue, and I couldn't figure out a theoretical proof for any particular formula. So, I actually sat down tonight and ran some tests in Box2d to get an idea what's going on here. Restitution is a high level concept, and the low level cause is the way materials bend and shake when they collide - the most minimal, oversimplified example of such a thing that is easy to simulate is a couple particles connected by a damped spring.
Here's what I did:
1) Turned gravity off
2) Created two "objects," each consisting of two particles and a spring, with different damping constants on the spring
3) Measured the wall CORs of these objects (with the individual particle/wall COR set to zero, so the max COR here is .5 - all the bounce comes from the spring) by checking pre-collision and post-collision velocities
4) Bounced them against each other (particle-particle COR of 0), and measured the resulting pairwise COR
Here are a few results:
As you can see, if the individual CORs are equal, the pairwise COR turns out the same, as claimed before (conservation of momentum can be used to show this in general if the objects are identical). Otherwise, it comes up somewhere in between, but it's neither min, max, nor a product or square root of a product (nope, not an average, either). Further, by mucking with the spring constant (hence time scale) of each spring, it's possible to make the resulting CORs change for the same inputs. If you look at the third example, you see that the COR for something that's basically got a COR of .5 and one that's basically 0 comes out to .3 when measured with spring contraptions; yet as discussed earlier, you'd be hard pressed to find anything at all that a hacky sack (COR 0) will bounce off of with a resulting COR of .3, which means you can't put a formula on this stuff in general.
Since this is an oversimplified example, don't read anything much into those values. The point is that the combination formula for unequal CORs is dependent on a lot of things, and there's no clean formula that can get it right, unless you want to add a whole slew of other independent variables. And if you want to go that route, COR is often very dependent on velocity and angle of impact as well, so good luck! (some researchers even discovered that certain oblique collisions between normal objects and soft objects can have CORs of over 1 due to local surface deformation on impact)
If someone wanted to pursue this further, it would be an interesting bit of physics to see what emerges as a formula when you bind large numbers of particles into a larger elastic solid using a similar testing methodology (though you'd need to turn the time step down to get realistic stiffness and stability if you use simple springs); there probably is a clear and fairly stable dependence there, at least on average and as you take some limit, though I'm not sure the formula will be all too pretty.
[If you don't want to read this whole post, the quick version is that I ran some tests and it turns out that none of those formulas are right in general, and there's probably no such formula with any sort of simple proof behind it (though there might be a decent formula for objects satisfying some reasonable constraints, it would have to be inferred from experiment, simulation, or statistical mechanics, not from a simple physical argument).]
That said, I am a physics dork, so I'm curious - shockingly, I could not find much hard science on this particular issue, and I couldn't figure out a theoretical proof for any particular formula. So, I actually sat down tonight and ran some tests in Box2d to get an idea what's going on here. Restitution is a high level concept, and the low level cause is the way materials bend and shake when they collide - the most minimal, oversimplified example of such a thing that is easy to simulate is a couple particles connected by a damped spring.
Here's what I did:
1) Turned gravity off
2) Created two "objects," each consisting of two particles and a spring, with different damping constants on the spring
3) Measured the wall CORs of these objects (with the individual particle/wall COR set to zero, so the max COR here is .5 - all the bounce comes from the spring) by checking pre-collision and post-collision velocities
4) Bounced them against each other (particle-particle COR of 0), and measured the resulting pairwise COR
Here are a few results:
Code: Select all
COR A: .4962687
COR B: .4029491
COR A+B: .4539108
A: .4962688
B: .1008021
A+B: .3504858
A: .50310
B: .038577
A+B: .33897
A: .1008021
B: .1008021
A+B: .1008
A: .4962687
B: .4962687
A+B: .4961431
As you can see, if the individual CORs are equal, the pairwise COR turns out the same, as claimed before (conservation of momentum can be used to show this in general if the objects are identical). Otherwise, it comes up somewhere in between, but it's neither min, max, nor a product or square root of a product (nope, not an average, either). Further, by mucking with the spring constant (hence time scale) of each spring, it's possible to make the resulting CORs change for the same inputs. If you look at the third example, you see that the COR for something that's basically got a COR of .5 and one that's basically 0 comes out to .3 when measured with spring contraptions; yet as discussed earlier, you'd be hard pressed to find anything at all that a hacky sack (COR 0) will bounce off of with a resulting COR of .3, which means you can't put a formula on this stuff in general.
Since this is an oversimplified example, don't read anything much into those values. The point is that the combination formula for unequal CORs is dependent on a lot of things, and there's no clean formula that can get it right, unless you want to add a whole slew of other independent variables. And if you want to go that route, COR is often very dependent on velocity and angle of impact as well, so good luck! (some researchers even discovered that certain oblique collisions between normal objects and soft objects can have CORs of over 1 due to local surface deformation on impact)
If someone wanted to pursue this further, it would be an interesting bit of physics to see what emerges as a formula when you bind large numbers of particles into a larger elastic solid using a similar testing methodology (though you'd need to turn the time step down to get realistic stiffness and stability if you use simple springs); there probably is a clear and fairly stable dependence there, at least on average and as you take some limit, though I'm not sure the formula will be all too pretty.
MEGABUMP!
Hi, I figured this might be a good place to put this. I want to use the geometric mean (root product) rather than the maximum of the two restitutions. How might I go about changing that in the source? Apologies if this has already been answered.
More info: I used the automatic Box2D installer for Windows (Box2D-2.3b0.win32-py2.7.exe).
Hi, I figured this might be a good place to put this. I want to use the geometric mean (root product) rather than the maximum of the two restitutions. How might I go about changing that in the source? Apologies if this has already been answered.
More info: I used the automatic Box2D installer for Windows (Box2D-2.3b0.win32-py2.7.exe).
You can change the b2MixRestitution function:
https://code.google.com/p/box2d/source/ ... ntact.h#42
https://code.google.com/p/box2d/source/ ... ntact.h#42
Thanks... how can I do that in pybox2D? The b2MixRestitution function in "Box2D.py" just redirects to _Box2D, which is a builtin, and the file "b2Contact.h" does not exist on my computer.
Original thread: https://box2d.org/forum/viewtopic.php?t=659