Jump to content
• Advertisement

# How to get X,Y,Z angles from view/side vectors?

This topic is 3959 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

Hey, I have a side and view vector for my player's character in my game. I want to calculate the angles for the rotation on the X, Y, and Z axises for when I draw him.
//This what I mean when I say X, Y, and Z angles:

glRotatef(xrot, 1, 0, 0);
glRotatef(yrot, 0, 1, 0);
glRotatef(zrot, 0, 0, 1);

Thanks for the help.

#### Share this post

##### Share on other sites
Advertisement
use the dot product. the cosine of the angle between to vectors (beginning at the same point) equals to their dot product divided by the multiplication of their lengths. You can see it more clearly here: http://en.wikipedia.org/wiki/Dot_product#Geometric_interpretation

#### Share this post

##### Share on other sites
Quote:
 Original post by polyfragHey, I have a side and view vector for my player's character in my game. I want to calculate the angles for the rotation on the X, Y, and Z axises for when I draw him.//This what I mean when I say X, Y, and Z angles:glRotatef(xrot, 1, 0, 0);glRotatef(yrot, 0, 1, 0);glRotatef(zrot, 0, 0, 1);

That step is unnecessary for just rendering the object. The 2 vectors and the corresponding up vector (to be computed by cross-product as usual) together build the orthogonal basis by itself. Just copy them into a OpenGL compliant matrix (array), and use glMultMatrix. This way will also unburden you from some problems reconstructing the sign from values of symmetric functions (like sine and cosine are).

Ignore this suggestion if you actually need the angles for something else.

#### Share this post

##### Share on other sites
I was in the middle of writing up a huge explanation of the method I was using to get the angles and was going to do a visualization of the problem I had when I realized I had no idea how I could draw that to explain... wow, I've wasted so much time trying to get this to work. I've spent most of yesterday and today trying to get this to work.

Basically, the gist of what I was going to say was that I used

       Yangle = atan(z/x)

to get the rotation about the Y axis. But the same method doesn't work for the Z- and X-angles. When I was trying out these methods to get the X- and Z-angles (I was testing them individually), they worked fine for when I was looking directly perpendicular to the axis (and was swivelling up and down to test if the character rotated correctly), but when I rotated a bit left or right the result I got was more and more wrong....

I also tried this other method using cross products to get the X- and Z- angles:

       CPoint upvec = cross(side, view - pos);       CPoint xaxisvec(1.0f, 0.0f, 0.0f);       CPoint xrotvec = cross(upvec, xaxisvec);              float ydif = xrotvec.y;       float zdif = xrotvec.z;              float angle = 360.0f - RADTODEG( atan( ydif / zdif ) );

And it worked fine for when I used it with X- and Z-rotation, but when I combined it with Y-rotation the character ended up all over the place...

Quote:
 That step is unnecessary for just rendering the object. The 2 vectors and the corresponding up vector (to be computed by cross-product as usual) together build the orthogonal basis by itself. Just copy them into a OpenGL compliant matrix (array), and use glMultMatrix. This way will also unburden you from some problems reconstructing the sign from values of symmetric functions (like sine and cosine are).Ignore this suggestion if you actually need the angles for something else.

Thanks, but I do need to use it also for physics (I guess I should've mentioned).

Quote:
 use the dot product. the cosine of the angle between to vectors (beginning at the same point) equals to their dot product divided by the multiplication of their lengths. You can see it more clearly here: http://en.wikipedia.org/wiki/Dot_product#Geometric_interpretation

Mmm... I don't know which vectors to use to get the angles I need. I was guessing using something like the X and Z axis vectors with the side, view, and up vectors to get the angles I needed but that wouldn't work because that also includes the sideways angle for example I'm trying to get the X-angle.

This is what I need this for by the way:

I need the rotation so that when I look up and down the character's upper body will move up and down along with my view.

#### Share this post

##### Share on other sites
Quote:
 Thanks, but I do need to use it also for physics (I guess I should've mentioned).
Euler angles are usually not the preferred way to represent orientations for physics purposes. Why do you need them exactly? For compatibility with an external API?

Anyway, what you want is a matrix-to-Euler conversion. You already have the orientation matrix - as previously noted, it can be built directly from your forward and side vectors. From there, you can use any number of pre-existing matrix-to-Euler conversion functions to extract the Euler angles from the matrix.

Keep in mind that you need to get both the axis order and basis vector type (row or column) right in order for the extracted values to make sense. As for how to perform the conversion, you can Google for 'matrix to Euler', or maybe check out geometrictools.com or the CML website (the latter is linked in my signature).

Again though, I'd make sure that you actually need the angles before investing much energy in this.

#### Share this post

##### Share on other sites
Quote:
 Euler angles are usually not the preferred way to represent orientations for physics purposes. Why do you need them exactly? For compatibility with an external API.

That was the way I was doing it in my physics engine. This is because all the rotations done on the meshes in my physics engine use the same "Node" class that I use for the camera. (But this is an inefficient and an unclean way to do it.)

Quote:
 Anyway, what you want is a matrix-to-Euler conversion. You already have the orientation matrix - as previously noted, it can be built directly from your forward and side vectors. From there, you can use any number of pre-existing matrix-to-Euler conversion functions to extract the Euler angles from the matrix.Keep in mind that you need to get both the axis order and basis vector type (row or column) right in order for the extracted values to make sense. As for how to perform the conversion, you can Google for 'matrix to Euler', or maybe check out geometrictools.com or the CML website (the latter is linked in my signature).

Mmmm... thanks. I found a website that explains how to do that. I'll try it out.

Quote:
 Again though, I'd make sure that you actually need the angles before investing much energy in this.

I'm not sure. Should I use quaternions? I dunno... this method works fine for now... although when I'm facing around a certain angle (sideways) trying to swivel the camera up and down it kind of locks up and I have to do a bit of side-ward motion to get the camera to rotate up and down. I think this is gimbal lock, but I'm not sure. I heard that was a problem with using Euler angles, so I thought maybe that was it.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
21
3. 3
A4L
15
4. 4
5. 5
khawk
14
• Advertisement

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

• Total Topics
633742
• Total Posts
3013638
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!