Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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();
    glBegin(GL_QUADS);
        //top
        {
            glColor3f(5.0f,1.0f,5.0f); // white
            glVertex3f(posX + radius, posY + radius, posZ - radius);
            glVertex3f(posX - radius, posY + radius, posZ - radius);
            glVertex3f(posX - radius, posY + radius, posZ + radius);
            glVertex3f(posX + radius, posY + radius, posZ + radius);
        }

        //bottom
        {
            glColor3f(1.0f,1.0f,0.0f); // ?? color
            glVertex3f(posX + radius, posY - radius, posZ + radius);
            glVertex3f(posX - radius, posY - radius, posZ + radius);
            glVertex3f(posX - radius, posY - radius, posZ - radius);
            glVertex3f(posX + radius, posY - radius, posZ - radius);
        }

        //right side
        {
            glColor3f(1.0f,0.0f,1.0f); // ?? color
            glVertex3f(posX + radius, posY + radius, posZ + radius);
            glVertex3f(posX + radius, posY - radius, posZ + radius);
            glVertex3f(posX + radius, posY - radius, posZ - radius);
            glVertex3f(posX + radius, posY + radius, posZ - radius);
        }

        //left side
        {
            glColor3f(0.0f,1.0f,1.0f); // ?? color
            glVertex3f(posX - radius, posY + radius, posZ - radius);
            glVertex3f(posX - radius, posY - radius, posZ - radius);
            glVertex3f(posX - radius, posY - radius, posZ + radius);
            glVertex3f(posX - radius, posY + radius, posZ + radius);
        }

        //front side 
        {
            glColor3f(0.0f,0.0f,1.0f); // blue 
            glVertex3f(posX + radius, posY + radius, posZ + radius);
            glVertex3f(posX - radius, posY + radius, posZ + radius);
            glVertex3f(posX - radius, posY - radius, posZ + radius);
            glVertex3f(posX + radius, posY - radius, posZ + radius);
        }

        //back side
        {
            glColor3f(0.0f,1.0f,0.0f); // green
            glVertex3f(posX + radius, posY - radius, posZ - radius);
            glVertex3f(posX - radius, posY - radius, posZ - radius);
            glVertex3f(posX - radius, posY + radius, posZ - radius);
            glVertex3f(posX + radius, posY + radius, posZ - radius);
        }
    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);
}
share|improve this question
1  
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. –  Byte56 Oct 25 '12 at 14:55
    
that explains why everything was bouncing around oddly.. :/ blah –  Kenneth Bray Oct 26 '12 at 3:33
    
thanks Byte56 again –  Kenneth Bray Oct 26 '12 at 3:34

1 Answer 1

up vote 3 down vote accepted

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
share|improve this answer
1  
Use GL11.glPushMatrix(); to save the matrix state and GL11.glPopMatrix(); to restore it after rendering the cube. –  msell Oct 26 '12 at 9:12
1  
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. –  msell Oct 26 '12 at 9:20
1  
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? –  msell Oct 29 '12 at 6:13
1  
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? –  msell Oct 29 '12 at 6:45
1  
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(). –  msell Oct 29 '12 at 7:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.