ConvexHullShape does not fall over

timmy79
Posts: 3
Joined: Fri Aug 21, 2009 2:28 am

ConvexHullShape does not fall over

Post by timmy79 »

Hi Guys,

I am having a little trouble getting some bowling pins to fall over. Here is a video showing what happens.
http://www.youtube.com/watch?v=GM44YyIvNdI

As you can see the pins fly into the air ok when they are hit by the ball but they just all bounce back upright.

The pins themselves are created like so

Code: Select all

	btTriangleMesh *trimesh = new btTriangleMesh(false,false);
	//get mesh data
	for(unsigned int i=0;i < mSubMeshes.size();++i)
	{
		btVector3    vertexPos[3];
		const unsigned idxcnt = mSubMeshes[i]->num_triangles*3;
		const unsigned short* idx = &mSubMeshes[i]->Indices[0];
		for ( unsigned p=0; p<idxcnt; p+=3)
		{
			Vec3 p0 = mSubMeshes[i]->Vertices[idx[p+0]];
			Vec3 p1 = mSubMeshes[i]->Vertices[idx[p+1]];
			Vec3 p2 = mSubMeshes[i]->Vertices[idx[p+2]];

			vertexPos[0] = btVector3(p0.x,p0.y,p0.z);
			vertexPos[1] = btVector3(p1.x,p1.y,p1.z);
			vertexPos[2] = btVector3(p2.x,p2.y,p2.z);

			trimesh->addTriangle(vertexPos[0], vertexPos[1], vertexPos[2]);
		}

      }
	
	btConvexShape* tmpConvexShape = new btConvexTriangleMeshShape(trimesh);
	
	//create a hull approximation
	btShapeHull* hull = new btShapeHull(tmpConvexShape);
	btScalar margin = tmpConvexShape->getMargin();
	hull->buildHull(margin);
	tmpConvexShape->setUserPointer(hull);
		
	btConvexHullShape* convexShape = new btConvexHullShape();
	for (unsigned b=0;b<hull->numVertices();b++)
	{
		convexShape->addPoint(hull->getVertexPointer()[b]);	
	}
	delete tmpConvexShape;
	delete hull;
		
	mCollisionShape = convexShape;
	btTransform startTransform;
	startTransform.setIdentity();
	startTransform.setOrigin(btVector3(pos.x,pos.y,pos.z));
	btScalar	mass(2.f);

	mMotionState = new btDefaultMotionState(startTransform);
	mBody = mPhysicsMgr->createBody(mCollisionShape,mass,0,mMotionState);
	mBody->setFriction(0.21f);
	mBody->setDamping(0.1f,0.8f);
i step the simulation doing this

Code: Select all

	mDynamicsWorld->stepSimulation(dt,10,1.0/200);
I have tried different settings for the friction and damping but i just can't get it right.

Any clues as to what i am doing wrong?

Thanks,

Tim
Last edited by timmy79 on Fri Aug 21, 2009 3:06 am, edited 1 time in total.
timmy79
Posts: 3
Joined: Fri Aug 21, 2009 2:28 am

Re: rigidbody does not fall over

Post by timmy79 »

oops i forgot to mention if i replace the ConvexHullShape with just a btBoxShape or btCylinderShape instead the pins behave correctly. I am maybe doing something wrong when building the convexhull. I tried running the simulation using just the btConvexTriangleMeshShape (dropping the decompostion code out)and i get the exact same behavior as in the video. When i enable debugdraw it all lools ok.
timmy79
Posts: 3
Joined: Fri Aug 21, 2009 2:28 am

Re: ConvexHullShape does not fall over

Post by timmy79 »

It's ok now i have replaced the convex code and just used a btCylinderShape instead to approximate the pins. Not 100% accurate but it will never be noticed with what i am doing anyway. So close enough is good enough :D
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: ConvexHullShape does not fall over

Post by Erwin Coumans »

Generally it is best to approximate the shape, so your solution using btCylinderShape is a good idea.

Otherwise, you need to shift the center of mass so that the vertices of the pin are defined around the center of mass. You can use a btCompoundShape::calculatePrincipalAxisTransform for this. We will provide some sample how to do this in a future Bullet version.
Thanks,
Erwin