# Create a physics body with hole : bullet physics

I am writing an iOS game, using bullet for physics, blender to create models, cocos3d as the game engine.

I have a model like this

Basically there is a plane, with a hole, and a UVSphere. Both were created in blender. What I am trying is to create a physics body along the vertices of the plane. So that when the UVSphere goes over the hole, it should fall through.

I am using btBvhTriangleMeshShape to create physics body along the vertices of this plane. code is given below

//GPlane stands for groundPlane :)
CC3MeshNode* gPlaneNode  = (CC3MeshNode*)[self getNodeNamed:@"GPlane"];

/*Get Vertex and indices data out from the .pod*/
float *gVertexData       = (float*)((CC3VertexArrayMesh*)gPlaneNode.mesh)
.vertexLocations.vertices;
GLushort* gIndices       = (GLushort*)((CC3VertexArrayMesh*)gPlaneNode.mesh)
.vertexIndices.vertices;
int gIndiceCount         = ((CC3VertexArrayMesh*)gPlaneNode.mesh)
.vertexIndices.vertexCount;

/*Create triangle mesh*/
btTriangleMesh* gTriangleMesh = new btTriangleMesh();
for (int i = 0; i < gIndiceCount; i+=3)
{
unsigned int index1 = gIndices[i] * 6;
unsigned int index2 = gIndices[i+1] * 6;
unsigned int index3 = gIndices[i+2] * 6;
btVector3(gVertexData[index1] , gVertexData[index1+1], gVertexData[index1+2]),
btVector3(gVertexData[index2] , gVertexData[index2+1], gVertexData[index2+2] ),
btVector3(gVertexData[index3] , gVertexData[index3+1], gVertexData[index3+2]));
}

/*Create  a rigid body using this triangle mesh*/
btBvhTriangleMeshShape *gTriMeshShape   =   new
btBvhTriangleMeshShape(gTriangleMesh,true);
gTriMeshShape->setMargin(0.1);
btDefaultMotionState *groundMotionState =
new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1)
,btVector3(0,0.0,0)));
btRigidBody::btRigidBodyConstructionInfo groundRigidBodyCI(0
,groundMotionState,gTriMeshShape,btVector3(0,0,0.0));
groundRigidBodyCI.m_restitution = 0.3;
btRigidBody *groundRigidBody = new btRigidBody(groundRigidBodyCI);


And I am adding the sphere to the scene like this

sphereNode    =   (CC3MeshNode*)[self getNodeNamed:@"Sphere"];
btCollisionShape *fallShape = new btSphereShape(1);
btDefaultMotionState *fallMotionState = new btDefaultMotionState(btTransform(
btQuaternion(sphereNode.location.x,sphereNode.location.y
,sphereNode.location.z,1), btVector3(0,0,0)));
btScalar mass = 1.0;
btVector3 fallInertia(0,0,0);
fallShape->calculateLocalInertia(mass, fallInertia);
btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass
,fallMotionState,fallShape,fallInertia);
fallRigidBodyCI.m_restitution = 1.0;
fallRigidBody = new btRigidBody(fallRigidBodyCI);
fallRigidBody->setDamping(0.3,0.8);


To view the physics body, I use bullet physics debug drawing method and this is how it is drawn

As far as I can tell, physics body looks fine, the hole is there alright.

## My problem

It is simple. My ball just slide over the hold instead of falling through it. I expect with a mass and negative Y gravity, the ball to fall through the hole.

Ball is falling of the edge of the plane alright. So gravity is there.

Any idea?

• Try cutting through the hole, making two separate meshes, like this > i.imgur.com/eeqkx7T.jpg – Gustavo Maciel Jul 9 '13 at 6:48
• @GustavoMaciel That didn't help. It was the problem with the way I exported the sphere from blender. See my answer. Anyway thanks – Krishnabhadra Jul 12 '13 at 8:41

Okay, I got this working. I changed the sphere adding code to this

CC3MeshNode *node =  (CC3MeshNode*)[self getNodeNamed:@"Sphere"];

//This was the magic step.. I don't know how
//copying helped. But it did
CC3MeshNode *sphereNode = (CC3MeshNode*)[node copyWithName:@"someOtherName"];

sphereNode.location             =   cc3v(0.0, 1.0, 10);

//Remove the original node, since we no longer need this.
[self removeChild:node];

//Now physics body.
btCollisionShape *fallShape = new btSphereShape(0.5);

//This line also different.. Compare with the code in question
btDefaultMotionState *fallMotionState
=  new btDefaultMotionState(btTransform(btQuaternion(
sphereNode.location.x, sphereNode.location.y, sphereNode.location.z,1)
,btVector3(sphereNode.location.x,sphereNode.location.y
,sphereNode.location.z)));
btScalar mass  =   1.0;
btVector3 fallInertia(0,0,0);
fallShape->calculateLocalInertia(mass, fallInertia);
btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(
mass,fallMotionState,fallShape,fallInertia);
fallRigidBodyCI.m_restitution   =   1.0;
btRigidBody* fallRigidBody =   new btRigidBody(fallRigidBodyCI);
fallRigidBody->setDamping(0.3,0.8);