r/GraphicsProgramming • u/meith1 • Jul 02 '15
Problems in Quaternion rotation. (x-post r/opengl)
I'm using three vectors which represent every objects forward, right and up.
I'm using quaternion rotation axis-angle formula to rotate the forward vector in x, y and z axis respectively and then updating(transforming) the objects forward, right and up according to the rotation matrix.
However, even after cross checking all the steps I cannot seem to find out why my quaternion calculation is rotating the objects in a weird fashion. Sometimes it is double the required amount and sometimes one of the axis' get flipped completely. I'm logging all the values of the calculation and comparing the resultant in the final line.
Code files: transform.c Rotation function highlighted.
Few test logs:
Old Orientations in the test logs are original orientation of the object and new are after rotation of the object. As you can see in the very first where I rotate by 0, instead of old and new orientation being the same, the x and y axis, or the up and right of the object are inverted. I cannot seem to figure out why. Any help will be appreciated.
Edit: Walk through of what I'm trying to do in the code files for easier understanding.
Line 55 of transform.c : start of function which calculates the rotation quaternion and makes the rotation matrix.
Line 57 : I calculate the angles of rotation in radians.
Line 77: I calculate the rotation quaternion in x-axis using axis angle formula.
Line 79: same for y-axis
Line 82: for z axis.
Line 94: multiply the three quaternions in the following order: y * z * x using quat_mul function at line 149 of vector.c
Line 101: convert the vector to be rotated, In my case, the forward vector of the object into a quaternion.
Line 110: multiply the quternion and vector using quat_mul() funtion.
Line 113: multiply the above resultant into the conjugate of the quaternion.
Line 117: convert the above resultant into a rotation matrix using the funtion quat_tomat4 at line 160 of vector.c
Line 135: multiply forward vector with the rotation matrix to get the new rotated forward vector using the vec4_mat4mul() function at line 58 of vector.c.
Line 136 and 137: do the same for right and up.
Line 145 onwards: check my results.