Search code examples
collision-detectionphysicscollisionbulletphysics-engine

Bullet physics mass center and weird object reaction


I am new in bullet, and i have probably a basic problem. I try to simulate bowing pin falling, but they after falling down, getting up by themselves without any force added.

I wonder where is my mistake, can anyone of you help, i would be grateful.

here is a video showing what happens: https://www.sendspace.com/file/78tncr

and here is how I added floor :

tTransform l;
l.setIdentity();
l.setOrigin(btVector3(0,0,0));
btStaticPlaneShape* plane=new btStaticPlaneShape(btVector3(0,1,0),0);
btMotionState* motion=new btDefaultMotionState(l);
btRigidBody::btRigidBodyConstructionInfo info(0.0,motion,plane);
btRigidBody* body=new btRigidBody(info);
world->addRigidBody(body);
bodies.push_back(body);

and this is how I added bolwing pin :

btRigidbodyaddBolw (float x, float y , float z,float mass)
{
btTransform t;  
t.setIdentity();
t.setOrigin(btVector3(x,y,z));
btTriangleMesh * tmptri= new btTriangleMesh();
//this is simply reading from std::vector, where I have vertex of a shape
for(int i=0;i<=faces.size()-3;i=i+3)
{
    if(faces[i].wektor==-100)
    {
        i=i-2;
        continue;
    }
    btVector3 vertex1(vertexy[faces[i].wektor].GetX(), vertexy[faces[i].wektor].GetY(), vertexy[faces[i].wektor].GetZ());
    btVector3 vertex2(vertexy[faces[i+1].wektor].GetX(), vertexy[faces[i+1].wektor].GetY(), vertexy[faces[i+1].wektor].GetZ());
    btVector3 vertex3(vertexy[faces[i+2].wektor].GetX(), vertexy[faces[i+2].wektor].GetY(), vertexy[faces[i+2].wektor].GetZ());

    tmptri->addTriangle(vertex1, vertex2, vertex3);
}
btConvexShape *tmpshape = new btConvexTriangleMeshShape(tmptri);
btShapeHull *hull = new btShapeHull(tmpshape);
btScalar margin = tmpshape->getMargin();
hull->buildHull(margin);
btConvexHullShape* simplifiedConvexShape = new btConvexHullShape();
for (int i=0;i<hull->numVertices();i++)
{
    simplifiedConvexShape->addPoint(hull->getVertexPointer()[i]);   
}
delete tmpshape;
delete hull;
btMotionState * motion = new btDefaultMotionState(t);
btVector3 inertia(0,0,0);
if(mass!=0.0)
    simplifiedConvexShape->calculateLocalInertia(mass,inertia); 
btRigidBody::btRigidBodyConstructionInfo info(mass,motion,simplifiedConvexShape,inertia);
btRigidBody* body=new btRigidBody(info);
world->addRigidBody(body);  //and let the world know about it
bodies.push_back(body); //to be easier to clean, I store them a vector
return body;
}

I tried change shape of pin, mass, friction and restitution but nothing hepl, is there any way to change center of mass, maybe it would help?


Solution

  • This will be caused by your model having it's pivot point / transform origin at or near the base of the bowling pin. In Bullet the rigid body transform is the center of mass, so what you are seeing is the heavy center of mass correcting itself with gravity and thus pulling the pin upright.

    You have a couple of options:

    • Change your model so that the transform origin is somewhere near the likely center of mass, which would be around the middle of the fattest part of the lower body. This would be the easiest method, however you may need to change your positioning code to account for the offset.
    • Create a btCompoundShape to wrap your btConvexHullShape. This allows you to transform the shapes within the compound shape. There is some discussion on this here: http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?p=6244&f=9&t=1506