btConvexHullShape vs btBvhTrimeshShape interaction problem

User avatar
shotgunnutter
Posts: 17
Joined: Thu Nov 01, 2007 2:13 am

btConvexHullShape vs btBvhTrimeshShape interaction problem

Post by shotgunnutter »

Basically, there isn't any. If i create a sphere, the convex hulls interact with it quite normally, however they completely ignore the trimesh, falling right through it. I know that my data is valid because I am using the same data for rendering, and there are no problems there. I suspect my problem is related to not registering collision algorythms. Is there something I have missed here?

Here is the system I am using to generate trimeshes:

Code: Select all

void TrimeshAddTriangles(btTriangleMesh *& trimesh, StaticMesh * source)
{//recursively copy all triangles from source into trimesh
	int nk = source->num_kids;
	int ns = source->num_surfaces;
	
	static g_vector globalPos(0,0,0);
	g_vector localPos(source->position.x,source->position.y,source->position.z);
	
	globalPos += localPos; 
	//we only currently deal with translation, not rotation, so adding and subtracting
	//can be used in place of push/popping the matrix.
	
	for(int s = 0; s < ns; s++)
	{
		btVector3 V0,V1,V2;
		float gx,gy,gz;
		globalPos.get_vector(gx,gy,gz);
		
		V0.setValue(
			source->vertices[source->surfaces[s].vertrefs[0]].x + gx,
			source->vertices[source->surfaces[s].vertrefs[0]].y + gy,
			source->vertices[source->surfaces[s].vertrefs[0]].z + gz
		);
		
		V1.setValue(
			source->vertices[source->surfaces[s].vertrefs[1]].x + gx,
			source->vertices[source->surfaces[s].vertrefs[1]].y + gy,
			source->vertices[source->surfaces[s].vertrefs[1]].z + gz
		);
		
		V2.setValue(
			source->vertices[source->surfaces[s].vertrefs[2]].x + gx,
			source->vertices[source->surfaces[s].vertrefs[2]].y + gy,
			source->vertices[source->surfaces[s].vertrefs[2]].z + gz
		);
		
		trimesh->addTriangle(V0,V1,V2);
	}
	
	for (int k=0; k < nk; k++)
	{
		TrimeshAddTriangles(trimesh,source->kids[k]);
	}
	globalPos = globalPos - localPos;
}

void TrimeshFromStaticMesh (btCollisionShape *& trimeshShape, StaticMesh * source)
{
	btTriangleMesh* trimesh = new btTriangleMesh();
	TrimeshAddTriangles(trimesh, source);
	
	bool useQuantizedBvhTree = true;	
	trimeshShape = new btBvhTriangleMeshShape(trimesh,useQuantizedBvhTree); 
}
Here is how I generate convex hulls:

Code: Select all

void ConvexHullAddPoints(btConvexHullShape * dest, StaticMesh * source)
{
	int nk = source->num_kids;
	int nv = source->num_vertices;
	
	static g_vector globalPos(0,0,0);
	g_vector localPos(source->position.x,source->position.y,source->position.z);
	
	globalPos += localPos; 
	
	for(int v=0; v<nv; v++)
	{
		g_vector vec = globalPos + g_vector(source->vertices[v].x,source->vertices[v].y,source->vertices[v].z);
		dest->addPoint(btPoint3(vec.x, vec.y, vec.z));
	}
	
	for(int k=0; k<nk; k++)
	{
		ConvexHullAddPoints(dest,source->kids[k]);
	}
	globalPos = globalPos - localPos;
}

void ConvexHullFromStaticMesh  (btCollisionShape *& convexHullShape, StaticMesh * source)
{
	btConvexHullShape * hull = new btConvexHullShape();
	ConvexHullAddPoints(hull,source);
	convexHullShape = hull;
}

I am initialising the physics engine like so:

Code: Select all

	// collision configuration contains default setup for memory, collision setup. Advanced users can create their own configuration.

	btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();



	// use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)

	btCollisionDispatcher* dispatcher = new	btCollisionDispatcher(collisionConfiguration);



	// the maximum size of the collision world. Make sure objects stay within these boundaries

	// Don't make the world AABB size too large, it will harm simulation quality and performance
	btVector3 worldAabbMin(-10000,-1000,-10000);
	btVector3 worldAabbMax(10000,1000,10000);
	int	maxProxies = 1024;
	btAxisSweep3* overlappingPairCache = new btAxisSweep3(worldAabbMin,worldAabbMax,maxProxies);
	// the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)

	btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;

	dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,overlappingPairCache,solver,collisionConfiguration);

	dynamicsWorld->setGravity(btVector3(0,-10,0));
Thanks for any advice.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: btConvexHullShape vs btBvhTrimeshShape interaction problem

Post by Erwin Coumans »

Check out and compare your setup with BenchmarkDemo6 in Bullet/Demos/Benchmarks.

It shows 1000 objects with a btConvexHullShape falling on a concave btBvhTriangleMeshShape.

You don't need to manually register additional collision algorithms for this.
Hope this helps,
Erwin
User avatar
shotgunnutter
Posts: 17
Joined: Thu Nov 01, 2007 2:13 am

Re: btConvexHullShape vs btBvhTrimeshShape interaction problem

Post by shotgunnutter »

Well, I managed to fix it in the end, the problem was the way I was handling relative transformations of child objects in my static meshes. Thanks for the advice. I actually understood the bits of the bullet API that I was using perfectly well.

Thanks again.