问题描述
大家好,
如果从屏幕上看,x轴朝右,y轴在屏幕前方,z轴是向上。
我有一个点(x1,y1,z1),它正在寻找目标点(x2,y2,z2)。我想以度数计算偏航,俯仰和滚动。
我尝试计算旋转矩阵和角度,但似乎它没有给出正确的结果。 />
任何人都可以帮帮我。
提前致谢。
-swapna
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
推荐答案
V := (v1, v2, v3) = (x2, y2, z2) - (x1, y1, z1) = (x2-x1, y2- y1, z2- z1)
偏航角是该矢量与该水平面相同矢量的投影VP所包围的角度。只需将z坐标设置为0即可获得该向量。
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)
要计算偏航角,请利用两个矢量的标量乘积等于其各自长度与封闭角的余弦的乘积这一事实: V * VP = | V | * | VP | * cos(偏航)
。基于此,你得到:
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是VP与yz平面包围的角度。您可以通过首先将 VP
投影到yz平面中来获得它...
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)
...然后计算 VPP
之间的角度和 VP
:
... and then calculate the angle enclosed between VPP
and VP
:
pitch = acos( VP*VPP / (|VP|*|VPP|) )
剩下要做的就是把它放到C / C ++代码中,并实现计算标量乘积的函数 v1 * v2
两个向量 v1
和 v2
,长度<$给定向量的c $ c> | v | v
。
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
.
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 );
}
我知道这个问题适用于C ++,但它应该很容易转换。例如,如果将C#与MonoGame一起使用,则完整代码如下所示:
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);
原始代码:
[]
这篇关于确定偏航,俯仰和滚转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!