两个 3D 矢量之间的 X 角? [英] The X angle between two 3D vectors?

查看:30
本文介绍了两个 3D 矢量之间的 X 角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个名为 A 和 B 的 3D 向量,它们都只有一个 3D 位置.我知道如何使用 atan2 函数找到沿单位圆的角度,范围为 0-360 度:

I have two 3D vectors called A and B that both only have a 3D position. I know how to find the angle along the unit circle ranging from 0-360 degrees with the atan2 function by doing:

(我的 atan2 函数没有意义,现在它应该找到 2 个向量之间的y 角"):

(my atan2 function made no sense, now it should find the "y-angle" between 2 vectors):

toDegrees(atan2(A.x-B.x,A.z-B.z))+180

但这给了我两个向量之间的 Y 角.我需要找到它们之间的 X 角.它与使用 x、y 和 z 位置值有关.不仅仅是 x 和 z,因为这给出了两个向量之间的 Y 角.我需要 X 角,我知道这听起来很模糊,但我不知道如何解释.例如,如果您向上或向下看而不是旋转 x 轴,那么您可能在 3D 空间中有一个相机.但现在我需要得到两个向量之间的上/下"角度.如果我沿 y 轴旋转该 3D 相机,则 x 轴不会改变.因此,对于 2 个向量,无论它们之间的y 角"是多少,如果 y 角发生变化,2 个向量之间的 x 角将保持不变,因为它是上/下"角,就像在相机中一样.

But that gives me the Y angle between the 2 vectors. I need to find the X angle between them. It has to do with using the x, y and z position values. Not the x and z only, because that gives the Y angle between the two vectors. I need the X angle, I know it sounds vague but I don't know how to explain. Maybe for example you have a camera in 3D space, if you look up or down than you rotate the x-axis. But now I need to get the "up/down" angle between the 2 vectors. If I rotate that 3D camera along the y-axis, the x-axis doens't change. So with the 2 vectors, no matter what the "y-angle" is between them, the x-angle between the 2 vectors wil stay the same if y-angle changes because it's the "up/down" angle, like in the camara.

请帮忙?我只需要一行数学/伪代码或解释.:)

Please help? I just need a line of math/pseudocode, or explanation. :)

推荐答案

计算高度角

好的,也许我终于理解了您在下面关于结果与 y 角度无关的评论,以及它与两个向量的关系.看来您对两个向量和这两个向量之间的夹角并不真正感兴趣,而是对差向量差向量感兴趣.与水平面形成的角度.在水平坐标系(通常用于天文学)中,该角度将被称为高度"或海拔",而不是您使用(已编辑)问题中的公式计算的方位角".高度"与相机的倾斜"密切相关,而方位角"" 与平移"有关.

Computing the altitude angle

OK, it might be I finally understood your comment below about the result being independent of the y angle, and about how it relates to the two vectors. It seems you are not really interested in two vectors and the angle between these two, but instead you're interested in the difference vector and the angle that one forms against the horizontal plane. In a horizontal coordinate system (often used in astronomy), that angle would be called "altitude" or "elevation", as opposed to the "azimuth" you compute with the formula in your (edited) question. "altitude" closely relates to the "tilt" of your camera, whereas "azimuth" relates to "panning".

我们仍然有一个二维问题.二维向量的一个坐标是差分向量的 y 坐标.另一个坐标是向量在水平面上投影后的长度,即sqrt(x*x + z*z).最终的解决方案是

We still have a 2D problem. One coordinate of the 2D vector is the y coordinate of the difference vector. The other coordinate is the length of the vector after projecting it on the horizontal plane, i.e. sqrt(x*x + z*z). The final solution would be

x = A.x - B.x
y = A.y - B.y
z = A.z - B.z
alt = toDegrees(atan2(y, sqrt(x*x + z*z)))
az = toDegrees(atan2(-x, -z))

选择顺序(A - B 而不是 B - A)使得A高于 B"产生一个正的 y因此,根据您在下面的评论,海拔高度为正.上面方位角计算中的减号应该替换您问题代码中的 + 180 ,除了现在的范围是 [-180, 180] 而不是 [0, 360].只是为了给你一个选择,选择你喜欢的那个.实际上,无论哪种方式,您都可以计算 B - A 的方位角.对这两个角度使用不同顺序的事实可能会有些混乱,因此请考虑这是否真的是您想要的,或者您是否要反转高度的符号或将方位角更改 180°.

The order (A - B as opposed to B - A) was chosen such that "A above B" yields a positive y and therefore a positive altitude, in accordance with your comment below. The minus signs in the azimuth computation above should replace the + 180 in the code from your question, except that the range now is [-180, 180] instead of your [0, 360]. Just to give you an alternative, choose whichever you prefer. In effect you compute the azimuth of B - A either way. The fact that you use a different order for these two angles might be somewhat confusing, so think about whether this really is what you want, or whether you want to reverse the sign of the altitude or change the azimuth by 180°.

作为参考,我将在下面提供我的原始答案,对于那些实际上正在寻找围绕某个固定 x 轴的旋转角度的人,原始问题建议的方式.

如果您在问题中提到的这个 x 角度确实是围绕 x 轴的旋转角度,如相机示例所示,那么您可能需要考虑这样:将 x 坐标设置为零,您将在 yz 平面中得到二维向量.您可以将其视为到所述平原的正交投影.现在您又回到了 2D 问题并且可以在那里解决它.

If this x angle you mention in your question is indeed the angle of rotation around the x axis, as the camera example suggests, then you might want to think about it this way: set the x coordinate to zero, and you will end up with 2D vectors in the y-z plane. You can think of this as an orthogonal projection onto said plain. Now you are back to a 2D problem and can tackle it there.

就我个人而言,我会简单地调用 atan2 两次,每个向量一次,然后减去生成的角度:

Personally I'd simply call atan2 twice, once for each vector, and subtract the resulting angles:

toDegrees(atan2(A.z, A.y) - atan2(B.z, B.y))

x=0 隐含在上面的公式中只是因为我只对 yz 进行操作.

The x=0 is implicit in the above formula simply because I only operate on y and z.

我还没有完全理解你的单个 atan2 调用背后的逻辑,但我必须考虑这么久的事实表明我不想维护它,至少不是没有很好的解释性评论.

I haven't fully understood the logic behind your single atan2 call yet, but the fact that I have to think about it this long indicates that I wouldn't want to maintain it, at least not without a good explanatory comment.

我希望我能正确理解你的问题,这就是你要找的东西.

I hope I understood your question correctly, and this is the thing you're looking for.

这篇关于两个 3D 矢量之间的 X 角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆