Unstable behavior on shapes like cylinder or box

Dragonlord
Posts: 198
Joined: Mon Sep 04, 2006 5:31 pm
Location: Switzerland
Contact:

Unstable behavior on shapes like cylinder or box

I'm trying to tackler issues in the bullet integration. I've got two main issues I can't find a solution for. Maybe somebody has seen them before and knows in what direction to look for the problem?

The first is a strange behavior of cylinder and box shapes. For example I've got a can (beer can, pick your poison). This is a cylinder shape. If I pick up the shape and drop it on a desk (box shape) the cylinder shape drops to the side resting on the top (or bottom) circle cap edge and spins around on the desk. Same problem can be achieved with box shape. In general shapes are very unstable and a stack of boxes or cylinders (thing of a pyramid of cans like on a playground ball throwing game) drop apart due to the instability.

Are cylinder and box shapes unstable by design? Are hull shapes (havok style) more stable in bullet? What is anyway the recommended shape for best stability and performance? box, sphere, cylinder, capsule or hull?

The second problem is the total lack of friction. Objects keep on sliding, rotation and doing other strange stuff and I can not figure out for the live of me how I can make bullet do friction. Any help would be appreciated
Dragonlord
Posts: 198
Joined: Mon Sep 04, 2006 5:31 pm
Location: Switzerland
Contact:

Re: Unstable behavior on shapes like cylinder or box

I've added a small video showing the problem.
screencap.gif (2.32 MiB) Viewed 10252 times
drleviathan
Posts: 843
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Unstable behavior on shapes like cylinder or box

The box and cylinder shapes are not unstable. I've seen them work fine.

I don't know what is wrong with your beer can. It behaves as if... the moment it hits the ground its world-frame inverse inertia tensor gets zero elements in the two axes perpendicular to the UP direction. That is: it is constrained to only rotate about world-frame UP axis. I can't imagine what would do that except for rogue custom code that is somehow (a) messing with the object's inertia tensor, (b) applying a hinge constraint or (c) some Action that is incorrectly slamming the object's angular velocity every frame.

Friction can be set per-object via btCollisionObject::setFriction(), but I assume you've been trying that. I've seen situations where some bodies never settle and instead just rattle around. As I recall they was caused by: (1) incorrect inertia tensor for tiny objects, (2) high restitution, zero damping, and too-small sleeping thresholds, and (3) a bug where custom Action was incorrectly re-activating the object every frame.
Dragonlord
Posts: 198
Joined: Mon Sep 04, 2006 5:31 pm
Location: Switzerland
Contact:

Re: Unstable behavior on shapes like cylinder or box

drleviathan wrote: Tue Feb 22, 2022 1:42 am The box and cylinder shapes are not unstable. I've seen them work fine.

I don't know what is wrong with your beer can. It behaves as if... the moment it hits the ground its world-frame inverse inertia tensor gets zero elements in the two axes perpendicular to the UP direction. That is: it is constrained to only rotate about world-frame UP axis. I can't imagine what would do that except for rogue custom code that is somehow (a) messing with the object's inertia tensor, (b) applying a hinge constraint or (c) some Action that is incorrectly slamming the object's angular velocity every frame.

Friction can be set per-object via btCollisionObject::setFriction(), but I assume you've been trying that. I've seen situations where some bodies never settle and instead just rattle around. As I recall they was caused by: (1) incorrect inertia tensor for tiny objects, (2) high restitution, zero damping, and too-small sleeping thresholds, and (3) a bug where custom Action was incorrectly re-activating the object every frame.
I added a printf to check the inertia tensor. I have the impression it is okay. At last I see no zero's:
updateInertiaTensor: 0x555abd8c9a80 [83.7424,9.44874,-22.1889][9.44874,92.087,-34.0537][-22.1889,-34.0537,157.556]

This method is called every call to stepSimulation(). Is this correct?

I also put a printf on setAngularVelocity in btRigidBody. There is no call to this method so none of my code messes with the angular velocity without my knowledge.

Any other ideas what could be wrong? How can I approach debugging this issue?

EDIT: I tried getting values from a single run of setupContactConstraint of the situation above where the can is nearly flat on the ground spinning. Maybe you see some value which is glaringly off or broken? https://pastebin.com/7VaYd9U0

EDIT: EDIT: I've also logged all updateInertiaTensor() calls of the object. At no time zero values are present in the tensor.
drleviathan
Posts: 843
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Unstable behavior on shapes like cylinder or box

The beer can... when it lands on the ground does it get deactivated? or does it remain active?

Are you using a custom MotionState in your game? If so, what does its getWorldTransform() and setWorldTransform() implementations look like?
Dragonlord
Posts: 198
Joined: Mon Sep 04, 2006 5:31 pm
Location: Switzerland
Contact:

Re: Unstable behavior on shapes like cylinder or box

drleviathan wrote: Tue Feb 22, 2022 4:55 pm The beer can... when it lands on the ground does it get deactivated? or does it remain active?

Are you using a custom MotionState in your game? If so, what does its getWorldTransform() and setWorldTransform() implementations look like?
Yes, a motion state is used. The code is here: https://github.com/LordOfDragons/dragen ... nState.cpp
drleviathan
Posts: 843
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Unstable behavior on shapes like cylinder or box

Ok that debpMotionState looks fine: I don't see any misbehaving logic there.
Dragonlord
Posts: 198
Joined: Mon Sep 04, 2006 5:31 pm
Location: Switzerland
Contact:

Re: Unstable behavior on shapes like cylinder or box

drleviathan wrote: Tue Feb 22, 2022 5:50 pm Ok that debpMotionState looks fine: I don't see any misbehaving logic there.
Any ideas where I could put a lever to debug this problem? I tried examining the contact constraint generated but I did not get the impression something is wrong. That said I'm not much into physics coding so I don't know what kind of values would look wrong.
Dragonlord
Posts: 198
Joined: Mon Sep 04, 2006 5:31 pm
Location: Switzerland
Contact:

Re: Unstable behavior on shapes like cylinder or box

I've done some more experimenting and disabled this and that rigid body option until I got a change. It looks like the rolling friction cause the problem. I used the value from the RollingFrictionDemo which is 0.1f . Using this value the cylinder does not roll but can even stand on its edge at 80 degrees. If I set the rolling friction to 0 (which is the default) then the edge standing problem is gone but the cylinder starts rolling away accelerating while doing so.

How can I thus prevent cylinders from rolling (and accelerating) on the floor without standing ridiculously on their edge? The demo values do not seem to work.
drleviathan
Posts: 843
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Unstable behavior on shapes like cylinder or box

Yes interesting. I downloaded your source code this morning and was looking around. I checked the commit logs and saw that you may have made some progress on the problem. I then diffed with history to browse the relevant changes.

To tell the truth, I don't have any experience with rolling/spinning friction. It looks like they were added/enabled back in late 2019 and all of my personal Bullet experience is prior to that year (maybe I should stop trying to answer questions on this forum). It is possible you've encountered a new/real bug in the Bullet simulation.
Dragonlord
Posts: 198
Joined: Mon Sep 04, 2006 5:31 pm
Location: Switzerland
Contact:

Re: Unstable behavior on shapes like cylinder or box

It seems to use rolling friction you need to also enable anisotropic rolling friction. Doing so makes the rolling friction work without objects standing on the edges that much. There is still some angles at which they stand still but I'll go through the bullet examples and collect the parameters used. Maybe I see something that changed. I'm also used to the old code and just updated from 2.87 to 2.89 . I guess I have to adjust some more values to get it working again. As good as in the examples would be enough for me. Right now a tower of cans for ball throwing won't stack beyond the bottom layer so there is something still off.
sharase23
Posts: 5
Joined: Thu Feb 09, 2023 9:13 am

Re: Unstable behavior on shapes like cylinder or box

I will suggest you several ways to solve shape instability: You can increase the collision margin: Set a larger collision margin for your shapes. The collision margin is an invisible padding around the shape that helps to stabilize contacts between objects. Adjust the shape's mass subway surfers and inertia: Make sure the mass and inertia properties of the shapes are set correctly. Inappropriate mass and inertia values can lead to unstable behavior.