Camera Orbit Using Quaternion

Started by
2 comments, last by eppo 11 years, 4 months ago
I'm trying to build a camera class that will allow me to orbit a sphere using a quaternion. Can someone help get me going in the right direction as far as how to use a quaternion, manipulate it with input, and then apply it to the camera? I've gotten the camera to orbit but I've come across the infamous gimbal lock problem and quaternions I hear are the way to go. I am using the libGDX game framework in case you want to take a look at their PerspectiveCamera, and Quaternion classes that I'm using.
Advertisement
I've looked at the libGDX documentation and its Matrix4x4 class seems a lot more developed than its Quaternion class, so you might want to stick to using a matrix as the camera's orientation/transformation instead.

First create the Matrix4(), the constructor will automatically set it to an identity matrix. You can use methods like setFromEulerAngles() and setToLookAt() to supply an initial orientation.

Secondly, get the camera to render correctly using the matrix. You can set the view property of a Camera object to the inverse of its orientation and a projection matrix can be created with Matrix4.setToProjection().

To 'extract' the yaw- and pitch-axes (as mentioned in the earlier post), you can either fetch them directly from the matrix' fields, or use Matrix.mulVec() to transform the cardinal y- and x-axes: yawAxis = Matrix4.mulVec(matrix, Vector(0, 1, 0)) / pitchAxis = Matrix4.mulVec(matrix, Vector(1, 0, 0)).

Use Matrix4.setToRotation() to create the two rotations to add to the current orientation: Matrix4 orientationNew = orientation.mul(yaw).mul(pitch).

(A second method would be to transform the 2D 'cross product' of the joystick coordinates from camera- into world-space and use that as a rotational axis.)
Could you expand upon your second method of transform the 2D "cross product" of the joystick coordinates? I am trying to convert my joystick to an axis that I can rotate around. I've gotten the rotation working (sort of) with a matrix, but as soon as I change the axis to rotate by with the joystick. The rotations get all messed up and start rotating on the wrong axes. Thanks a lot for your help!
The idea is to rotate around an axis orthogonal to the joystick vector. In a left-handed coordinate system you construct it by taking the joy / z-axis cross product:

joyAxis = normalize(cross(Vector3(joystick.x, joystick.y, 0.f), Vector3(0.f, 0.f, 1.f))) = normalize(Vector3(joystick.y, -joystick.x, 0.f)).

Next, you can either transform this vector from the camera's local space into world space. joyAxisWorld = mul(joyAxis, cameraXfrm) and then rotate around this axis by the joystick vector's magnitude:

orientation *= MatrixRotationAxis(joyAxisWorld, Vector2Length(joystick))

or you can pre-multiply the offset:

orientation = MatrixRotationAxis(joyAxis, Vector2Length(joystick)) * orientation.

This topic is closed to new replies.

Advertisement