views:

589

answers:

2

Hi, I'm trying to obtain the camera rotation on various axis in OpenGL (but using Java, LWJGL, and jME specifically). The camera object allows me to get the direction as a Vector3f, but this doesn't seem to work to get the componentised rotation; each axis appears tied to another axis. I found that toAngleAxis with the angle component with offset was a quick hack, but doesn't work properly in most situations. I'm not so good at maths unfortunately, otherwise I may have been able to work out this problem :) Again, I just need the X, Y and Z axes componentised and in radians, from 0 radians to 2 PI radians.

Can anyone help?

Cheers and thanks in advance, Chris

A: 

Obtaining the rotation angels requires just transforming the view vector given in cartesian coordinates into spherical coordinates. You can find the formulas in wikipedia.

viewvector = <x, y, z>

r = sqrt(x² + y² + z²)
phi = arctan2(y, x)
theta = arccos(z / r)

Note that you can only obtain two rotation angels form the view vector. Obtaining the third rotation angle requires knowing the projection plane x or y axis.

Daniel Brückner
Does that require the view reference point and the view plane normal?
Chris Dennett
I've found a useful picture at http://www.fsz.bme.hu/~coll/papers/applets/doc/images/world3D.cameras.Camera3D.Image01.gif, how does the projection plane X or Y axis relate to this? Is it as simple as using the Vector3f for a y-up or z-up coordinate system?Cheers
Chris Dennett
It's the view plane normal you have to transform. And the view plane x and y axis (often called up and right vector) is required to determine the roll angle - that is rotating the camera around the view direction. http://techpubs.sgi.com/library/dynaweb_docs/0620/SGI_Developer/books/Perf_PG/sgi_html/figures/04.4.plane.rotation.gif
Daniel Brückner
+2  A: 

I'd recommend reading about Euler Angles, yaw/pitch/roll, and quaternion orientation. These topics will help you understand everything involved. If I understand correctly, you're trying to construct Euler angles from a specified orientation.

See this code for some algorithms for working with Euler angles. In particular, I believe what you want is the setDirection method.

This will give you a yaw and pitch from a directional vector. Note that you only need 2 rotations, though, since "roll" would require a rotation about the directional vector (or your direction specified as a single quaternion rotation).

Reed Copsey
I can't use the setRotation method, unfortunately -- there are other systems acting upon the camera and I need to convert the held camera rotation. Also, these systems simply do things like multiplying the forward vector of the camera to make it move forward, etc.
Chris Dennett
I was pointing out setRotation() because it does the actual math you need - it gives you, from a specific vector (rotation), how to get pitch and yaw (the 2 rotations to create it). There's no need to actually "set" the rotation - this just gives you a way to discover the rotations in place.
Reed Copsey