Collsion between btBoxShape and btTriangleMesh

Filippo
Posts: 1
Joined: Wed Dec 10, 2008 7:46 am

Collsion between btBoxShape and btTriangleMesh

Post by Filippo »

Hi, im pretty new to Bullet and Im trying to get started with collision detection. My goal into collision detection into a larger application.

What i want to do is to collide arbitraty triangle meshes against other triangle meshes and boxes, spheres etc.

My attempt to collide a box against a trianglemesh fails and I hope someone could give me a pointer on how to solve this.

Basically what I do is to create box and add it to the collisionworld:

btMatrix3x3 basisA;
basisA.setIdentity();

_collisionObject.getWorldTransform().setBasis(basisA);

btBoxShape* boxA = new btBoxShape(btVector3(5, 5, 5));

_collisionObject.setCollisionShape(boxA);
_collisionObject.getWorldTransform().setOrigin(0, 0, 0);

-> add collisionobject to world

Secondly, I load a .osg file, loop all the vertex data and construct a triangle mesh from the vertex information in the .osg file. The code looks something like this:


btTriangleMesh* trimesh = new btTriangleMesh();

btConvexShape* convexShape = new btConvexTriangleMeshShape(trimesh);

for( all vertices in osg file)
{
trimesh->addTriangle(v1, v2, v3);
-> jump three vertices and continue loop
}

btBvhTriangleMeshShape* shape = new btBvhTriangleMeshShape(trimesh, true);

btMatrix3x3 basisA;
basisA.setIdentity();

_collisionObject.getWorldTransform().setBasis(basisA);

_collisionObject.setCollisionShape(shape);

-> add collisionObject to collisonWorld

Am I using the correct approach to setup a triangle mesh for collision. My method does not register a collision. Can anyone show my a simple example on how to create a trianglemesh from a vertex array and add to the collision world.

Any help greatly appreciated!!!


Cheers,
Filip
mickey
Posts: 107
Joined: Fri Sep 19, 2008 6:08 pm

Re: Collsion between btBoxShape and btTriangleMesh

Post by mickey »

Hi

I think you maybe doing it the other way around. Usually I create a btBvhTriangleMeshShape first, and if I want a simplified mesh, I create a btConvexTriangleMeshShape from a btTriangleMesh which is filled up with vertices from btBvhTriangleMeshShape.

Here's my 2 functions that create both - just a warning though, this function allocates memory and you need to delete the allocation yourself. Also its in DirectX.

Code: Select all

btBvhTriangleMeshShape *CreateBvhTriangleMeshShape(LPD3DXMESH pMesh, TriMeshData *pData)
{
	DWORD numVertices		= pMesh->GetNumVertices();
	DWORD numFaces			= pMesh->GetNumFaces();

	Vertex *v = 0;
	pMesh->LockVertexBuffer(0, (void**)&v);

	// Extract vertices
	pData->vertices = new btScalar[numVertices * 3];	

	for(UINT i = 0; i < numVertices; i++)
	{
		pData->vertices[i*3+0] = v[i].position.x;
		pData->vertices[i*3+1] = v[i].position.y;
		pData->vertices[i*3+2] = v[i].position.z;		
	}	

	pMesh->UnlockVertexBuffer();

	// Extract indices
	pData->indices = new int[numFaces * 3];	
	WORD* ind = 0;
	pMesh->LockIndexBuffer(0,(void**)&ind);		
	
	//memcpy( &indices, &ind, sizeof(ind));	
	for (int i=0;i < numFaces; i++)
	{	
		pData->indices[i*3+0] = ind[i*3+0];
		pData->indices[i*3+1] = ind[i*3+1];
		pData->indices[i*3+2] = ind[i*3+2];		
	}

	pMesh->UnlockIndexBuffer();		

	int indexStride = 3 * sizeof(int);
	int vertStride = sizeof(btVector3);

	pData->indexVertexArrays = new btTriangleIndexVertexArray(numFaces, pData->indices, indexStride,
		numVertices, (btScalar*) &pData->vertices[0], sizeof(btScalar) * 3);

	bool useQuantizedAabbCompression = true;
	btBvhTriangleMeshShape *shape = new btBvhTriangleMeshShape(pData->indexVertexArrays, true);	
		
	//delete indices;
	//delete vertices;	
	//delete indexVertexArrays;	

	return shape;
}
And here's how I create a simplified convex hull:

Code: Select all

btConvexHullShape *CreateConvexHullShape( LPD3DXMESH pMesh )
{
	TriMeshData trimeshshape;
	CreateBvhTriangleMeshShape( pMesh, &trimeshshape );					

	btTriangleMesh* trimesh = new btTriangleMesh();
	for( int i = 0; i < pMesh->GetNumFaces(); i++ )
	{
		int index0 = trimeshshape.indices[i*3];
		int index1 = trimeshshape.indices[i*3+1];
		int index2 = trimeshshape.indices[i*3+2];

		btVector3 vertex0(trimeshshape.vertices[index0*3], trimeshshape.vertices[index0*3+1],trimeshshape.vertices[index0*3+2]);
		btVector3 vertex1(trimeshshape.vertices[index1*3], trimeshshape.vertices[index1*3+1],trimeshshape.vertices[index1*3+2]);
		btVector3 vertex2(trimeshshape.vertices[index2*3], trimeshshape.vertices[index2*3+1],trimeshshape.vertices[index2*3+2]);

		trimesh->addTriangle(vertex0,vertex1,vertex2);
	}

	btConvexShape* tmpConvexShape = new btConvexTriangleMeshShape(trimesh);

	// Create a hull approximation
	btShapeHull* hull = new btShapeHull(tmpConvexShape);
	btScalar margin = tmpConvexShape->getMargin();
	hull->buildHull(margin);	
	btConvexHullShape* simplifiedConvexShape = new btConvexHullShape();
	for(int i= 0; i < hull->numVertices(); i++ )
	{
		simplifiedConvexShape->addPoint(hull->getVertexPointer()[i]);	
	}		

	delete tmpConvexShape;
	delete hull;

	return simplifiedConvexShape;
}
And so my triangle mesh data is not cluttered, I have a struct that holds it:

Code: Select all

struct TriMeshData
{
	btScalar	*vertices;
	int			*indices;	
	btTriangleIndexVertexArray	*indexVertexArrays;
	TriMeshData()
	{
		vertices = NULL;
		indices = NULL;
		indexVertexArrays = NULL;
	}
	~TriMeshData()
	{
		if( vertices ) delete [] vertices;
		if( indices ) delete [] indices;
		if( indexVertexArrays ) delete indexVertexArrays;
	}
};
A sample call to these:

LPD3DXMESH mesh;
LoadMesh( &mesh ... );
TriMeshData *tridata = new TriMeshData();
btCollisionShape *sceneShape = CreateBvhTriangleMeshShape( mesh, tridata );

And remember to delete tridata and sceneShape afterwards.

Hope that helps.
Zern
Posts: 4
Joined: Fri Jun 03, 2011 3:21 am

Re: Collsion between btBoxShape and btTriangleMesh

Post by Zern »

@mickey,


Been years since you made this post but I just wanted to say thank you :) used your code and it works like a charm!
Just had to manually add "btshapeHull.h" and "btshapeHull.cpp" to my project file and include the header for anyone who is getting error messages about 'btShapeHull'.