Mouse-based rotating quaternions (OpenGL and Java)

I am writing a game in Java using OpenGL (LWJGL binding to be specific). Each object, including the camera, has a quaternion that represents its rotation. I figured out how to apply a quaternion to the current OpenGL matrix, and everything rotates just fine. The problem I am facing is making the camera rotate with the mouse.

Right now, every frame, the game captures the amount that the mouse has moved one axis, then it applies this amount to the quaternion to rotate the camera. Here is the code that the quaternion rotates, I will publish it, since I think that where the problem is (although I am always mistaken about this kind of thing):

    public void rotateX(float amount){
        Quaternion rot = new Quaternion(1.0f, 0.0f, 0.0f, (float)Math.toRadians(amount));
        Quaternion.mul(rot, rotation, rotation);
        rotation.normalise();
    }

This method should rotate the quaternion around the X axis. "Rotation" is a quaternion representing the rotation of an entity. "amount" is the amount I want to rotate the quaternion (aka the amount that the mouse was moved). "rot" is a normalized vector along the X axis with the aw value of the sum converted to radians (I assume that the goal here is to give it an angle of, say, 10 degrees, and make it rotate the quaternion along this axis so that the angle is). Using Quaternion.mul takes the new quaternion, multiplies it by the quaternion of rotation, and then saves the result as a quaternion of rotation. I don’t know if normalization is needed, since "rot" is normal, and "rotation" should already be normalized.

rotateY rotateZ , "rot" (0.0, 1.0, 0.0 y 0.0, 0.0, 1.0 z).

, , Z. Y X. , Z, ( , ).

- -, . , Y, , , ( X). , X, ( Y). , , , , ( ) .

- ( , , ), , . , (, -, - 3D-... , - , , , ), , . , , , : "(

, ... - 3D > _ <

+5
1
Quaternion rot = new Quaternion(1.0f, 0.0f, 0.0f, (float)Math.toRadians(amount));

, .

, , . , , ; vec3 , .

.

- . , , . .

vec3 , . vec3, * , , . , [-pi/2, pi/2].

:

float radHalfAngle = ... / 2.0; //See below
float sinVal = Math.Sin(radHalfAngle);
float cosVal = Math.Cos(radHalfAngle);
float xVal = 1.0f * sinVal;
float yVal = 0.0f * sinVal;  //Here for completeness.
float zVal = 0.0f * sinVal;  //Here for completeness.
Quaternion rot = new Quaternion(xVal, yVal, zVal, cosVal);

, amount , , amount - , . - . toRadians - , .

. rot, , X . X , .

+8

All Articles