Changing static to dynamic rigid body

lvella
Posts: 16
Joined: Thu Oct 30, 2008 4:28 pm

Changing static to dynamic rigid body

Post by lvella »

I need some rigid bodies in my simulation to remain static until an event occurs. Disabling the simulation for those bodies with setActiovationState() won't do, because they would be reactivated when interacting with other bodies. So I thought of setting their mass to 0 until the event occurs, then I set their mass to some positive value.

This is not the actual code, but shows how I am doing:

Code: Select all

/* general code */
btVector3 inertia(0.0, 0.0, 0.0);
pShape->calculateLocalInertia(mass, inertia);
btRigidBody::btRigidBodyConstructionInfo info(mass, this, pShape, inertia);
info.m_friction = friction;
m_pRigidBody = new btRigidBody(info);

/* specific code */
m_pRigidBody->setMassProps(0.0f, btVector(0.0f, 0.0f, 0.0f));

/* .... */
/* on event occurrence */
m_pRigidBody->setMassProps(mass, inertia);
This way, the body starts up stationary, but when I call setMassProps() upon the event occurrence, the body starts floating up defying gravity, like a slow floating asteroid in space, while I expect it to fall in the direction of gravity.
Do you know what is wrong? What am I missing in the usage of setMassProps() ?
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Changing static to dynamic rigid body

Post by Erwin Coumans »

What am I missing in the usage of setMassProps
Remove the rigid body from the dynamics world, then make the changes (setMassProps), and finally re-add the body to the dynamics world.

Hope this helps,
Erwin
ShortyTwoMeters
Posts: 5
Joined: Sat Apr 04, 2009 7:25 am

Re: Changing static to dynamic rigid body

Post by ShortyTwoMeters »

I think a simpler way would just be to toggle CF_STATIC_OBJECT in the body's collision flags. Modifying your example:

Code: Select all

/* general code */
btVector3 inertia(0.0, 0.0, 0.0);
pShape->calculateLocalInertia(mass, inertia);
btRigidBody::btRigidBodyConstructionInfo info(mass, this, pShape, inertia);
info.m_friction = friction;
m_pRigidBody = new btRigidBody(info);

/* Make the body static without changing the mass props*/
m_pRigidBody->setCollisionFlags(m_pRigidBody->getCollisionFlags() | btCollisionObject::CF_STATIC_OBJECT);

/* .... */
/* on event occurrence - clear the static object flag */
m_pRigidBody->setCollisionFlags(m_pRigidBody->getCollisionFlags() & ~btCollisionObject::CF_STATIC_OBJECT);
lvella
Posts: 16
Joined: Thu Oct 30, 2008 4:28 pm

Re: Changing static to dynamic rigid body

Post by lvella »

I do tried to toggle the CF_STATIC_OBJECT in the body's collision flags. Actually, I don't remember why this was not my chosen solution, but there is some reason for it to not work.
Maybe the flag didn't prevent the body from being moved by the simulation, but I really don't remember. I'll try again.

Thanks for the answers.