# How would I use GLM for Bullet Physics?

Right now im working on my c++ game, and im having alot of trouble using GLM and Bullet Physics to create my world correctly.

The main problem is that when i export my physics with Blenderv2.57, and export my models for GLM, the models never line up with the physics correctly...

Im wondering if there are any model loaders that would be good to use with Bullet Physics, and I can export the vertices,texture coords, etc. from Blender to be loaded and used as btTriangleMeshes..

I managed to make a btBvhTriangleMesh with my GLM model, but I think its falling, or my character is not picking it up correctly..

heres the code:

void BulletifyContinent(GLMmodel* mesh,float x,float y,float z,float scale)
{
btTriangleMesh* trimesh = new btTriangleMesh();
for(int t=0;t<mesh->numtriangles;++t)
{
GLuint index0 =3*mesh->triangles[t].vindices[0];
GLuint index1 =3*mesh->triangles[t].vindices[1];
GLuint index2 =3*mesh->triangles[t].vindices[2];

GLfloat* p0 = &mesh->vertices[index0];
GLfloat* p1 = &mesh->vertices[index1];
GLfloat* p2 = &mesh->vertices[index2];

btVector3* v0 = new btVector3( p0[0], p0[1], p0[2] );
btVector3* v1 = new btVector3( p1[0], p1[1], p1[2] );
btVector3* v2 = new btVector3( p2[0], p2[1], p2[2] );
*v0 *= scale;
*v1 *= scale;
*v2 *= scale;
}
btCollisionShape* shape = 0;
bool useQuantization = true;
shape = new btBvhTriangleMeshShape(trimesh,useQuantization);
btDefaultMotionState *StillStateMOT = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,0),btVector3(x,y,z)));
btScalar Mass = 1;
btVector3 FallInertia(0,0,0);
btRigidBody::btRigidBodyConstructionInfo StillRigidCI(Mass,StillStateMOT,shape,FallInertia);
btRigidBody *ContiRigid = new btRigidBody(StillRigidCI);


Edit 1:

btScalar Mass = 1;
btVector3 FallInertia(0,0,0);
// This line is missing in your code
shape->calculateLocalInertia(Mass, FallInertia);
btRigidBody::btRigidBodyConstructionInfo StillRigidCI(Mass,StillStateMOT,shape,FallInertia);
btRigidBody *ContiRigid = new btRigidBody(StillRigidCI);


Edit 2:

There is no need to create the bullet vertices on the heap with new:

btVector3 v0( p0[0], p0[1], p0[2] );
btVector3 v1( p1[0], p1[1], p1[2] );
btVector3 v2( p2[0], p2[1], p2[2] );
v0 *= scale;
v1 *= scale;
v2 *= scale;


Edit 3:

This is not a proper identity quaternion:

btDefaultMotionState(btTransform(btQuaternion(0,0,0,0),btVector3(x,y,z)));


either do it this way:

btTransform trans;
trans.setIdentity();
trans.setOrigin(btVector3(x,y,z));
btDefaultMotionState *StillStateMOT = new btDefaultMotionState(trans);


or by passing a proper identity quaternion either yourself:

btDefaultMotionState *StillStateMOT =
new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(x,y,z)));


or using quaternion's identity member function:

btDefaultMotionState *StillStateMOT =
new btDefaultMotionState(btTransform(btQuaternion::getIdentity(),btVector3(x,y,z)));

• You forgot to calculate the local inertia for the shape, add shape->calculateLocalInertia(Mass, FallInertia); in the line after btVector3 FallInertia(0,0,0); – Maik Semder Jun 2 '11 at 6:48
• You don't need to create the triangle vertices on the heap with new, look for my edit in your question – Maik Semder Jun 2 '11 at 6:51
• I added the corrected code to your question, see Edit 1 and Edit 2 If you have further questions regarding this code, start a new question with the code examples. – Maik Semder Jun 2 '11 at 6:59
• There was an invalid quaternion passed to the initial body transform. Added Edit 3. – Maik Semder Jun 2 '11 at 7:12
• In the future please start new questions with the code details if new questions arise, it doesn't cost you anything and makes your question history more readable. Your new question was down voted because you simply repeated the old question instead of starting a new one with the new information i.e. the above code samples. Just don't repeat the old question, provide all necessary information in the new question and leave the old one as it was. – Maik Semder Jun 2 '11 at 7:16

What would be a good model loader for Bullet Physics?

Bullet has a build-in model/world-loader. Export everything in blender to .bullet and simply load it using btBulletWorldImporter

Here is a wiki explaining the details

• I wish everything were this simple :D – Jonathan Connell May 31 '11 at 9:50
• i know it has a world importer, Im doing this right now. But the problem is that Im using GLm to load my models...And when doing so, the models that are textured do not line up with the collision meshes that the .bullet file provides.. – Molmasepic May 31 '11 at 14:07
• Did you compare both data after loading? Use a debugger and compare certain vertices. Other than that it's hard to tell whats wrong. Could be everything. Use the debugger. – Maik Semder May 31 '11 at 14:23
• the main way i compared the data is checking if my player is exactly on the spot in the GLM model when colliding with the physics world. And i have multiple "areas" that were put in this physics file...Im wondering if maybe theres a way to use the vertices from the GLM loaded model for the bullet physics mesh. I have attempted this before and it caused the game to crash...i wondering how to do it right.. – Molmasepic May 31 '11 at 14:49
• Yes you can use the vertices from GLM. You have to use a btBvhTriangleMeshShape and pass this as colision shape when creating the rigid body. You will find some examples when you search in the bullet demos for btBvhTriangleMeshShape. I'll edit my answer and add the example usage from a demo – Maik Semder May 31 '11 at 15:12

And when doing so, the models that are textured do not line up with the collision meshes that the .bullet file provides

How much off are they? Bullet adds a little margin to the boundaries of each body depending of the collision shape, that's ok and pretty small (~0.04) This is what the Docs say:

Collision Margin

Bullet uses a small collision margin for collision shapes, to improve performance and reliability of the collision detection. It is best not to modify the default collision margin, and if you do use a positive value: zero margin might introduce problems. By default this collision margin is set to 0.04, which is 4 centimeter if your units are in meters (recommended).

Dependent on which collision shapes, the margin has different meaning. Generally the collision margin will expand the object. This will create a small gap. To compensate for this, some shapes will subtract the margin from the actual size. For example, the btBoxShape subtracts the collision margin from the half extents. For a btSphereShape, the entire radius is collision margin so no gap will occur. Don’t override the collision margin for spheres. For convex hulls, cylinders and cones, the margin is added to the extents of the object, so a gap will occur, unless you adjust the graphics mesh or collision size. For convex hull objects, there is a method to remove the gap introduced by the margin, by shrinking the object. See the Demos/BspDemo for this advanced use

• theyre not off all the same..some of the models could be 2-3 units off, while other models could be exactly on point..im going to attempt the btTriangleMesh again, but im not sure how it will work since i moved the graphics model to another spot(translate) and i also scaled most of the models – Molmasepic May 31 '11 at 17:21
• You can't scale rigid bodies with bullet. What do you mean you scaled them? – Maik Semder May 31 '11 at 18:49
• It sounds more like the rendering is wrong. Like i.e. wrong matrix multiplication order. – Maik Semder May 31 '11 at 18:50
• hey i managed to get the mesh to transfer to a btBvhTriangleMesh, but i think its falling...Im going to edit my question to show the code i used to create the rigid body – Molmasepic May 31 '11 at 19:00
• Take your time, debug the issue, then write a new question with the new information. – Maik Semder May 31 '11 at 19:11