# Unit Vectors to Euler Angles

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

## Recommended Posts

Hi all, My camera implementation is now using unit vectors (up vector and a look-at vector) and I could like to allow 1. Output of the current orientation of the camera, in euler angles 2. Expose a function to enter euler angles to the camera Hence for functionality 1, I have to be able to convert unit vectors into their corresponding euler angles, and for functionality 2, I have to convert euler angles to vectors. How do I achieve that? Thanks in advance!

##### Share on other sites
Taking the scalar product of two unit-length vectors gives you the cosine of the angle between them. Plug that into acos() and you've got the angle between the two vectors.

##### Share on other sites
Quote:
 Original post by superpigTaking the scalar product of two unit-length vectors gives you the cosine of the angle between them. Plug that into acos() and you've got the angle between the two vectors.
I think he wants to know how to convert between a forward-up vector pair and an Euler-angle triple, not how to find the angle between two vectors.

@The OP: To convert from forward-up to Euler angles, build a matrix from the forward and up vectors (which you have to do at some point anyway in order to set up the camera transform), and then extract the Euler angles directly from the matrix.

To set the camera orientation using an Euler-angle interface, build a matrix from the Euler angles, and then take the matrix' forward and up vectors as your forward-up pair.

You'll need to decide what order you want to use for the Euler angle conversions (e.g. xyz, xzy, etc.), and then find (or write) code to perform these conversions. Since this can be a little tricky, I recommend using existing code for this purpose. I know that the WildMagic library (at geometrictools.com) includes code for these conversions (at least for Euler-to-matrix - can't remember if it includes matrix-to-Euler, but it probably does). The CML (also linked in my signature) also includes code for this (which in turn is based on ideas presented by Shoemake in an article on Euler-angle conversions that can - or at least could at one point - be found online).

##### Share on other sites
superpig's post was highly relevant. If you want to know how much the camera is rotated about the vertical axis, you'll compare the angle between the forward vector, and the zero angle forward vector. Basically if you want angles, they have to be relative to some reference vector. Superpig illustrated how to find these angles.

jyk, is also on the right track. If you take your view matrix it defines the view coordinate space. Stored in the matrix are your up, forward, right vectors. Using basic trig on these 3 vectors you should easily be able to device a method to extract some angles relevant to your needs.

Take a look at finding polar coordinates from a vector. Finding the polar coordinates of your forward vector will give your heading and attitude angles. The last 1 can be obtained with some trig and the up vector.

##### Share on other sites
Quote:
 superpig's post was highly relevant. If you want to know how much the camera is rotated about the vertical axis, you'll compare the angle between the forward vector, and the zero angle forward vector. Basically if you want angles, they have to be relative to some reference vector. Superpig illustrated how to find these angles.
The angle between the forward vector and the 'default' forward vector won't (necessarily) tell you how much the camera is rotated about the (default) vertical axis; for example, if the camera has pitched up 45 degrees, the angle between the current and default forward vectors will be 45 degrees, despite the fact that the camera has not rotated about the vertical axis at all.

The angle between the default forward vector and the projection of the current forward axis onto the 'ground plane' will however yield the 'yaw' angle you're looking for. This however requires finding the angle between vectors that are not (necessarily) unit length, which is best done using an 'atan2' function, not acos().

Of course there are also corner cases that must be tested for. If the camera is pitched +/-90 degrees, the projection of the forward vector onto the 'ground plane' is the zero vector, and the 'yaw' angle cannot be computed.

Anyway, by the time you've jumped through all of these hoops, taken the many different possible axis orders into consideration, and worked out how to handle the special cases robustly, you've just come back to the matrix-Euler conversion code I mentioned earlier (or some variation thereof).

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633735
• Total Posts
3013597
×