switching rigidbody between static and dynamic

Physics APIs, Physics file formats, Maya, Max, XSI, Cinema 4D, Lightwave, Blender, thinkingParticles™ and other simulation tools, exporters and importers
dtruman
Posts: 2
Joined: Tue Oct 07, 2014 6:45 am

switching rigidbody between static and dynamic

Post by dtruman »

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:

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);

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:

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;
}
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.
dtruman
Posts: 2
Joined: Tue Oct 07, 2014 6:45 am

Re: switching rigidbody between static and dynamic

Post by dtruman »

Fixed:

After a long night I figured it out. First of all for an unknown reason at this time using a triangle mesh (the mesh of the object) will have the possibility of crashing the application. So instead I used a convex collision shape. In addition, you will need to call some flags in the switch to properly switch between static and dynamic. The code is as such:

Code: Select all

btConvexShape* tmpConvexShape=new btConvexTriangleMeshShape(mTriMesh);

btShapeHull* hull=new btShapeHull(tmpConvexShape);
btScalar margin=tmpConvexShape->getMargin();
hull->buildHull(margin);
tmpConvexShape->setUserPointer(hull);

btConvexHullShape* convexShape=new btConvexHullShape();
bool updateLocalAabb=false;

for(int i=0; i<hull->numVertices(); i++)
{
    convexShape->addPoint(hull->getVertexPointer()[i],updateLocalAabb);
}
convexShape->recalcLocalAabb();
convexShape->setMargin(0.001f);

delete tmpConvexShape;
delete hull;

meshInfos[currentMesh].shape=convexShape;
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);
and switching:

Code: Select all

    if(gravityOn && !addedToWorld)
{
    if(mass>0)
    {
        world->removeRigidBody(body);
        btVector3 inertia(0,0,0);
        body->getCollisionShape()->calculateLocalInertia(mass, inertia);
        body->setActivationState(DISABLE_DEACTIVATION);
        body->setMassProps(mass, inertia);
        body->setLinearFactor(btVector3(1,1,1));
        body->setAngularFactor(btVector3(1,1,1));
        body->updateInertiaTensor();
        body->clearForces();
        btTransform transform;
        transform.setIdentity();
        float x=position.x;
        float y=position.y;
        float z=position.z;
        transform.setOrigin(btVector3(x, y,z));
        body->getCollisionShape()->setLocalScaling(btVector3(1,1,1));
        body->setWorldTransform(transform);
        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);
    btVector3 inertia(0,0,0);
    body->getCollisionShape()->calculateLocalInertia(0, inertia);
    body->setCollisionFlags(btCollisionObject::CF_STATIC_OBJECT);
    body->setMassProps(0, inertia);
    body->setLinearFactor(btVector3(0,0,0));
    body->setAngularFactor(btVector3(0,0,0));
    body->setGravity(btVector3(0,0,0));
    body->updateInertiaTensor();
    body->setAngularVelocity(btVector3(0,0,0));
    body->setLinearVelocity(btVector3(0,0,0));
    body->clearForces();
    body->setActivationState(WANTS_DEACTIVATION);
    btTransform transform;
    transform.setIdentity();
    float x=position.x;
    float y=position.y;
    float z=position.z;
    transform.setOrigin(btVector3(x, y,z));
    body->getCollisionShape()->setLocalScaling(btVector3(1,1,1));
    body->setWorldTransform(transform);
    world->addRigidBody(body);
    addedToWorld=false;
}