• Advertisement
Sign in to follow this  

Camera Orbit Using Quaternion

This topic is 1861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.) Edited by eppo

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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. Edited by eppo

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement