0
votes

I am trying to have 2 cubes rotate differently.

to set the rotation, I do this.

GL11.glRotatef(rot[0], 1.0f, 0.0f, 0.0f);
GL11.glRotatef(rot[1], 0.0f, 1.0f, 0.0f);
GL11.glRotatef(rot[2], 0.0f, 0.0f, 1.0f);

Instead of using GL11.glLoadIdentity() to reset the rotation, inside the cube class, I do something like this.

GL11.glRotatef(rot[0] * -1.0f, 1.0f, 0.0f, 0.0f);
GL11.glRotatef(rot[1] * -1.0f, 0.0f, 1.0f, 0.0f);
GL11.glRotatef(rot[2] * -1.0f, 0.0f, 0.0f, 1.0f);

This should reset the rotation of each axis.

The array "rot" holds the x, y, and z rotations, and is updated through these 3 methods in the cube class.

public void pitch(float angle) {
    rot[0] = angle;
}

public void yaw(float angle) {
    rot[1] = angle;
}

public void roll (float angle) {
    rot[2] = angle;
}

Individually, each "GL11.glRotatef(etc,etc,etc,etc) and GL11.glRotatef(etc * -1.0f, etc,etc,etc)" works fine, but when they are all together, strange rotations happen.

I'm not sure if this is something to do with Gimbal Lock or my code, please help.

Thanks in advance.

2

2 Answers

2
votes

Your inversion code seems to be wrong. Note that matrix multiplications are not commutative:

The inverse of

R(x) * R(y) * R(z)

(which is simmilar to your three rotatef calls) is

R(-z) * R(-y) * R(-x)

. This means that you have to change the order in which you call your second glRotatef commands.

0
votes

first you should stop using the old deprecated fixed function pipeline

otherwise what you want is something like so:

GL11.pushMatrix();
GL11.glRotatef(rot[0], 1.0f, 0.0f, 0.0f);
GL11.glRotatef(rot[1], 0.0f, 1.0f, 0.0f);
GL11.glRotatef(rot[2], 0.0f, 0.0f, 1.0f);

//render

GL11.popMatrix();

pushMatrix() saves the current matrix and popMatrix() restores the last saved matrix