CoG of btConvexHullShapes

Vylsain
Posts: 3
Joined: Sun Oct 30, 2011 6:14 pm

CoG of btConvexHullShapes

Post by Vylsain »

Hello everyone.

I'm doing an application and I use btConvexHullShapes but I encounter a problem.
The collision is well calculated but not the moves.

Here's a video of my problem with a simple cube :

http://www.youtube.com/watch?v=zMasdqb6z34

As you can see, it looks like the Center of gravity is outside of the shape.
All in all, it has a weird behaviour.

As you'll see in my code, I didn't do anything else than creating the object, and giving it a mass and an inertia.
I didn't modify the CoG, thinking it was automatically initialized in the middle of the cube (is it wrong ? Do I have do initiate it ?)


Here's the constructor of the class that buil the btConvexHullShapes :

Code: Select all

ConvexHullShape::ConvexHullShape(btVector3 * tab, btTransform &TransformCHS)
{
	CHS = new btConvexHullShape();
	
	for(int i=0; i<8; i++)
	{
             CHS->addPoint(tab[i]);
	}

        TransformCHS.setIdentity();
	TransformCHS.setOrigin(btVector3(0.0,25.0,0.0));
	TransformCHS.setRotation(btQuaternion(1,0,0,2));

	localInertia=btVector3(0,0,0);

	mass = 0.5f;

	CHS->calculateLocalInertia( mass, localInertia );

	MotionStateCHS = new btDefaultMotionState(TransformCHS);
	btRigidBody::btRigidBodyConstructionInfo myBoxRigidBodyConstructionInfo(0.5, MotionStateCHS, CHS, localInertia );
	bodyCHS = new btRigidBody(myBoxRigidBodyConstructionInfo);
}
In the main :

Code: Select all


        myWorld->setGravity( btVector3(0,-9.8,0) );
        
        //vertices of the cube
        tab[0] = btVector3 (0.0,0.0,0.0);
	tab[1] = btVector3 (10.0,0.0,0.0);
	tab[2] = btVector3 (10.0,0.0,10.0);
	tab[3] = btVector3 (0.0,0.0,10.0);
	tab[4] = btVector3 (0.0,10.0,0.0); 
	tab[5] = btVector3 (10.0,10.0,0.0);
	tab[6] = btVector3 (10.0,10.0,10.0);
	tab[7] = btVector3 (0.0,10.0,10.0);
	
	CHS_mon_Model=ConvexHullShape(tab,myTransform);

	myWorld->addRigidBody(CHS_mon_Model.bodyCHS);

	

	/// FLOOR ===================================================
	
        btCollisionShape* shape_sol = new btBoxShape( btVector3(100,0,100) );

	myTransform.setIdentity();
	myTransform.setOrigin( btVector3(0,0,0) );

	btVector3 localInertiaSol(0,0,0);

	btScalar mass = 0.0f;

	myMotionState_Sol = new btDefaultMotionState(myTransform);
	btRigidBody::btRigidBodyConstructionInfo rbInfo_sol( mass, myMotionState_Sol, shape_sol, localInertiaSol );
	body_sol = new btRigidBody(rbInfo_sol);

	myWorld->addRigidBody(body_sol);
        //=====================================================================

	InitCallbacks();

	glutDisplayFunc(Display);

	glutMainLoop();	
}
And in my Display function :

Code: Select all

if (myWorld && aff==true) //si myWorld est instancié et si 'a' a été appuyée
	{
		myWorld->stepSimulation(1.f/60, 10);
		myWorld->debugDrawWorld();
	}

	CHS_mon_Model.MotionStateCHS->m_graphicsWorldTrans.getOpenGLMatrix(CHS_mon_Model.MatrixCHS);
	
	glPushMatrix();
		glMultMatrixf(CHS_mon_Model.MatrixCHS);
		if(mod==true){
		        DrawBox();
		}
	glPopMatrix();

	DrawFloor();
I tried a lot of things but still don't know where the problem is coming from...

Is it really a problem of CoG and how do I modify it if that's the case ?

Thank you
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: CoG of btConvexHullShapes

Post by Flix »

Vylsain wrote:As you can see, it looks like the Center of gravity is outside of the shape.
All in all, it has a weird behaviour.
Not weird at all to me. In fact this is what you wanted to do...
Vylsain wrote: //vertices of the cube
tab[0] = btVector3 (0.0,0.0,0.0);
tab[1] = btVector3 (10.0,0.0,0.0);
tab[2] = btVector3 (10.0,0.0,10.0);
tab[3] = btVector3 (0.0,0.0,10.0);
tab[4] = btVector3 (0.0,10.0,0.0);
tab[5] = btVector3 (10.0,10.0,0.0);
tab[6] = btVector3 (10.0,10.0,10.0);
tab[7] = btVector3 (0.0,10.0,10.0);
As you can see, the center of the btConvexHullShape (0,0,0) is a vertex of the cube. Just place (0,0,0) at the center of the cube instead.

PS. The same thing happens in btCompoundShapes (I mean (0,0,0) is considered the center of mass).
Vylsain
Posts: 3
Joined: Sun Oct 30, 2011 6:14 pm

Re: CoG of btConvexHullShapes

Post by Vylsain »

Oh ok, I thought the CG was calculated as the barycentre of all the points...

Thank you very much Flix ! :D
Vylsain
Posts: 3
Joined: Sun Oct 30, 2011 6:14 pm

Re: CoG of btConvexHullShapes

Post by Vylsain »

Yeah it works !!!

Thank you again for this good library and it's good community ! 8)
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: CoG of btConvexHullShapes

Post by Erwin Coumans »

If you have a btConvexHullShape (or a couple of other collision shapes) and you want to adjust the center of mass/inertia, you can also use the btCompoundShape for that.

Store the shapes in a btCompoundShape and use the calculatePrincipalAxisTransform method. It is not well documented, but the Bullet/Demos/FractureDemo uses it (see btFractureBody::shiftTransform).

Thanks,
Erwin