Collision b/w simple cube btTriangleMeshes not working

bobbel
Posts: 2
Joined: Wed Jan 23, 2013 2:24 pm

Collision b/w simple cube btTriangleMeshes not working

Post by bobbel »

Hello everyone!

I try to get collision detection b/w triangle meshes to work. I only need collision detection itself, no physics. In a nutshell my problem is the following: I have two boxes I move using keys. If I take simple btBoxShapes for both of them, the collisions get reported as expected. If I use one triangle mesh and one box shape, the collision still works. But if I use two triangle meshes, no collisions are reported anymore. I use the world's debugDrawObject to visualize the shapes, they appear correctly.

Does someone know a solution to this problem? Below I wrote some details on the implementation.

Thanks in advance and best regards,
bobbel.

I don't know at all where to look for errors, so I'll be a little bit more elaborate, I hope that's fine. I'm using the CollisionInterfaceDemo source code as a basis.

In CollisionInterfaceDemo::initPhysics() the triangle data for the meshes is initialized:

Code: Select all

btVector3 cube[] = {
	btVector3(1.000,1.000,1.000), btVector3(-1.000,1.000,1.000), btVector3(1.000,-1.000,1.000),
	btVector3(-1.000,1.000,1.000), btVector3(-1.000,-1.000,1.000), btVector3(1.000,-1.000,1.000) ,
	btVector3(-1.000,1.000,-1.000), btVector3(1.000,1.000,-1.000), btVector3(-1.000,-1.000,-1.000),
	btVector3(1.000,1.000,-1.000), btVector3(1.000,-1.000,-1.000), btVector3(-1.000,-1.000,-1.000),
	btVector3(1.000,-1.000,1.000), btVector3(1.000,1.000,1.000), btVector3(1.000,-1.000,-1.000), 
	btVector3(1.000,1.000,1.000), btVector3(1.000,1.000,-1.000), btVector3(1.000,-1.000,-1.000), 
	btVector3(-1.000,1.000,1.000), btVector3(-1.000,-1.000,1.000), btVector3(-1.000,1.000,-1.000), 
	btVector3(-1.000,-1.000,1.000), btVector3(-1.000,-1.000,-1.000), btVector3(-1.000,1.000,-1.000), 
	btVector3(1.000,1.000,-1.000), btVector3(-1.000,1.000,-1.000), btVector3(1.000,1.000,1.000), 
	btVector3(-1.000,1.000,-1.000), btVector3(-1.000,1.000,1.000), btVector3(1.000,1.000,1.000), 
	btVector3(-1.000,-1.000,-1.000), btVector3(1.000,-1.000,-1.000), btVector3(-1.000,-1.000,1.000), 
	btVector3(1.000,-1.000,-1.000), btVector3(1.000,-1.000,1.000), btVector3(-1.000,-1.000,1.000) 
};
int cubeSize = sizeof(cube)/sizeof(btVector3);
I use that to build two box meshes like this:

Code: Select all

btTriangleMesh* boxAMesh = new btTriangleMesh();
for(int i = 0; i < cubeSize; i += 3)
	boxAMesh->addTriangle(cube[i],cube[i+1],cube[i+2],true);

btBvhTriangleMeshShape* boxA = new btBvhTriangleMeshShape(boxAMesh,true,true);
boxA->setMargin(0.f);
	
objects[0].setCollisionShape(boxA);
Then I build the world like this:

Code: Select all

btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
	
btDbvtBroadphase*	broadphase = new btDbvtBroadphase();
	
collisionWorld = new btCollisionWorld(dispatcher,broadphase,collisionConfiguration);
		
collisionWorld->addCollisionObject(&objects[0]);
collisionWorld->addCollisionObject(&objects[1]);
Later I reset the position of the objects in CollisionInterfaceDemo::clientResetScene():

Code: Select all

objects[0].getWorldTransform().setOrigin(btVector3(-3.f,0.f,0.f));
objects[1].getWorldTransform().setOrigin(btVector3(3.f, 0.f, 0.f));
If I moved an object, I will perform collision detection in CollisionInterfaceDemo::displayCallback as follows:

Code: Select all

btDrawingResult renderCallback;
if(movedObject > -1)
	collisionWorld->contactTest(&objects[movedObject],renderCallback); 
where movedObject holds the index of the last moved object. The render callback itself won't do much besides giving some debug output on the shell (and in my case give the collided objects a distinct color).

I perform the moving of objects along the x-axis in a straight-forward way. In CollisionInterfaceDemo::keyboardCallback I calculate a position on the x-axis and then set the position of
the moved object as follows:

Code: Select all

objects[movedObject].getWorldTransform().getOrigin().setX(newObjX);
bobbel
Posts: 2
Joined: Wed Jan 23, 2013 2:24 pm

Re: Collision b/w simple cube btTriangleMeshes not working

Post by bobbel »

After a lot of more reading I found the answer myself :)

The Bullet User Manual on p. 18 gave the perfect hint (the picture about choosing the right collision shape).

My mistake was that btBvhTriangleMeshShape is for static geometry only. Other shapes have to be used if the object is to be moved. I tried btConvexHullShape and it reports the correct collisions between the boxes.

Regards,
bobbel