# Rotating a cube using jBullet collisions

How would one go about rotating/flipping a cube with the physics of jBullet?

Here is my Draw method for my cube object:

    public void  Draw() {
// center point posX, posY, posZ
float radius = .25f;//size / 2;

glPushMatrix();
//top
{
glColor3f(5.0f,1.0f,5.0f); // white
}

//bottom
{
glColor3f(1.0f,1.0f,0.0f); // ?? color
}

//right side
{
glColor3f(1.0f,0.0f,1.0f); // ?? color
}

//left side
{
glColor3f(0.0f,1.0f,1.0f); // ?? color
}

//front side
{
glColor3f(0.0f,0.0f,1.0f); // blue
}

//back side
{
glColor3f(0.0f,1.0f,0.0f); // green
}
glEnd();
glPopMatrix();

}


This is my update method for the cube position:

    public void Update() {
Transform trans = new Transform();
cubeRigidBody.getMotionState().getWorldTransform(trans);

posX = trans.origin.x;
posY = trans.origin.y;
posZ = trans.origin.z;

Quat4f outRot = new Quat4f();
trans.getRotation(outRot);
rotX = outRot.x;
rotY = outRot.y;
rotZ = outRot.z;
rotW = outRot.w;
}


I am assuming I need to use glrotatef, but it does not seem to work at all when I try that..

this is how I have tried to rotate the cubes:

    GL11.glRotatef(rotW, rotX, 0.0f, 0.0f);
GL11.glRotatef(rotW, 0.0f, rotY, 0.0f);
GL11.glRotatef(rotW, 0.0f, 0.0f, rotZ);


Here is how I am getting the XYZ for the cubes from jBullet:

    public void Update() {
//cubeTransform = new Transform();
cubeRigidBody.getMotionState().getWorldTransform(cubeTransform);

posX = cubeTransform.origin.x;
posY = cubeTransform.origin.y;
posZ = cubeTransform.origin.z;

processTransformMatrix(cubeTransform);
}

• Remember that when using glRotate you need to rotate first then draw, then glRotate back to neutral. OpenGL rotate, translate and scale all set OpenGL into a state so that everything after that is drawn with that rotation, translation and scale.
– House
Oct 25 '12 at 14:55
• that explains why everything was bouncing around oddly.. :/ blah
– user22020
Oct 26 '12 at 3:33

You are mixing quaternions with euler angles. You would need to do a conversion to make it work. Instead of rotations around specific axis, I recommend to use the transformation matrix from Bullet physics directly. This includes the position as well.

// These can be pre-allocated.
float[] matrix = new float[16];
Transform transform = new Transform();
FloatBuffer transformationBuffer = BufferUtils.createFloatBuffer(16);

// Get the transformation matrix from JBullet.
MotionState motionState = rigidBody.getMotionState();
motionState.getWorldTransform(transform);
transform.getOpenGLMatrix(matrix);

// Put the transformation matrix into a FloatBuffer.
transformBuffer.clear();
transformBuffer.put(transformation);
transformBuffer.flip();

GL11.glPushMatrix(); // Save the current OpenGL transformation
GL11.glMultMatrix(transformationBuffer); // Apply the object transformation
renderObject();
GL11.glPopMatrix(); // Restore the saved transformation

• Use GL11.glPushMatrix(); to save the matrix state and GL11.glPopMatrix(); to restore it after rendering the cube. Oct 26 '12 at 9:12
• The screenshot looks like some scaling problem, no idea what is causing it. If that is the case, you could also create a matrix from a quaternion to get just the rotation matrix and use that for glMultMatrix. You also might need to transpose the matrix before passing to OpenGL, but I'm not sure about that. Oct 26 '12 at 9:20
• Updated my answer to include the saving and restoring of the transformation matrix. Are the objects rotated correctly with the code above? Is the scaling the only issue you have with this? How are you creating the JBullet geometry for the cube? Oct 29 '12 at 6:13
• My suggestion is to not get the positions at all from JBullet. Instead just use the getOpenGLMatrix, which gives you both position and orientation. Then my other question is what parameters are you passing to the constructor of BoxShape? Do they match with vertices you are creating? Would everything work correctly if you just scaled the visuals by 2? Oct 29 '12 at 6:45
• The given code should be enough for generic JBullet rendering with LWJGL. For more complete example code check the demo application within JBullet, especially GLShapeDrawer.drawOpenGL() and LwjglGL.drawCube(). Oct 29 '12 at 7:04