I'm currently working on a 3D scene which has to be rotated by dragging the mouse. To do this, I'm utilizing a ArcBall implementation.
The used framework, immediately transformed the resulting quaternion into a rotation matrix. Any subsequent rotations would also be converted into a matrix. These matrices in turn are multiplied.
I wanted to optimize this by simply multiplying the rotation quaternions. Now the scene seems to be spinning uncontrollably while the original version worked okay(ish).
My code for multiplying quaternions:
public static Quat4f mul(Quat4f q1, Quat4f q2) {
float x = q1.x * q2.w + q1.y * q2.z - q1.z * q2.y + q1.w * q2.x;
float y = -q1.x * q2.z + q1.y * q2.w + q1.z * q2.x + q1.w * q2.y;
float z = q1.x * q2.y - q1.y * q2.x + q1.z * q2.w + q1.w * q2.z;
float w = -q1.x * q2.x - q1.y * q2.y - q1.z * q2.z + q1.w * q2.w;
return new Quat4f(x,y,z,w);
}
The multiplication is done as (the result is normalized for good measure):
currentRotation = Quat4f.mul(newRotation, currentRotation);
An finally, the rotation is applied as:
glMultMatrixf(currentRotation.toMatrix());
Does anyone know what I'm doing wrong?