Page 1 of 1

Basic 3D Mesh Collision question

Posted: Mon Jan 05, 2015 1:55 pm
by moeizle

I am new to bullet, and i wish to test for collision between a number of static models in my environment.

I load multiple models and i use the code below to detect collision but for some reason when i test for the 'number of manifolds' its always zero even though the models are colliding.

Am i missing something?

Your help is very much appreciated.

Please find the code below

Code: Select all

void bt_collides_arbitrary_mesh(bool& collision,float& pen_depth,vector<Model> m)
	btCollisionConfiguration* bt_collision_configuration;
	btCollisionDispatcher* bt_dispatcher;
	btAxisSweep3* bt_broadphase;
	btCollisionWorld* bt_collision_world;

	double scene_size = 500;
	unsigned int max_objects = 10000;

	bt_collision_configuration = new btDefaultCollisionConfiguration();
	bt_dispatcher = new btCollisionDispatcher(bt_collision_configuration);

	btScalar sscene_size = (btScalar) scene_size;
	btVector3 worldAabbMin(-sscene_size, -sscene_size, -sscene_size);
	btVector3 worldAabbMax(sscene_size, sscene_size, sscene_size);

	bt_broadphase = new btAxisSweep3(worldAabbMin, worldAabbMax, max_objects,0,true);

	bt_collision_world = new btCollisionWorld(bt_dispatcher, bt_broadphase, bt_collision_configuration);

	//Create multiple  collision objects
	vector<btCollisionObject*> M;
	vector<btTriangleMeshShape*> mM;
	vector<btTriangleMesh*> TIVA;

	btCollisionObject* tM;
	btTriangleMeshShape* tmM;
	btTriangleMesh* tTIVA;

	//Move each to a specific location
	btMatrix3x3 basis;
	int counter=0;
	for(int i=0;i<m.size();i++)

		//read faces
		TIVA[counter]=new btTriangleMesh();
		for(int k=0;k<m[i].f.size();k++)
			btVector3 v0(m[i].v[m[i].f[k].a-1].x,m[i].v[m[i].f[k].a-1].y,m[i].v[m[i].f[k].a-1].z);
			btVector3 v1(m[i].v[m[i].f[k].b-1].x,m[i].v[m[i].f[k].b-1].y,m[i].v[m[i].f[k].b-1].z);
			btVector3 v2(m[i].v[m[i].f[k].c-1].x,m[i].v[m[i].f[k].c-1].y,m[i].v[m[i].f[k].c-1].z);

		//set up mesh shape
		mM[counter]=new btBvhTriangleMeshShape(TIVA[counter],true);

		//set up collision shape

		M[counter]=new btCollisionObject();


	//Perform collision detection

	int numManifolds = bt_collision_world->getDispatcher()->getNumManifolds();

	//For each contact manifold
	for (int i = 0; i < numManifolds; i++) 
		btPersistentManifold* contactManifold = bt_collision_world->getDispatcher()->getManifoldByIndexInternal(i);
		btCollisionObject* obA = const_cast<btCollisionObject*>(contactManifold->getBody0());
		btCollisionObject* obB = const_cast<btCollisionObject*>(contactManifold->getBody1());
		contactManifold->refreshContactPoints(obA->getWorldTransform(), obB->getWorldTransform());
		int numContacts = contactManifold->getNumContacts();
		//For each contact point in that manifold
		for (int j = 0; j < numContacts; j++) 
		    //Get the contact information
			btManifoldPoint& pt = contactManifold->getContactPoint(j);
			btVector3 ptA = pt.getPositionWorldOnA();
			btVector3 ptB = pt.getPositionWorldOnB();
			double ptdist = pt.getDistance();
			btVector3 normal_nB=pt.m_normalWorldOnB;



	//clean up
	for(int i=0;i<counter;i++)
		delete M[i];
		delete mM[i];
	delete bt_collision_world;
	delete bt_collision_configuration;
	delete bt_dispatcher;
	delete bt_broadphase;

Re: Basic 3D Mesh Collision question

Posted: Mon Jan 05, 2015 2:05 pm
by c6burns
Can btBvhTriangleMeshShape even collide with another btBvhTriangleMeshShape? I feel the answer is no.

Re: Basic 3D Mesh Collision question

Posted: Mon Jan 05, 2015 2:13 pm
by moeizle
Thank you for your reply.

what other shape do you suggest i use? keeping in mind that the meshes are not necessarily convex.

Re: Basic 3D Mesh Collision question

Posted: Mon Jan 05, 2015 6:14 pm
by c6burns
From btBvhTriangleMeshShape.h:

Code: Select all

///The btBvhTriangleMeshShape is a static-triangle mesh shape, it can only be used for fixed/non-moving objects.
///If you required moving concave triangle meshes, it is recommended to perform convex decomposition
///using HACD, see Bullet/Demos/ConvexDecompositionDemo. 
///Alternatively, you can use btGimpactMeshShape for moving concave triangle meshes.
I've never actually used either of these. I assume HACD would result in a number of convex shapes you would then use to create a btCompoundShape, which is probably best performance-wise and why it is recommended there. btGimpactMeshShape is probably simpler to use, but worse performance.

Re: Basic 3D Mesh Collision question

Posted: Tue Jan 06, 2015 7:33 am
by moeizle
Thank you for your reply,

i actually used to decompose the model into convex shapes and use btConvexHullShape, but the problem is that it seems that the order in which i insert the vertices to the btConvexHullShape matters, that is i get different collision results depending on the order i enter the vertices of the same convex object. I thought maybe this has to do with the order of construction of the convex hull inside btConvexHullShape, so that's why i resorted to a triangle mesh.

did you face this ordering problem before?