Shifting center of mass

B_old
Posts: 79
Joined: Tue Sep 26, 2006 1:10 pm

Shifting center of mass

Post by B_old »

Hello,
from searching the forum I believe compound shapes are the preferred method for shifting the center of mass.
I have a tree shape whose origin is at the base of the trunk. I want to approximate the trunk with a cylinder. So I made a compound shape which only contains a cylinder and translated the cylinder on the y-axis. For a static body this gives me exactly what I want, but there it isn't really necessary because there is no mass anyway. For a dynamic object this behaves strange though. It seems like the center of mass is still at the base of the tree instead in the middle of the trunk/cylinder, because the tree doesn't really fall down, but sways in a funny kind of way.
I hope someone can make sense of my explanation and give me a hint about where my mistake could be. Thanks.
Verbo
Posts: 39
Joined: Fri May 08, 2009 9:27 pm

Re: Shifting center of mass

Post by Verbo »

Hi,

I recommend you create your motion state class derived from btDefaultMotionState (to store your center of mass offset in order to adjust the graphical transfrom to match you shift in the compound shape), and override getWorldTransform() this way:

void MotionState::getWorldTransform(btTransform& worldTrans ) const
{
btTransform bulletGraphicWorldTransform;
bulletGraphicWorldTransform.setIdentity();

// Extract the world transform from your graphical entity here, into "bulletGraphicWorldTransform".
...
...
...

// Bug fix from the btDefaultMotionState version. Don't ask me why, but the
// graphic world transform was multiplied by the center of mass offset transform instead of the
// opposite, which was applying the offset in the world instead of locally. Well, in my case it was a bug,
//but maybe it depends on the engine coord system in which you use bullet physics.

worldTrans = bulletGraphicWorldTransform * m_centerOfMassOffset.inverse();
}


I went through the same painful steps as you did, but in the end, this worked for me, thanks to the many threads from Erwin and the other members from the community :)

Enjoy!

Verbo
B_old
Posts: 79
Joined: Tue Sep 26, 2006 1:10 pm

Re: Shifting center of mass

Post by B_old »

Thanks for the answer Verbo!
I actually thought about the same thing before and than stumbled over a thread where Erwin seemed to encouraging the "compound-solution".
Any more opinions on this one? Otherwise I'll probably go ahead and make another MotionState as Verbo suggests.