Unexplained behaviour with rigidbody applyImpulse

Apollo
Posts: 17
Joined: Wed Dec 21, 2016 1:15 pm

Unexplained behaviour with rigidbody applyImpulse

Post by Apollo » Mon May 13, 2019 6:24 pm

Hi,

I am trying to create a billiards/pool game, but am running into a problem with applying impulses to the spheres to simulate top spin. When I apply the impulse centrally (using applyCentralImpulse), there is no issue. Everything behaves as I would expect it to. When I use applyImpulse to simulate back-spin, using the following code, everything behaves as I would expect it to.

Code: Select all

btVector3 halfSize = boundingBox.getHalfSize();
rigidbody->applyImpulse(btVector3(_direction * _power), btVector3(0, -halfSize.y * 0.25f, 0));
When I use applyImpulse to simulate top-spin however, using the code shown below, the behaviour is completely unexpected. The direction of the object ball that was hit is completely different to the first two scenarios described, and I'm at a loss to explain why.

Code: Select all

btVector3 halfSize = boundingBox().getHalfSize();
rigidbody->applyImpulse(btVector3(_direction * _power), btVector3(0, halfSize.y * 0.25f, 0));
For all three scenarios, before forces are applied, the positions are identical, the direction of the shot is identical, the only thing that changes is the applyCentralImpulse/applyImpulse call, and I can't see any explanation as to why the top-spin function should result in different behaviour. The rigidbody variables setup code for the balls are shown below:

Code: Select all

rigidBody->setFriction(0.9f);
rigidBody->setRestitution(0.9f);
rigidBody->setDamping(0.6f, 0.75f); 
rigidBody->setContactProcessingThreshold(0);
rigidBody->setCcdMotionThreshold(0.000001f);
rigidBody->setCcdSweptSphereRadius(0.30f);
rigidBody->setActivationState(DISABLE_DEACTIVATION);
Any suggestions of what could cause the strange behaviour in the top-spin scenario only are welcome. Thanks!

steven
Posts: 54
Joined: Mon Nov 05, 2018 8:16 am
Location: China

Re: Unexplained behaviour with rigidbody applyImpulse

Post by steven » Tue May 14, 2019 1:14 pm

Could you provide some simple example? thanks.

User avatar
drleviathan
Posts: 527
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Unexplained behaviour with rigidbody applyImpulse

Post by drleviathan » Tue May 14, 2019 1:32 pm

You say:
...the behaviour is completely unexpected. The direction of the object ball that was hit is completely different to the first two scenarios described
All we know is that it is "different" but you don't otherwise describe it. It is difficult to come up with ideas without having some idea as to the how the behavior is wrong. Perhaps you could explain how it is wrong, or perhaps supply a video of the good and bad behaviors for comparison.

Meanwhile some questions come to mind:

(1) I assume you are using btSphereShape for you balls, but what btCollisionShape are you using for the table? Is it concave or convex? Do you do anything funny with its margin?

(2) What are the dimensions of your billiard balls? What is your substep duration?

In particular, I'm wondering if your ball is somehow penetrating the table and either picking up a strange normal or getting into a situation where Bullet tries to resolve penetration rather than relying on "normal" collisions.

Apollo
Posts: 17
Joined: Wed Dec 21, 2016 1:15 pm

Re: Unexplained behaviour with rigidbody applyImpulse

Post by Apollo » Wed May 15, 2019 2:28 pm

steven wrote:
Tue May 14, 2019 1:14 pm
Could you provide some simple example? thanks.
drleviathan wrote:
Tue May 14, 2019 1:32 pm
You say:
...the behaviour is completely unexpected. The direction of the object ball that was hit is completely different to the first two scenarios described
All we know is that it is "different" but you don't otherwise describe it. It is difficult to come up with ideas without having some idea as to the how the behavior is wrong. Perhaps you could explain how it is wrong, or perhaps supply a video of the good and bad behaviors for comparison.

Meanwhile some questions come to mind:

(1) I assume you are using btSphereShape for you balls, but what btCollisionShape are you using for the table? Is it concave or convex? Do you do anything funny with its margin?

(2) What are the dimensions of your billiard balls? What is your substep duration?

In particular, I'm wondering if your ball is somehow penetrating the table and either picking up a strange normal or getting into a situation where Bullet tries to resolve penetration rather than relying on "normal" collisions.
So by behaviour, I literally mean that the resultant direction of the ball that was hit by the cue ball is different when I apply top spin to the cue ball. I would expect the cue ball to move differently (which it does), after all it is the one the spin is being applied to, but the object ball should move in the same direction regardless of the spin on the cue ball. The image below is the easiest way I could think of to demonstrate the problem.

So the red line is the direction that the blue ball should take once it has been hit. When I use apply central impulse to move the white ball, the resultant collision with the blue ball sends the blue ball in the direction of the red line. The same when I use applyImpulse to apply back spin. But when I use applyImpulse to apply top-spin, the blue ball moves in the direction of the black line. That should not happen. The blue ball should be moving in the same direction regardless of what spin is applied to the cue ball.

And sorry, I'll try and provide some additional information. The balls are btSphereShapes yes and have a radius of 0.3, and the table is a btBvhTriangleMeshShape. Additional info for the table is provided below:

Code: Select all

rigidBody->setFriction(0.9f);
rigidBody->setRestitution(0.9f);
rigidBody->setContactProcessingThreshold(0);
Here is an image of the debug drawer being rendered:

And finally, I set the number of substeps in stepSimulation to 10. Hopefully this extra info helps, thanks!
Last edited by Apollo on Thu May 16, 2019 3:24 pm, edited 1 time in total.

User avatar
drleviathan
Posts: 527
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Unexplained behaviour with rigidbody applyImpulse

Post by drleviathan » Wed May 15, 2019 3:36 pm

Hrm... in the real world I would expect the spin on the cue to change the direction of the colored ball even when they collide in exactly the same spot. This is because both linear and angular momentum are conserved and the table is also involved. The balance of forces at the moment of impact will depend on the spin --> angular energy will be converted to linear or visa-versa depending on the the inputs. The fact that back-spin doesn't change the direction is surprising.

Apollo
Posts: 17
Joined: Wed Dec 21, 2016 1:15 pm

Re: Unexplained behaviour with rigidbody applyImpulse

Post by Apollo » Wed May 15, 2019 3:48 pm

drleviathan wrote:
Wed May 15, 2019 3:36 pm
Hrm... in the real world I would expect the spin on the cue to change the direction of the colored ball even when they collide in exactly the same spot. This is because both linear and angular momentum are conserved and the table is also involved. The balance of forces at the moment of impact will depend on the spin --> angular energy will be converted to linear or visa-versa depending on the the inputs. The fact that back-spin doesn't change the direction is surprising.
The difference you'd expect to see though in the direction of the coloured ball is minimal, I think. There should be a slight deviation dependent on the spin of the cue ball, but in my example the difference is massive. The coloured ball moves off in a completely different direction. I'm really not sure what could cause such a massive deviation. I have tested this issue in a pool game for confirmation, and the object ball only slightly deviates dependent on the spin. It's direction is almost the same regardless, whereas like I say, in my game the difference is massive.

Apollo
Posts: 17
Joined: Wed Dec 21, 2016 1:15 pm

Re: Unexplained behaviour with rigidbody applyImpulse

Post by Apollo » Wed May 15, 2019 4:14 pm

I had a thought about what could be causing it, and I wondered what you think about it, @drleviathan ... could the size of the balls be the cause of the angle difference? As in, because the balls are relatively small, the angle difference isn't actually that big, it just gives the perception of it because of the small size? If that makes sense?

User avatar
drleviathan
Posts: 527
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Unexplained behaviour with rigidbody applyImpulse

Post by drleviathan » Wed May 15, 2019 6:35 pm

Well... the ball is moving in the same direction in each case so if it hits at a different spot I would expect it to only be a vertical difference. For small balls I would expect a vertical variation to still produce horizontal deflection differences, but I don't have a good intuition for how much those would be.

Another possibility comes to mind: variation based on collisions with the meshy table. Since the table model is using btBvhTriangleMeshShape then it is possible slight variations from spin could produce slight trajectory variations when colliding with edges of triangles or maybe colliding through to triangles on the other side. Try replacing the mesh with a btBoxShape to see if you still suffer the same degree of variations.

Yet another idea: if the inertia tensor is too big for its linear mass then I would expect spin variations on deflection angles to be higher. Dunno if this is a problem for you but it did occur to me.

Final idea: if you really want to get to the bottom of how the variation happens you might consider trying the simulation with zero gravity and no table to see how the table affects the result.

Apollo
Posts: 17
Joined: Wed Dec 21, 2016 1:15 pm

Re: Unexplained behaviour with rigidbody applyImpulse

Post by Apollo » Wed May 15, 2019 8:31 pm

drleviathan wrote:
Wed May 15, 2019 6:35 pm
Well... the ball is moving in the same direction in each case so if it hits at a different spot I would expect it to only be a vertical difference. For small balls I would expect a vertical variation to still produce horizontal deflection differences, but I don't have a good intuition for how much those would be.

Another possibility comes to mind: variation based on collisions with the meshy table. Since the table model is using btBvhTriangleMeshShape then it is possible slight variations from spin could produce slight trajectory variations when colliding with edges of triangles or maybe colliding through to triangles on the other side. Try replacing the mesh with a btBoxShape to see if you still suffer the same degree of variations.

Yet another idea: if the inertia tensor is too big for its linear mass then I would expect spin variations on deflection angles to be higher. Dunno if this is a problem for you but it did occur to me.

Final idea: if you really want to get to the bottom of how the variation happens you might consider trying the simulation with zero gravity and no table to see how the table affects the result.
Your inertia tensor idea, how would I go about investigating that? How do I know if it's too big for its mass?

Here are some results based on the suggestions you thought I should try:
- Per your suggestion I replaced the table collider with a btBoxShape, it still gets the different angle with top-spin.
- I ran the simulation with zero gravity but with the btBvhTriangleMeshShape, I couldn't really see a visible difference in angle, maybe slightly, but certainly much much less than with gravity on.
- With zero gravity and no table I can't see any difference in angle.

Does any of that help pinpoint the issue?

What does the fact that it appears to be resolved with no gravity mean for what the problem is?

User avatar
drleviathan
Posts: 527
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Unexplained behaviour with rigidbody applyImpulse

Post by drleviathan » Thu May 16, 2019 5:33 pm

The fact that removing the table makes the divergence vanish means (I think) that the collision really is a 3-body interaction rather than a 2-body. The table acts as a momentum reservoir the the forces must balance at the time of collision. As such, I believe the effect is real and valid.

Ignore the "too big inertia tensor" idea. For now, we'll assume it is set correctly.

BTW, looking back at your code... you're setting the linear and angular damping values to 0.6 and 0.75 respectively which I think is way too high. I would recommend the following:

(1) Change the parameters as follows:
ball linear and angular damping values = 0.0
reduce the table friction to 0.1
reduce table restitution to 0.1
set ball restitution to 0.99

(2) Then with gravity + table in place check the angular differences again. I would expect to see differences between all three cases: backspin, nospin, and forward spin. If not all three are different I would consider it unexpected but not disaster. Note it and continue...

(3) Drop a ball from some height directly onto the table and observe how high it bounces. Dial the table restitution up or down until it bounces the amount you would expect. Note: if your balls sometimes tunnel through your mesh table at reasonable drop heights then you either need to do some combination of: reduce your substep duration, enable continuous collision detection (CCD) for the balls, or use a convex shape (e.g. btBoxShape) for the table plane.

Ideally the table bumpers would be a separate RigidBody from the table surface with a lower restitution (and probably more friction than discovered in step (4) below).

(4) With a ball sitting on the table: give it topspin or backspin and observe how fast it picks up linear speed. Dial the table's friction up until the ball accelerates in a satisfactory way.

(5) Back to the colliding ball scenario...dial the ball linear damping up (keep angular damping at 0.0) until balls slow down satisfactorily. I would expect the needed linear damping to be smaller than 0.6.

That should be a decent way to tune a billiard table.

Apollo
Posts: 17
Joined: Wed Dec 21, 2016 1:15 pm

Re: Unexplained behaviour with rigidbody applyImpulse

Post by Apollo » Thu May 16, 2019 5:53 pm

drleviathan wrote:
Thu May 16, 2019 5:33 pm
The fact that removing the table makes the divergence vanish means (I think) that the collision really is a 3-body interaction rather than a 2-body. The table acts as a momentum reservoir the the forces must balance at the time of collision. As such, I believe the effect is real and valid.

Ignore the "too big inertia tensor" idea. For now, we'll assume it is set correctly.

BTW, looking back at your code... you're setting the linear and angular damping values to 0.6 and 0.75 respectively which I think is way too high. I would recommend the following:

(1) Change the parameters as follows:
ball linear and angular damping values = 0.0
reduce the table friction to 0.1
reduce table restitution to 0.1
set ball restitution to 0.99

(2) Then with gravity + table in place check the angular differences again. I would expect to see differences between all three cases: backspin, nospin, and forward spin. If not all three are different I would consider it unexpected but not disaster. Note it and continue...

(3) Drop a ball from some height directly onto the table and observe how high it bounces. Dial the table restitution up or down until it bounces the amount you would expect. Note: if your balls sometimes tunnel through your mesh table at reasonable drop heights then you either need to do some combination of: reduce your substep duration, enable continuous collision detection (CCD) for the balls, or use a convex shape (e.g. btBoxShape) for the table plane.

Ideally the table bumpers would be a separate RigidBody from the table surface with a lower restitution (and probably more friction than discovered in step (4) below).

(4) With a ball sitting on the table: give it topspin or backspin and observe how fast it picks up linear speed. Dial the table's friction up until the ball accelerates in a satisfactory way.

(5) Back to the colliding ball scenario...dial the ball linear damping up (keep angular damping at 0.0) until balls slow down satisfactorily. I would expect the needed linear damping to be smaller than 0.6.

That should be a decent way to tune a billiard table.
Thanks for the explanation! I will investigate the different values you have suggested and make a new post when I have the results. One thing to note, I used to have tunnelling (a while back) with the balls when they were moved at high speeds, upon research of how to resolve the problem I enabled CCD and that fixed the tunnelling completely.

Apollo
Posts: 17
Joined: Wed Dec 21, 2016 1:15 pm

Re: Unexplained behaviour with rigidbody applyImpulse

Post by Apollo » Thu May 16, 2019 6:49 pm

So I have adjusted the values to fit your recommendations, and I ended up back with the same issue of there being too large a difference when top spin was used. There was a difference between backspin and no-spin, but it was much smaller and seemed a lot more feasible. However, in changing these values that you suggested, I noticed something. The ball rigidbodies' friction were all set to 0.9. When I reduced that value to something a lot lower (I tested various values, and ended up deciding on 0.45 that gave the best effect), I found that the top-spin angle difference became much less severe. It looks much more how I would expect it to. There is a difference in the angle of the coloured ball between all three types of spin, but they are fairly small and that's how I think it should be. Do you know why the ball friction would make such an impact? Is that something that you would expect, because it was high at 0.9f?

Also, on a different note, you suggested to remove angular damping altogether. Was that something that was purely while I was testing the difference effect, or did you mean that it shouldn't have been enabled full stop? Because as you'd expect, now when the balls stop moving linearly, they are still moving angularly / rotating. Should I enable it again, just at a much lower value than the 0.75 it was previously?

Thanks for all of the help!

User avatar
drleviathan
Posts: 527
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Unexplained behaviour with rigidbody applyImpulse

Post by drleviathan » Thu May 16, 2019 7:28 pm

Oops, I forgot to mention lowering the ball friction to something very low and then dialing it back up. It is good to have it lower than 0.9: a ball is smooth after all. If you adjusted the table's friction first before setting the ball to lower friction I would recommend going through the table tuning again, but reducing both ball and table friction down to 0.2 or something and then dialing the table's friction higher until the spinning ball accelerates as you would expect: I would expect the table should have higher friction than the balls themselves. Correct relative friction coefficients help because then the ball-vs-ball friction forces are about right relative to ball-vs-table forces.

The true angular damping for a real billiard ball would be very very small. I was expecting the friction with the table to slow down the angular velocity. That is, as ball slows down linearly then the angular velocity would also decrease because the the two velocities are coupled due to friction with the table. The net effect being: both linear and angular velocities reduce over time.

If the balls continue to rotate when they are aren't moving linearly then something is off: friction isn't working.

It is ok to add some angular damping as necessary to get the balls to slow down during rolling, however I would recommend tuning that parameter last. Tune the restitution first, then the friction, and finally damping. The reason being: damping is the most "non-physically accurate" feature of the simulation. If you want the physics to be as "real" as possible it is best to minimize damping.

Apollo
Posts: 17
Joined: Wed Dec 21, 2016 1:15 pm

Re: Unexplained behaviour with rigidbody applyImpulse

Post by Apollo » Thu May 16, 2019 8:14 pm

drleviathan wrote:
Thu May 16, 2019 7:28 pm
Oops, I forgot to mention lowering the ball friction to something very low and then dialing it back up. It is good to have it lower than 0.9: a ball is smooth after all. If you adjusted the table's friction first before setting the ball to lower friction I would recommend going through the table tuning again, but reducing both ball and table friction down to 0.2 or something and then dialing the table's friction higher until the spinning ball accelerates as you would expect: I would expect the table should have higher friction than the balls themselves. Correct relative friction coefficients help because then the ball-vs-ball friction forces are about right relative to ball-vs-table forces.

The true angular damping for a real billiard ball would be very very small. I was expecting the friction with the table to slow down the angular velocity. That is, as ball slows down linearly then the angular velocity would also decrease because the the two velocities are coupled due to friction with the table. The net effect being: both linear and angular velocities reduce over time.

If the balls continue to rotate when they are aren't moving linearly then something is off: friction isn't working.

It is ok to add some angular damping as necessary to get the balls to slow down during rolling, however I would recommend tuning that parameter last. Tune the restitution first, then the friction, and finally damping. The reason being: damping is the most "non-physically accurate" feature of the simulation. If you want the physics to be as "real" as possible it is best to minimize damping.
Yeah they continue to rotate after the balls have stopped moving linearly. (while angular damping = 0). I then added a little angular damping, bearing in mind what you said about keeping the value small. It was 0.75 originally, whereas now it is at 0.2. It has the effect of stopping the rotation once the ball has stopped moving linearly, in fact it does it quite quickly so I might even be able to lower it slightly more. Is setting it to 0.2 enough of a reduction?

At the moment the various values are:
- Ball friction: 0.45 (was 0.9)
- Ball restitution 0.99
- Ball linear damping 0.45 (was 0.6)
- Ball angular damping 0.2 (was 0.75)

- Table friction: 0.80 (was 0.90)
- Table restitution: 0.60 (was 0.90)

Would you say those values are more reasonable? Like I say reducing the ball friction has helped the angle difference, so that visual cue coupled with your suggestions definitely seems like things are heading in the right direction

User avatar
drleviathan
Posts: 527
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Unexplained behaviour with rigidbody applyImpulse

Post by drleviathan » Thu May 16, 2019 8:33 pm

Yes, the values look better, and if it is working well for you then great! You have a good reference point now. You can make tuning deviations from there.

Low angular damping is better. My advice: keep dialing it down until it is bad, then dial it back to where it was last good.

Post Reply