btBvHTriangleMeshShape works with some models but not others

smuffer
Posts: 16
Joined: Fri Dec 04, 2009 7:14 pm

btBvHTriangleMeshShape works with some models but not others

Post by smuffer »

Hi there,
Im currently trying to make a racing game and because the track will be a static object im making the collision shape using btBvHTriangleMeshShape. When creating the collision shape for the track it comes out completely wrong, yet when I make the collision shape for a different a different object like an enemy it comes out fine?! I made a debug drawer to show the differences. The purple versions are the collision shapes.

Image
enemy without collision.

Image
enemy with collision. Notice how it takes the shape perfectly.

Image
track without collision.

Image
track with collision. Notice how it is completely messed up?!

The only difference I can tell is that the track is a loop. As in it is an object with a hole in it. Could this be causing problems or am I chasing a crazy idea?

Does anybody have any ideas as to why this would be happening?
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: btBvHTriangleMeshShape works with some models but not others

Post by Erwin Coumans »

Can you share the model that gives problems as a Maya, Blender, .fbx or .obj file?
(zipped attachment to this forum)

Thanks,
Erwin
smuffer
Posts: 16
Joined: Fri Dec 04, 2009 7:14 pm

Re: btBvHTriangleMeshShape works with some models but not others

Post by smuffer »

Hiya, yeah that would be great if you could take a look at them. I dont have the original versions but I do have the .x file exports as my group are using directx.

I have included the working model called the kazie and the track which does not work.

Thanks for the help.
Models.rar
You do not have the required permissions to view the files attached to this post.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: btBvHTriangleMeshShape works with some models but not others

Post by Erwin Coumans »

Both models collide fine when imported in Blender, and reading the .blend file. Download gamekit executable (IrrKit), unzip attached .blend files and drag them onto the IrrKit.exe.

It could be just a rendering problem. Have you tried using the debug drawer (derive your own class from btIDebugDraw). There must be an issue with your mesh/btBvhTriangleMesh initialization. Are you using the btStridingMeshInterface? It can be errorprone to get the striding values right. Can you first try out btTriangleMesh instead?

Thanks,
Erwin
You do not have the required permissions to view the files attached to this post.
smuffer
Posts: 16
Joined: Fri Dec 04, 2009 7:14 pm

Re: btBvHTriangleMeshShape works with some models but not others

Post by smuffer »

So I tried that out and your right they do seem to work ok in irrKit. I have tried to use btIDebugDrawer but couldnt get it working so ended up implementing the collision shape drawer from http://www.programmingmind.com/bullet-p ... ng-directx which is what I used in the pictures above. Im quite sure that it is drawing correctly because even when I try to drop something through the middle of the track it collides with something as the pictures shows.

This is how im currently building my static objects:

Code: Select all

bool StaticEntity::Create( D3DXVECTOR3 position, float mass, LPD3DXMESH mesh )
{
	TriMeshData *data = new TriMeshData;

	DWORD numVertices = mesh->GetNumVertices();
	DWORD numFaces = mesh->GetNumFaces();

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

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

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

	mesh->UnlockVertexBuffer();

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

	mesh->UnlockIndexBuffer();		

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

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

	m_collisionShape = new btBvhTriangleMeshShape(data->indexVertexArrays, true);

	btTransform trans;
	trans.setIdentity();	
	trans.setOrigin(ConvertVector(position));

	btVector3 inertia(0,0,0);
	btDefaultMotionState* motionState = new btDefaultMotionState( trans );
	btRigidBody::btRigidBodyConstructionInfo cInfo(mass,motionState,m_collisionShape,inertia);
	m_rigidBody = new btRigidBody(cInfo);

	return true;
}
Am I doing something wrong? So I should try using btTriangleMesh instead of btBvHTriangleMeshShape?
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: btBvHTriangleMeshShape works with some models but not others

Post by Erwin Coumans »

The problem is likely in btTriangleIndexVertexArray, striding or other issue. Also, if you are using btTriangleIndexVertexArray you should not delete the underlying index/vertex array data (there is no copy made).

Best is to first test where the problem is, by using btTriangleMesh instead of btTriangleIndexVertexArray.

This will be fed into the constructor of btBvhTriangleMeshShape (btTriangleMesh replaces btTriangleIndexVertexArray).

Hope this helps,
Erwin
smuffer
Posts: 16
Joined: Fri Dec 04, 2009 7:14 pm

Re: btBvHTriangleMeshShape works with some models but not others

Post by smuffer »

Hiya,

so I tried substituting the btTriangleIndexVertexArray for the btTriangleMesh and still the same result. Once again my other models work fine but the tracks collision is still going wrong.
I used this thread as a guide http://www.bulletphysics.org/Bullet/php ... f=9&t=2961
This is the code I now have to build the static collision model btBvhTriangleMeshShape:

Code: Select all

bool StaticEntity::Create( D3DXVECTOR3 position, float mass, LPD3DXMESH mesh )
{
	TriMeshData *data = new TriMeshData;

	DWORD numVertices = mesh->GetNumVertices();
	DWORD numFaces = mesh->GetNumFaces();


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

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

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

	mesh->UnlockVertexBuffer();

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

	mesh->UnlockIndexBuffer();		

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

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

//----------------------NEW CODE HERE------------------
   btTriangleMesh* trimesh = new btTriangleMesh();
   for( DWORD i = 0; i < numFaces; i++ )
   {
      int index0 = data->indices[i*3];
      int index1 = data->indices[i*3+1];
      int index2 = data->indices[i*3+2];

      btVector3 vertex0(data->vertices[index0*3], data->vertices[index0*3+1],data->vertices[index0*3+2]);
      btVector3 vertex1(data->vertices[index1*3], data->vertices[index1*3+1],data->vertices[index1*3+2]);
      btVector3 vertex2(data->vertices[index2*3], data->vertices[index2*3+1],data->vertices[index2*3+2]);

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

	m_collisionShape = new btBvhTriangleMeshShape(trimesh, true);

//--------------------------------------------------------------

	btTransform trans;
	trans.setIdentity();	
	trans.setOrigin(ConvertVector(position));

	btVector3 inertia(0,0,0);
	btDefaultMotionState* motionState = new btDefaultMotionState( trans );
	btRigidBody::btRigidBodyConstructionInfo cInfo(mass,motionState,m_collisionShape,inertia);
	m_rigidBody = new btRigidBody(cInfo);

	return true;
}
So does this mean that the track mesh is faulty? Then again it worked with irrKit?!

I appreciate the help and any more ideas would be great.
smuffer
Posts: 16
Joined: Fri Dec 04, 2009 7:14 pm

Re: btBvHTriangleMeshShape works with some models but not others

Post by smuffer »

*SOLVED*

The answer was.... NEVER TRUST YOUR ARTISTS!!

I dont know much in the way of modelling but it had something to do with exporting and OBJ's.