Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

If I look from my screen, x-axis is towards right, y-axis is in-front of screen and z-axis is upwards.

I have a point(x1, y1, z1) and it is looking at target point(x2, y2, z2). I would like to calculate yaw, pitch and roll in degrees.

I tried calculating rotational matrix and angles but it seems that it is not giving a right result.

Can anyone please help me out.

Thanks in advance.

-swapna
Posted

First, your y axis should point towards the screen, or else you won't have a right-handed coordinate system, and that might mess up your formulas when adapting a given algorithm such as the one linked to in Solution 1.

Second, your information does not suffice to determine the roll angle: it could be any value.

Third, the algorithm, in pseudocode, looks something like this:
Moving from one point to another gives you a direction. it is definied by the vector V you get by subtracting the two points.
V := (v1, v2, v3) = (x2, y2, z2) - (x1, y1, z1) = (x2-x1, y2- y1, z2- z1)

The yaw angle is the angle this vector encloses with the projection VP of that same vector with the horizontal plane. You can get that vector simply by setting the z-coordinate to 0.
VP := (v1, v2, 0)

To calculate the yaw angle, take advantage of the fact that the scalar product of two vectors equates the product of their respective lengths and the cosine of the enclosed angle, in this case: V*VP = |V|*|VP|*cos(yaw). Based on this, you get:
yaw := acos ( V*VP/(|V|*|VP|) )

Pitch is the angle that VP encloses with the y-z plane. You can get it in the very same manner, by first projecting VP into the y-z plane ...
VPP := (0, v2, 0)

... and then calculate the angle enclosed between VPP and VP:
pitch = acos( VP*VPP / (|VP|*|VPP|) )


What is left to do for you is put this into C/C++ code, and implement functions for calculating the scalar product v1*v2 of two vectors v1 and v2, and the length |v| of a given vector v.
 
Share this answer
 
Have you seen this thread, it appears to give you all the calcs required;
http://forum.onlineconversion.com/showthread.php?t=5408[^]
 
Share this answer
 
Comments
rswapna28 3-Feb-12 4:11am    
Thanks for the link. According to the link it is clear that, it is rotated with some angle on x,y,z axis. But in my case, I dunno the angle it is rotated. What i know is only my point is looking towards some target point. So now I want to calculate my yaw, pitch and roll in degrees.

Hope I cleared my question.

Thanks Smile | :)
DaveAuld 3-Feb-12 5:06am    
You need more information to determine all the values; if you are at point a and looking at point b, you can probably only calculate the pitch.
Albert Holguin 3-Feb-12 16:53pm    
Actually, you should be able to calculate pitch and yaw based on that. Roll is the only independent measurement.
If anyone is looking for a simple code solution, just create a matrix from source and target (lookat), and use the following code (C#):

public void ExtractYawPitchRoll( out float yaw, out float pitch, out float roll )
{
    yaw   = (float) Math.Atan2( V02, V22 );
    pitch = (float) Math.Asin( -V12 );
    roll  = (float) Math.Atan2( V10, V11 );
}


I know the question is for C++ but it should be fairly easy to convert. If using C# with MonoGame, for example, the full code looks like this:

Matrix matrix = Matrix.CreateLookAt(source, target, Vector3.Up);
float yaw = (float)Math.Atan2(matrix.M13, matrix.M33);
float pitch = (float)Math.Asin(-matrix.M23);
float roll = (float)Math.Atan2(matrix.M21, matrix.M22);


Original code from:

aforge/Matrix4x4.cs at master · cureos/aforge · GitHub[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900