Search code examples
openglmatrixrotationquaternions

Issues with rotation matrix


I'm currently working on the intermediates between a physics engine and a rendering engine. My physics engine takes in a series of forces and positions and returns a quaternion.

I am currently converting that quaternion into a rotation matrix using the answers to my previous question (which is working fine). My co-ordinate system is z - into the screen, y - up, and x - right.

Now after all that exposition, I have been testing via rotating a single axis at a time. I can rotate about the y axis and z axis without any issues what so ever. However, when i attempt to rotate around the z axis the system is producing a bizarre result. The rotation is fine, but as it rotates the object flattens (ie: negatively scales) in the z direction, before "flipping" and returning to full scale. It does so every 90 degrees, at a 45 degree offset to the cardinal directions.

This is my code to convert my quaternion to a rotation matrix:

    Matrix4f output = new Matrix4f();
    output.setIdentity();

    if(input.length()!=0){
    input.normalise();
    }

    float xx = input.x * input.x;
    float xy = input.x * input.y;
    float xz = input.x * input.z;
    float xw = input.x * input.w;

    float yy = input.y * input.y ;
    float yz = input.y * input.z;
    float yw = input.y * input.w;

    float zz = input.z * input.z;
    float zw = input.z * input.w;


    output.m00 = 1 -2*((yy+zz));
    output.m01 = 2*(xy+zw);
    output.m02 = 2*(xz-yw);

    output.m10 = 2*(xy-zw);
    output.m11 = 1 - (2*(xx+zz));
    output.m12 = 2*(yz+xw);

    output.m20 = 2*(xz+yw);
    output.m21 = 2*(yz+xw);
    output.m22 = 1-(2*(xx+yy));

Now I'm viewing this in real time as the object rotates, and I see nothing that shouldn't be there. Also, this passes untouched from this equation directly to opengl, so it is really beyond me why I should have this issue. Any ideas?


Solution

  • output.m21 = 2*(yz+xw); should be output.m21 = 2*(yz-xw);