btBvhTriangleMeshShape does not work...

yamashi2
Posts: 8
Joined: Sun Sep 06, 2009 2:33 pm

btBvhTriangleMeshShape does not work...

Post by yamashi2 »

Hello,

I am trying to use the btBvhTriangleMeshShape, I tried to feed a btTriangleMesh and then a btTriangleIndexVertexArray, in both cases my models don't collide at all...
The funny thing is when I use btConvexTriangleMeshShape it works, well not really because it's not the actual shape that I want...
If I could get any help...

Code: Select all

	btVector3* vert = new btVector3[mModel.header.vertexCount];
	int* indices = new int[mModel.header.triangleCount*3];
	
	for(int j=0; j<mModel.header.vertexCount; j++)
	{
		GLVertex* glVertex = &mModel.vertices[j];
		vert[j] = btVector3(glVertex->position.x,glVertex->position.y, glVertex->position.z);
	}
	
	for(int j=0; j<mModel.header.triangleCount; j++)
	{
		for(int k = 0; k < 3; ++k)
		{
			indices[j*3 + k] = mModel.triangles[j].vertexIndices[k];
		}
	}	
	
	btTransform startTransform;
	btQuaternion quat;
	startTransform.setIdentity();
	startTransform.setOrigin(btVector3(0,0,0));	
	
	btDefaultMotionState* lMotionState = new btDefaultMotionState(startTransform);
	
	btVector3 aabbMin(-1000,-1000,-1000),aabbMax(1000,1000,1000);	
	btBvhTriangleMeshShape* trimeshShape  = new btBvhTriangleMeshShape(
		new btTriangleIndexVertexArray(mModel.header.triangleCount,indices, sizeof(int),mModel.header.vertexCount,(btScalar*) &vert[0].x(),sizeof(btVector3))
	    ,true,aabbMin,aabbMax);
	
	btScaledBvhTriangleMeshShape* shape = new btScaledBvhTriangleMeshShape(trimeshShape,btVector3(1,1,1));
	mRigidBody = new btRigidBody(1.f, lMotionState, shape, btVector3(0,0,0));	
	
	BulletEngine::GetInstance().GetDynamicWorld()->addRigidBody(mRigidBody);
Thanks !
Ripiz
Posts: 47
Joined: Mon Aug 16, 2010 10:43 am

Re: btBvhTriangleMeshShape does not work...

Post by Ripiz »

Referring to new btTriangleIndexVertexArray(...
It should be 3*sizeof(int) not just sizeof(int), as it requires index size of one triangle. Also I'm not sure if it works with integer, I'm using unsigned short.
btVector3 is way bigger than 3 floats, so it can't calculate things correctly, create new struct/class, either use own vector if it's exactly 3 floats in size.
Also it seems you have more parameters than documentation says are needed, which is weird.
yamashi2
Posts: 8
Joined: Sun Sep 06, 2009 2:33 pm

Re: btBvhTriangleMeshShape does not work...

Post by yamashi2 »

I changed the sizeof(int) to sizeof(int) * 3 but tstill does not work...
The rest of the code is copied from the ConcaveDemo...
Ripiz
Posts: 47
Joined: Mon Aug 16, 2010 10:43 am

Re: btBvhTriangleMeshShape does not work...

Post by Ripiz »

sizeof(btVector3) isn't equal to 3 * sizeof(float), or any other known size, so function doesn't work properly.
Create own structure for vertex like struct Vector3D{ float x,y,z; }; and fill values, then change parameters and it'll work.
yamashi2
Posts: 8
Joined: Sun Sep 06, 2009 2:33 pm

Re: btBvhTriangleMeshShape does not work...

Post by yamashi2 »

Well the code comes from an official demo shipped with the SDK so I don't think it's the reason why it's not working...
I will try when I get home...
yamashi2
Posts: 8
Joined: Sun Sep 06, 2009 2:33 pm

Re: btBvhTriangleMeshShape does not work...

Post by yamashi2 »

Using a capsule shape did the trick for the character.