Help with Trimesh collision

yxsam
Posts: 2
Joined: Thu Sep 18, 2008 7:01 am
Location: Singapore

Help with Trimesh collision

Post by yxsam »

Pardon my weak understanding of programming but i only started this for 2 years.

I'm programming a 3D space simulator with OpenGL. I wrote a .obj file loader and i have the total number of vertices/normals/texCoord/triangles and even indices. Currently i skip the external files for materials and textures though.
But I can still render the mesh out nicely in the window nonetheless so it should not be a problem for the trimesh part.

I tried multiple ways to use the btBvhTriangleMeshShape function. Searching through forums and stuff like that all points back to refer to concave demo or vehicle demo. Even so i had problem getting the trimesh collision to work. I followed and copied out the required steps. Since everyone in the forum kinda got it to work i feel kinda depress not to make any progress after a week T_T.

Anyways these are the ways i tried to make the trimesh rigidbody. Initially i attempted with a complex model but after having the impression that it's my model might be fualty i switch to a simple box model.

Method 1:

Code: Select all

int vertStride = sizeof(btVector3);
int indexStride = 3*sizeof(int);
int totalTriangles = 0;
int totalVertices = 0;
int TempTotalVertices = 0;
int index=0;

// Vertices clean up, arrange them into btVector3

gVertices = new btVector3[ObjExtend.mNumVertices];
for (int i = 0; i < (ObjExtend.mNumVertices); i++)
{
	gVertices[i] = btVector3(ObjExtend.mVertex[index],ObjExtend.mVertex[index+1],ObjExtend.mVertex[index+2]);
	index += 3;
}
index = 0;

// Another clean up, in Obj i took out the indices of the Vertices/UV/Normals
// Doing strides of 3 to get only the indices for the vertices

gIndices = new int[totalTriangles*3];
for (int i = 0; i < (ObjExtend.mNumIndices); i++)
{
	gIndices[i] = ObjExtend.mIndices[index];
	index += 3;
}
index = 0;


indexVertexArray = new btTriangleIndexVertexArray(
                          totalTriangles,
		gIndices,
		indexStride,
		ObjExtend.mNumVertices,(btScalar*) &gVertices[0].x(),vertStride);

bool useQuantizedAabbCompression = true;

btVector3 aabbMin(-1000,-1000,-1000);
btVector3 aabbMax(1000,1000,1000);

trimeshShape  = new btBvhTriangleMeshShape(indexVertexArray,useQuantizedAabbCompression,aabbMin,aabbMax);
collisionShapesArray.push_back(trimeshShape);

// Tried with and without serialization
void* buffer = 0;
int numBytes = trimeshShape->getOptimizedBvh()->calculateSerializeBufferSize();
buffer = btAlignedAlloc(numBytes,16);
bool swapEndian = false;
trimeshShape->getOptimizedBvh()->serialize(buffer,numBytes,swapEndian);
FILE* file = fopen("bvh.bin","wb");
fwrite(buffer,1,numBytes,file);
fclose(file);
btAlignedFree(buffer);

//btCollisionShape* ObjectShape, has been intialized, it was used for a sphere shape, collisionShapesArray[0] holds 
//a sphere

ObjectShape = collisionShapesArray[1];
 
mass = 0;
trans.setIdentity();

staticBody = new btRigidBody(mass,0,ObjectShape,btVector3(0,0,0));	
staticBody->setWorldTransform(trans);
 
dynamicsWorld->addRigidBody(staticBody);
 
staticBody->setCollisionFlags(staticBody->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);//STATIC_OBJECT);
I went through the codes line by line, it only crash when i addRigidBody to the world, since other functions did return smoothly i assume my inputs are more or less correct.

Method 2 i create a btTriangleMesh using the loaded vertices and indices. Then i pass it into btBvhTriangleMeshShape for the first parameter replacing the indexVertexArray.

Code: Select all

btCollisionShape* terrainShape = new btBvhTriangleMeshShape(pTriMesh, true, false); 
collisionShapesArray.push_back(terrainShape);

btTransform trans; 
trans.setIdentity(); 
trans.setOrigin(btVector3(0, 0, 0)); 

mass = 0; 
bool isDynamic = (mass != 0.f); 
 
btVector3 localInertia(0, 0, 0); 

if (isDynamic) 
 	terrainShape->calculateLocalInertia(mass,localInertia); 
 
btDefaultMotionState* myMotionState = new btDefaultMotionState(trans); 
btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,terrainShape,localInertia); 
btRigidBody* body = new btRigidBody(rbInfo); 
 
dynamicsWorld->addRigidBody(body); 
Same deal, it's the addRigidBody that crashes the program. I double confirmed that pTriMesh is correct by rendering the passed in vertices values on screen and it looks fine.

Am i missing anything? Or am i missing lots of things. I'm not sure since thats the stuff i figured i need from the examples and they somehow having the same problem whichever i tried.

I can post my codes here but tell me which part thats the likelyhood to cause problems since my file is kinda huge.

Still any simplified example will be of great help, seeing those #ifdef and #endif makes me wanna cry T_T, though i would wanna do the same one day since it looks leet.
rusty
Posts: 25
Joined: Fri Sep 19, 2008 10:23 am

Re: Help with Trimesh collision

Post by rusty »

Have you tried adding a test collision object that uses the triangle mesh shape to see if that crashes too? So instead of attaching it to your rigid body, create a btCollisionObject and add that.

If it crashes, it's a problem with your mesh data. If not, it's just an issue with using the triangle mesh for a dyanmic object.

Personaly, I would use a convex hull shape for the ship collision.
oBFusCATed
Posts: 3
Joined: Tue Sep 30, 2008 9:51 pm

Re: Help with Trimesh collision

Post by oBFusCATed »

Hello,
Do you have Bullet compiled in debug mode? In the beginning I was using only release build and when something crashes in Bullet I was getting very strange and misleading backtraces/call stacks.

There are asserts here and there, but when you use a release build they don't trigger and you get strange crashes.
btBvhTriangleMeshShape can be used for static objects only. If you use it with dynamic or kinematic(not 100% sure if it is valid) one it crashes. There is an assert in there (if I remember correclty) that checks if the object is dynamic.

Good luck and I'm sorry if I've made some mistakes, I can't look at sources to verify my statements at the moment.
yxsam
Posts: 2
Joined: Thu Sep 18, 2008 7:01 am
Location: Singapore

Re: Help with Trimesh collision

Post by yxsam »

rusty wrote: Personaly, I would use a convex hull shape for the ship collision.
Thank you so much!, i used the hull and now it works. So in the end i don't know what exactly went wrong ~_~