Create bullet physics rigid body along the vertices of a blender model

Posted by Krishnabhadra on Game Development See other posts from Game Development or by Krishnabhadra
Published on 2013-07-02T10:01:47Z Indexed on 2013/07/02 11:15 UTC
Read the original article Hit count: 403

I am working on my first 3D game, for iphone, and I am using Blender to create models, Cocos3D game engine and Bullet for physics simulation. I am trying to learn the use of physics engine.

What I have done

I have created a small model in blender which contains a Cube (default blender cube) at the origin and a UVSphere hovering exactly on top of this cube (without touching the cube) enter image description here

I saved the file to get MyModel.blend. Then I used

File -> Export -> PVRGeoPOD (.pod/.h/.cpp)

in Blender to export the model to .pod format to use along with Cocos3D.

In the coding side, I added necessary bullet files to my Cocos3D template project in XCode.

I am also using a bullet objective C wrapper.

-(void) initializeScene {
    _physicsWorld = [[CC3PhysicsWorld alloc] init];
    [_physicsWorld setGravity:0 y:-9.8 z:0];

    /*Setup camera, lamp etc.*/
    ..........
    ...........

    /*Add models created in blender to scene*/
    [self addContentFromPODFile: @"MyModel.pod"];

    /*Create OpenGL ES buffers*/
    [self createGLBuffers];

    /*get models*/
    CC3MeshNode* cubeNode    = (CC3MeshNode*)[self getNodeNamed:@"Cube"];
    CC3MeshNode* sphereNode  = (CC3MeshNode*)[self getNodeNamed:@"Sphere"];

    /*Those boring grey colors..*/
    [cubeNode setColor:ccc3(255, 255, 0)];
    [sphereNode setColor:ccc3(255, 0, 0)];

    float *cVertexData  = (float*)((CC3VertexArrayMesh*)cubeNode.mesh).vertexLocations.vertices;
    int cVertexCount    = (CC3VertexArrayMesh*)cubeNode.mesh).vertexLocations.vertexCount;

    btTriangleMesh* cTriangleMesh  = new btTriangleMesh();

//    for (int i = 0; i < cVertexCount * 3; i+=3) {
//        printf("\n%f", cVertexData[i]);
//        printf("\n%f", cVertexData[i+1]);
//        printf("\n%f", cVertexData[i+2]);
//    }

    /*Trying to create a triangle mesh that curresponds the cube in 3D space.*/
    int offset = 0;
    for (int i = 0; i < (cVertexCount / 3); i++){
        unsigned int index1 = offset;
    unsigned int index2 = offset+6;
    unsigned int index3 = offset+12;
    cTriangleMesh->addTriangle(
           btVector3(cVertexData[index1], cVertexData[index1+1], cVertexData[index1+2] ),
           btVector3(cVertexData[index2], cVertexData[index2+1], cVertexData[index2+2] ),
           btVector3(cVertexData[index3], cVertexData[index3+1], cVertexData[index3+2]   
        ));
        offset += 18;
    }

    [self releaseRedundantData];

    /*Create a collision shape from triangle mesh*/
    btBvhTriangleMeshShape* cTriMeshShape   =   new btBvhTriangleMeshShape(cTriangleMesh,true);
    btCollisionShape *sphereShape =   new btSphereShape(1);

    /*Create physics objects*/
    gTriMeshObject  = [_physicsWorld createPhysicsObjectTrimesh:cubeNode shape:cTriMeshShape mass:0 restitution:1.0 position:cubeNode.location];
    sphereObject    = [_physicsWorld createPhysicsObject:sphereNode shape:sphereShape mass:1 restitution:0.1 position:sphereNode.location];
    sphereObject.rigidBody->setDamping(0.1,0.8);
}

When I run the sphere and cube shows up fine. I expect the sphere object to fall directly on top of the cube, since I have given it a mass of 1 and the physics world gravity is given as -9.8 in y direction. But What is happening the spere rotates around cube three or times and then just jumps out of the scene. Then I know I have some basic misunderstanding about the whole process.

So my question is, how can I create a physics collision shape which corresponds to the shape of a particular mesh model. I may need complex shapes than cube and sphere, but before going into them I want to understand the concepts.

© Game Development or respective owner

Related posts about collision-detection

Related posts about blender