Search code examples
javarotationanglequaternions

Quaternion "regular rotation" gives stranges values when convereted to Axis Angle


it seems that there is a problem with my custom Quaternion implementation. Download My quaternion implementation in Java.

When I lauch this snippet, in order to see quaternion angle evolutions, in a Main() method :

    Quaternion currentRotation = Quaternion.buildIdentityQuaternion();
    Quaternion constantRotation = Quaternion.buildFromAxisAngle(10, 0,1,0);

    while(true){
        float angle = currentRotation.toAxisAngle()[0];
        System.out.println(angle);
        currentRotation = constantRotation.mulInLeftOf(currentRotation);
    }

I've got the following results :

0.0
9.99997
19.999975
29.999979
39.99998
49.999977
59.999977
69.99998
79.99997
89.99997
99.99997
109.99997
119.99997
129.99997
139.99997
149.99997
159.99997
169.99997
179.99997
189.99998
199.99998
209.99998
219.99998
230.0
240.0
250.00002
260.00003
270.00003
280.00003
290.00006
300.0001
310.00012
320.00015
330.00024
340.00037
350.00082
360.0
350.00012
340.0001
330.0001
320.0001
310.00006
300.00006
290.00006

So why does the angle value first goes to 360 degrees then decreases toward 0 ? Though I've computed the angle with the formula 2*Acos(Quaternion.w) in Quaternion#toAxisAngle() method ? Maybe the implementation is not bad, so how can I compute the angle so that it returns 0, 10, ...,350, 360, 0, 10, ..., 350, 360, 0, 10 and so on ?

And finally, is there a way to compute the real angle ? SO that angle goes across values : 0,10,20,30,...,360,0,10,20... ?


However I managed to use it in a JOGL program to make a Simple 6-color cube rotate regularly, simply by computing a quaternion multiplication each time and by calling toMatrix() method on the result quaternion. What worked (do not pay attention to JOGL specific implementation details) :

  // The OpenGL functionnalities of my program
  MyJOGLEventListener implements GLEventListener {

       Quaternion myCurrentRotation = Quaternion.buildIdentityQuaternion() // w=1, x=0, y=0, z=0
       Quaternion constantRotation = Quaternion.buildFromAxisAngle(0.02f, 0,1,0) // angle = 0.02 degrees, x=0, y=1, z=0

       GLUgl2 glu = new GLUgl2();

       public void display(GLAutoDrawable drawable) {
            GL2 gl = drawable.getGL().getGL2();
            gl.glClear(/* OMITTED : color buffer and depth buffer*/);
            gl.glLoadIdentiy();

            glu.gluLookAt(/* OMITTED */);

            /// the three most relevent lines ///////////////////////////////////
            gl.glMultMatrix(myCurrentRotation.toMatrix(), 0);

            MyCubeDrawer.drawCube(gl);

            myCurrentRotation = constantRotation.mulInLeftOf(myCurrentRotation);
            //////////////////////////////////////////////////////////////////////

      }

     // OMITED : in init(GLAutoDrawable) method, I set up depth buffer
     // OMITED : the reshape(GLAutoDrawable drawable, int x, int y, int width, int height) sets the viewport and projection
 }

Regards


Solution

  • I suspect that your quaternion implementation is fine. When you pass 360 degrees with a quaternion, it inverts the axis of rotation. So at first, your quaternion represents positive rotations around the positive y axis; however, once you cross 360 degrees, it begins to represent a positive rotation around the negative y axis.

    So the angles you are getting are still correct. A rotation of zero is usually represented as [1 0 0 0]. A rotation of 180 degrees around the y axis will be represented as [0 0 1 0]. And then a rotation of 360 degrees (obviously equivalent to a rotation of 0) comes out to be [-1 0 0 0]. Rotating by another 180 degrees will give you [0 0 -1 0] This is a general property of quaternions. They're 2x redundant. If you negate all the components of the quaternion, it's equivalent to rotating the opposite direction around a flipped axis to get the same result.

    To get what you wanted, you could just check the y component of your quaternion. If it's less-than-zero, then subtract your computed rotation from 360 in order to flip the axis back to positive.