btBvhTriangleMeshShape and calculateLocalInertia

Baha
Posts: 4
Joined: Tue Jun 21, 2011 2:17 pm

btBvhTriangleMeshShape and calculateLocalInertia

Post by Baha »

Hello again^^

I might have just missunderstood something when it comes to btBvhTriangleMeshShape but I tried to create a rigid body
with a btBvhTriangleMeshShape as collision shape. But when I try to calculate the inertia with calculateLocalInertia I get a runtime error.

This is the constructor of the Object class I created. When I call this the error occurs in calculateLocalInertia.

Code: Select all

myClass::myClass(VECTOR_3F* vertices, VECTOR_3F* faces, int numOfVertices, int numOfFaces, btScalar mass, VECTOR_3F position)
{
    this->m_forces = true;

    this->m_vetrices    = vertices;
    this->m_faces        = faces;

    this->m_inertia = btVector3(0, 0, 0);

    this->m_mesh = new btTriangleMesh;
    btVector3 v1(0, 0, 0);
    btVector3 v2(0, 0, 0);
    btVector3 v3(0, 0, 0);

    for(int i = 0; i < numOfFaces; i++)
    { 
        v1 = btVector3(vertices[(int)faces[i].x-1].x, vertices[(int)faces[i].x-1].y, vertices[(int)faces[i].x-1].z);
        v2 = btVector3(vertices[(int)faces[i].y-1].x, vertices[(int)faces[i].y-1].y, vertices[(int)faces[i].y-1].z);
        v3 = btVector3(vertices[(int)faces[i].z-1].x, vertices[(int)faces[i].z-1].y, vertices[(int)faces[i].z-1].z);

        this->m_mesh->addTriangle(v1, v2, v3);
    }

    btCollisionShape* shape = new btBvhTriangleMeshShape(this->m_mesh, true, true);
    this->setCollisionshape(shape);

    btVector3 pos;
    pos.setX(position.x);
    pos.setY(position.y);
    pos.setZ(position.z);
    
    btDefaultMotionState* motionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),pos));
    this->setMotionState(motionState);

    this->calculateInertia();

    btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(mass,motionState,shape,this->getInertia());
    btRigidBody* rigidBody = new btRigidBody(rigidBodyCI);
    rigidBody->setActivationState(DISABLE_DEACTIVATION);
    this->setRigidBody(rigidBody);
}

void myClass::calculateInertia()
{
	btVector3 inertia(0,0,0);

	if(this->getCollisionShape() == NULL)
		return;

	this->getCollisionShape()->calculateLocalInertia(this->getMass(),inertia);

	this->m_inertia = inertia;
}
When I simply dont calculate the inertia and pass a (0, 0, 0) Vector as parameter for rigidBodyCI it somehow works. Like this it wont give a runtime error. But it wont interact with the world correctly (e.g. it will "translate" but not "rotate" when sliding down a ramp). So I guess I acutally do need the inertia but did another mistake.

Do the triangles have to be added in a spezific order? Or did I do another mistake?

I hope some has any idea why I get this error.

Thank you for your help!
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: btBvhTriangleMeshShape and calculateLocalInertia

Post by Flix »

As far as I know btBvhTriangleMeshShape can be used for static/kinematic bodies only; so it makes no sense setting a mass(!=0) or an inertia tensor to bodies associated with this shape.

btGImapctMeshShape can be used for moving concave bodies instead.

So I suggest you simply do not call the "calculateInertia" method.
Baha
Posts: 4
Joined: Tue Jun 21, 2011 2:17 pm

Re: btBvhTriangleMeshShape and calculateLocalInertia

Post by Baha »

Ah ok didnt know that btBvhTriangleMeshShape is only for static bodys.Thank you!
But if I use btGImapctMeshShape instead the localinertia thing works but the interaction with the world does not. I cant rotate it.... Should I be able to rotate mesh shapes? I bet Ive done something wrong.... Thats how I try to rotate

Code: Select all

void myClass::setRotation(float x, float y, float z, float angel)
{
    btTransform trans;
    btQuaternion rot;

    this->getMotionState()->getWorldTransform(trans);

    rot.setRotation(btVector3(x, y, z), angel*PI/180);
    trans.setRotation(rot);
    this->getRigidBody()->setWorldTransform(trans);
// Should I call the setWorldTransform on the MotionState as well?
//    this->getMotionState()->setWorldTransform(trans);
}
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: btBvhTriangleMeshShape and calculateLocalInertia

Post by Flix »

Is this something related to the btGImpactMeshShape only or to any other dynamic rigid body you're using ?

P.S.
Dynamic bodies should be moved by applying linear/angular forces/impulses (or at least velocities), and not by setting their position/orientation each step (this can be done with kinematic bodies).

You can "warp" you body to a new position and orientation if you want, but this shouldn't be done on every timestep (and you should at least clean the proxy from pairs before you do it, or better remove the body from the world, re-position it and re-add it later). I think you can search the forum for further info about it...