# Object rotating error in Bullet Physics with OpenGL

I have got some problem in my OpenGL game. I am using bullet physics and I want to achive quite simple effect - I want one object (a sphere) to roll and hit another (box) which will fall down.

I have got almost everything but still I have got some errors. When the box is hit it is rotating almost how I would like to. Well, almost means that when ball hits it it falls and rotates however there are about 20 degrees left to the ground.

It isn't stoping on a ball because the speed is so high that it is throw away and after a while it "meets" ball

This is my code for objects (including floor) initialization:

dynamicsWorld->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->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(0,groundMotionState,groundShape,btVector3(0,0,0));
btRigidBody* groundRigidBody = new btRigidBody(groundRigidBodyCI);

btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass,fallMotionState,ballShape,fallInertia);
fallRigidBody = new btRigidBody(fallRigidBodyCI);

fallRigidBody->setLinearVelocity(btVector3(-5,0,0));

pinShape = new btBoxShape(btVector3(0.5,2,0.5));

btDefaultMotionState* fallMotionState2 = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,3,0)));
btScalar mass2 = 1;
btVector3 fallInertia2(0,0,0);
pinShape->calculateLocalInertia(mass2,fallInertia2);
btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI2(mass2,fallMotionState2,pinShape,fallInertia2);
pinRigidBody = new btRigidBody(fallRigidBodyCI2);


and this is how I am setting the box position and rotation:

btTransform trans2;
pinRigidBody->getMotionState()->getWorldTransform(trans2);
cubeX = trans2.getOrigin().getX();
cubeY = trans2.getOrigin().getY();
cubeZ = trans2.getOrigin().getZ();

rot = trans2.getRotation();
//...
glTranslatef(cubeX,cubeY,cubeZ);
glRotatef(rot.getX()*(360/3.14),1,0,0);
glRotatef(rot.getY()*(360/3.14),0,1,0);
glRotatef(rot.getZ()*(360/3.14),0,0,1);

glScalef(0.5,5.0,0.5);
glutSolidCube(1.0f);


Can someone tell me what is wrong in it? I have got no idea what else I can do.

• Screenshots would help. Jan 7, 2012 at 19:10

There is an error in the code where the rotation of the box is passed to OpenGL.

btTransform::getRotation does not return an euler angle (like used in the code with glRotatef) but a quaternion.

Instead of btTransform::getOrigin and btTransform::getRotation use btTransform::getOpenGLMatrix and pass that matrix to OpenGL using glMultMatrixf.

Here is the corrected code, assuming bullet is used with single precision floating points, so btScalar is a float

    // get the transformation of the body into an OpenGL matrix
btTransform trans2;
pinRigidBody->getMotionState()->getWorldTransform(trans2);
btScalar m[16];
trans.getOpenGLMatrix(m);
//...

// pass that matrix to OpenGL and render the cube
glPushMatrix();
glMultMatrixf((GLfloat*)m);
glScalef(0.5,5.0,0.5);
glutSolidCube(1.0f);
glPopMatrix();

• Thank you very, very much for your help and advice. Now it works just great. Jan 8, 2012 at 13:04
• @sebap123 you're very welcome. I noticed you use different values for the box height in glScalef it is 5.0, but in pinShape it is 2.0. You might want to use the same values for the correct visual results. Anyway, good luck for your game :) Jan 8, 2012 at 13:15