# atan2 and extracting angles from a camera transform

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

## Recommended Posts

Dear All, I'd like to extract the rotation angles relative to a fixed frame of reference from a camera matrix(this the method listed http://www.j3d.org/matrix_faq/matrfaq_latest.html doesnt apply). The way I thought I could do this for say the Z axis is to take a vector vec(0,1,0), apply the camera matrix to the vector, and then use atan2(vec.x,vec.y); to calculate the angle. This in fact works when the camera is only rotating around the Z axis. However when I rotate around the X axis, once vec.y goes from positive to negative, the angle jumps from 0, to pi(which on consulting the atan2 documentation I see is the standard behaviour). However I end up without the correct angle. How ought I to handle this problem? Regards, Jesse

##### Share on other sites
Silly question: to what end?

##### Share on other sites
Assuming smooth motion, you should never see a jump in atan2 from 0 to pi, only from -pi to pi. Post your code.

##### Share on other sites
Well, you can accomplish what you want using the method you outline, as long as you add a few if statements and a few subtractions.
But as erissian said, why would you want to? If you tell us what you are trying to use these angles for, we can probably suggest a better way that uses the matrices directly.

##### Share on other sites
Dear All,
Theres not much to the code, but I'm listing it below
UpdateFromCameraMatrix(Transform3D Matrix)
{
Matrix.xd=0;
Matrix.yd=0;
Matrix.zd=0; //Remove the translation component.

Vector3 North(0,1,0);

Matrix.Transform(North);//Simple Matrix-Vector Multiplication, seems to be producing the correct results

double angle_z=(atan2(North.x,(North.y)));

..////Update the control graphic and view matrix.
}

Why am I trying to do this? Well I've written a navigator, that allows the user to rotate around a fixed frame of reference, that works great. Unfortuantly one of the requirements is that the user is not always navigating via the control, other code can(and often will) be in control, creating its own camera matrix. So once the navigator regains control, its settings ought to reflect the rotations that were made though this other code that I have no control over other then the fact that its passing me its camera matrix(which in the example case appears to be correct). While the control is active, it uses its own matrix, and when its done passes it back to the other code.

##### Share on other sites
Looks like you might be mixing up the order of arguments to atan2, depending on exactly what results you want.

##### Share on other sites
What I'm hoping to get is the angle between the rotated vector and (0,1,0), in other words, the amount the planet has rotated around the Z axis. Swapping the order of the variables just breaks the part thats working(when the planet is only rotating aroud the Z axis all is well).

##### Share on other sites
Quote:
 What I'm hoping to get is the angle between the rotated vector and (0,1,0), in other words, the amount the planet has rotated around the Z axis.

That's the only important part. Don't muck about with "north" and suchlike; it'll only confuse things. Take the unit vector (1,0,0), transform it, and do atan2 (designed to give you the signed angle between an arbitrary vector and the positive X axis) on the y and x components. The result will be the amount of rotation around the Z axis.

##### Share on other sites
Hmm, well I changed the code to

Vector3 Unit(1,0,0);

Matrix.Transform(Unit);

double angle_z=((atan2(Unit.y,(Unit.x))));

And now I'm able to rotate around the Z, and X axis, but when I rotate around the Y axis, it jumps again after the 180 degree rotation. So I have the same problem except with the Y axis instead of the X.

##### Share on other sites
I may be missing something basic, but AFAIK a single atan2 call cannot calculate the rotation about all 3 axes.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 12
• 15
• 11
• 12
• ### Forum Statistics

• Total Topics
634153
• Total Posts
3015844
×