How would I use GLM for Bullet Physics?

Molmasepic
  • How would I use GLM for Bullet Physics? Molmasepic

    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;
            trimesh->addTriangle( *v0, *v1, *v2 );
            ///trimesh->addTriangle(mesh->triangles[t].vindices[0]*100,mesh->triangles[t].vindices[1]*100,mesh->triangles[t].vindices[2]*100);
        }
        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);
        PhysicsWorld->addRigidBody(ContiRigid);
    

    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);
    PhysicsWorld->addRigidBody(ContiRigid);
    

    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;
    trimesh->addTriangle( v0, v1, v2 );
    

    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)));
    

  • 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

  • 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

Tags
opengl c++ 3d-meshes blender bullet-physics
Related questions and answers
  • btDefaultMotionState(Transform); btCollisionShape *Shape = new btStaticPlaneShape(btVector3(0,1,0),0); // Add Mass btVector3 LocalInertia; Shape-&gt;calculateLocalInertia(0, LocalInertia); // CReate...::QuaternionToEuler(const btQuaternion &amp;TQuat, btVector3 &amp;TEuler) { btScalar W = TQuat.getW(); btScalar X = TQuat.getX(); btScalar Y = TQuat.getY(); btScalar Z = TQuat.getZ(); float WSquared = W...(Transform); btCollisionShape *Shape = new btStaticPlaneShape(btVector3(0,1,0),0); // Add Mass btVector3 LocalInertia; Shape-&gt;calculateLocalInertia(0, LocalInertia); // CReate the rigid body object

  • ) initialization: dynamicsWorld-&gt;setGravity(btVector3(0,-10,0)); ballShape = new btSphereShape(1); fallMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(2,1,0))); btScalar mass = 5; btVector3 fallInertia(0,0,0); ballShape-&gt;calculateLocalInertia(mass,fallInertia); groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(1.),btScalar(70.))); btDefaultMotionState* groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,-1,0))); btRigidBody::btRigidBodyConstructionInfo groundRigidBodyCI

  • ,-10,0)); ballShape = new btSphereShape(1); pinShape = new btCylinderShape(btVector3(1,1,1)); pinShape-&gt;setMargin(0.04); fallMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,10,0))); btScalar mass = 1; btVector3 fallInertia(0,0,0); ballShape-&gt;calculateLocalInertia(mass,fallInertia); btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),1); btDefaultMotionState* groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,-1,0))); btRigidBody::btRigidBodyConstructionInfo groundRigidBodyCI

  • , In); // Get The Current Line ( NEW ) shaderData[i][0] = shaderData[i][1] = shaderData[i][2] = float(atof (Line)); // Copy Over The Value ( NEW ) } fclose...); // Set The Color Of The Model ( NEW ) // ORIGINAL DRAWING CODE //Draw the model as an interpolation between the two frames glBegin(GL_TRIANGLES); for(int i = 0; i < numTriangles; i++) { MD2Triangle* triangle = triangles + i; for(int j = 0; j < 3; j++) { MD2Vertex* v1 = frame1-&gt;vertices + triangle-&gt;vertices[j

  • , so I planned to start over. The libraries that I have used are SFML, OpenGL, and Bullet Physics, along with GLM to load the models. But the problem is that the 3D graphics arent working with the Physics correctly... So my main question is: What would be a good combination of libraries to make an online game with? Im sure that many people have good combinations of libraries for making a game..., that would be nice) Networking(a way to connect multiple clients to a server/database) and Physics(Ive worked with Bullet Physics, but I cant find a model setup that would fit with said physics

  • I want to create an object than only moves along the the X and Z axes, while keeping the Y-axis disabled (it's like movement in 2D, and the object wont fall down). I'm currently using a 6 dof constraint to restrict movement in the Y-axis, but it's not working: btRigidBody* zeroBody = new btRigidBody(0, NULL, NULL); // Create the body that we attach things to btRigidBody* robot = mCarChassis-&gt;getBulletRigidBody(); btGeneric6DofConstraint* constrict = new btGeneric6DofConstraint(*robot, *zeroBody, btTransform::getIdentity(), btTransform::getIdentity(), false); constrict-&gt

  • I have got stupid and annoying problem in my app. I am using bullet physics and I've started with hello world example on wiki: http://bulletphysics.org/mediawiki-1.5.8/index.php/Hello_World There is someting like this: btDefaultMotionState* fallMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,50,0))); What is described as set object 50m above something. So I can change the height but nothing more. I can't move it left, right - anything. Also when I set 0 everywhere ( I mean in btVector3) the object is jumping up rather than falling down

  • a = 0; a < (int)groups.size(); a++) { if(groups[a].type == "prop") { //Code for rotation glPopMatrix(); glPushMatrix(); float x,y,z; x = y = z = 0; int... /= Count; y /= Count; glTranslatef(0, -y, -z); glRotatef(angle, 1, 0, 0); glTranslatef(0, y, z); } for(int b = 0; b < (int)groups[a].faces.size(); b...(vertices[vertexIndex].Dimensions[_x], vertices[vertexIndex].Dimensions[_y], vertices[vertexIndex].Dimensions[_z]); } glEnd(); } } glPopMatrix(); glfwSwapBuffers(); Since I don't know

  • : float m_Position[3]; // x, y, z // offset 0, size = 3*sizeof(float) float m_TexCoords[2]; // u, v // offset 3*sizeof(float), size = 2*sizeof(float) float m_Normal[3... onscreen, in the shape of the terrain rectangle, but there are no regular lines etc. Here's the code I use for rendering: void ShaderProgram::Draw() { using namespace AntiMatter; if( ! m... in the shader code) for( int n = 0; n < vShaderArgs.size(); n ++) glBindAttribLocation( m_nProgramId, n, vShaderArgs[n].sFieldName.c_str() ); // Create and bind to a vertex array object, which

Data information