24.5 C
New York
Saturday, July 5, 2025

Create bullet physics inflexible physique alongside the vertices of a blender mannequin


I’m engaged on my first 3D recreation, for iphone, and I’m utilizing Blender to create fashions, Cocos3D recreation engine and Bullet for physics simulation. I’m attempting to be taught using physics engine.

What I’ve executed

I’ve created a small mannequin in blender which comprises a Dice (default blender dice) on the origin and a UVSphere hovering precisely on prime of this dice (with out touching the dice)

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

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

in Blender to export the mannequin to .pod format to make use of together with Cocos3D.

Within the coding facet, I added obligatory bullet information to my Cocos3D template venture in XCode.

I’m additionally utilizing a bullet goal C wrapper.

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

    /*Setup digital camera, lamp and many others.*/
    ..........
    ...........

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

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

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

    /*These boring gray colours..*/
    [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("npercentf", cVertexData[i]);
//        printf("npercentf", cVertexData[i+1]);
//        printf("npercentf", cVertexData[i+2]);
//    }

    /*Attempting to create a triangle mesh that curresponds the dice in 3D area.*/
    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 form 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);
}

Once I run the sphere and dice reveals up high quality. I anticipate the sphere object to fall immediately on prime of the dice, since I’ve given it a mass of 1 and the physics world gravity is given as -9.8 in y path. However What is going on the spere rotates round dice three or instances after which simply jumps out of the scene. Then I do know I’ve some fundamental misunderstanding about the entire course of.

So my query is, how can I create a physics collision form which corresponds to the form of a selected mesh mannequin. I may have advanced shapes than dice and sphere, however earlier than going into them I need to perceive the ideas.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles