Camera Rotation Problem

I am attempting to create a 3d game from scratch; and in my effort, I have come across a slight problem with my Camera.

Camera rotation is defined with [icode]Quaternion rotation[/icode]. The quaternion is rotated around the up and right vectors of the camera using delta mouse position as the angle. A rotation matrix is created and multiplied to a projection matrix for the final MVP.

The problem is that the camera is not rotating around itself. I don’t know what it’s rotating around, but the object never leaves the view. Instead, it just folds over the edge and comes back (probably a result of sin and cos in the quaternion rotation).

Here is a video demonstrating the problem:

RXWv2YC-rxs

Here is the [icode]Camera[/icode] class: http://pastebin.java-gaming.org/3cac7586f0b12
Here is the [icode]Quaternion[/icode] class: http://pastebin.java-gaming.org/cac786f6b0213
Here is the [icode]Quaternion[/icode] to [icode]Matrix4f[/icode] conversion: http://pastebin.java-gaming.org/ac78f7b620310
(Sorry for the formatting problems)

I am using this page for Quaternion reference and math: http://www.cprogramming.com/tutorial/3d/quaternions.html

The problem there is in the Quaternion to Matrix conversion. I use this formula to do that.

http://goharsha.com/images/qMatrix.png

The problem is that. In code, that becomes


public static Matrix4f toRotationMatrix(Quaternion q, Matrix4f dest)
{
    if (dest == null)
        dest = new Matrix4f();
    else
        dest.setIdentity();

    // Normalize the quaternion
    q.normalise();

    // The length of the quaternion
    float s = 2f / q.length();

    // Convert the quaternion to matrix
    dest.m00 = 1 - s * (q.y * q.y + q.z * q.z);
    dest.m10 = s * (q.x * q.y + q.w * q.z);
    dest.m20 = s * (q.x * q.z - q.w * q.y);

    dest.m01 = s * (q.x * q.y - q.w * q.z);
    dest.m11 = 1 - s * (q.x * q.x + q.z * q.z);
    dest.m21 = s * (q.y * q.z + q.w * q.x);

    dest.m02 = s * (q.x * q.z + q.w * q.y);
    dest.m12 = s * (q.y * q.z - q.w * q.x);
    dest.m22 = 1 - s * (q.x * q.x + q.y * q.y);

    return dest;
}

One more thing, I’ve just written a tutorial on this topic in my LWJGL Tutorial Series: Click here.

Hope this helps.

Thanks for the reply! I did not know that cameras were a part of those awesome tutorials of yours!

One question though: In the matrix picture you provided, cell(0, 2) (column 0, row 2) is defined as s * (qz * qz - qw * qy). From your code I assume that it’s supposed to say s * (qx * qz - qw * qy).

I implemented the code, and there is progress. Unfortunately, now I have a camera that rotates around the origin of the world (like I’m always calling lookAt(0, 0, 0)). Also, directional controls are messed up, like the vectors aren’t rotating enough.

I’ve checked through all the code on your website, and mine matches up.

If you need me to post code (or anything else), I can.

Thanks for the correction, I’ll correct it once I get back home from college. And, for the second problem, I translate the view matrix after rotation. It’s too easy to forget that, check once. Thanks for your complement by the way.

Thanks, I finally got it working ;D Now I can enjoy my triangles in peace…

Congratulations, by the way, your triangles are very amusing… Did you make a museum of triangles? :point:

The image is corrected, thanks for pointing it out.