**0**

3 replies to this topic

###
#1
Members - Reputation: **133**

Posted 15 December 2012 - 09:07 PM

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.

###
#2
Crossbones+ - Reputation: **4355**

Posted 16 December 2012 - 05:28 AM

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.)

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, 16 December 2012 - 07:37 AM.**

###
#3
Members - Reputation: **133**

Posted 17 December 2012 - 07:08 PM

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!

###
#4
Crossbones+ - Reputation: **4355**

Posted 18 December 2012 - 09:55 AM

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.

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, 18 December 2012 - 11:23 AM.**