switching rigidbody between static and dynamic
Posted: Tue Oct 07, 2014 6:48 am
I am fairly new to Bullet and my goal here is to be able to switch a btRigidBody between static and dynamic. To initialize my rigidbody I start out by doing this:
that sets it up as static_object because the "mass" variable I have is 0 to start. Later on I have a function that checks if a boolean was switched on and it switches the rigidbody to a dynamic object like this:
the addedToWorld boolean just makes sure that the if statement doesn't keep running through the code block every update. From what I have researched this should work but instead does nothing. Am I missing something? From what I've seen the best practice is to remove the rigidbody first before you do any changes to it. And then setMassProps to change inertia, setLinearFactor and setAngularFactor allows the object to not move or move depending on the vector you pass into it when collided into, updateInertiaTensor allows the inertia to update properly, and then I add the rigidbody back. I might have misunderstood some of this, any help would be greatly appreciated.
Code: Select all
btGImpactMeshShape* triMesh=new btGImpactMeshShape(mTriMesh);
triMesh->setLocalScaling(btVector3(1,1,1));
triMesh->updateBound();
meshInfos[currentMesh].shape=triMesh;
meshInfos[currentMesh].motionState=new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), btVector3(position.x,position.y,position.z)));
meshInfos[currentMesh].mass=mass;
btVector3 inertia;
meshInfos[currentMesh].shape->calculateLocalInertia(mass, inertia);
btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(0, meshInfos[currentMesh].motionState, meshInfos[currentMesh].shape, inertia);
meshInfos[currentMesh].rigidBody=new btRigidBody(rigidBodyCI);
Code: Select all
if(gravityOn && !addedToWorld)
{
if(mass>0)
{
world->removeRigidBody(body);
btVector3 inertia;
body->getCollisionShape()->calculateLocalInertia(mass, inertia);
body->setMassProps(mass, inertia);
body->setLinearFactor(btVector3(1,1,1));
body->setAngularFactor(btVector3(1,1,1));
body->updateInertiaTensor();
world->addRigidBody(body);
addedToWorld=true;
}
else
{
std::cout << "Mass must be set to a number greater than 0" << std::endl;
}
}
else if(!gravityOn && addedToWorld)
{
world->removeRigidBody(body);
body->setMassProps(0, btVector3(0,0,0));
body->setLinearFactor(btVector3(0,0,0));
body->setAngularFactor(btVector3(0,0,0));
body->updateInertiaTensor();
world->addRigidBody(body);
addedToWorld=false;
}