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);
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);
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]);
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));
Code: Select all
btDrawingResult renderCallback;
if(movedObject > -1)
collisionWorld->contactTest(&objects[movedObject],renderCallback);
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);