如何计算3D点的偏航角,俯仰角和侧倾角? [英] How do I calculate the yaw, pitch, and roll of a point in 3D?

查看:1225
本文介绍了如何计算3D点的偏航角,俯仰角和侧倾角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定3D空间中的一个点,将一条线转换为指向该对象所需的三个角度(例如欧拉角)是什么?

想象一下我在3D中有一条线(或一条盒子),并且想要转换其航向,俯仰和倾斜以从原点指向3D点,我将为那些角度使用什么值?

我无法计算出指向诸如(1,2,3)之类的位置的角度的数学方法.

解决方案

注意:我将使用由 Fletcher Dunn的用于图形和游戏开发的3D数学入门.. /p>

首先,请注意,在2D坐标系中,只需一个角度+大小即可指向" 2D中的任何点.

类似地,在3D坐标系中,您只需要两个角度 +大小即可指向"任何一个指向3D.最后一个角度(倾斜"或横滚")不影响3D中点的位置.而是旋转"指向它的箭头.如果对象是360度对称的,则根本不会看到旋转影响对象.如果物体不对称(例如飞机),则会对物体产生影响(例如,一侧向地面倾斜,另一侧向天空倾斜).

因此,最初的问题实际上变成了如何找到指向3D空间中任何点的航向角,俯仰角和大小?"

您可以使用三角函数轻松解决这一问题.假设我们有一个点(1,2,3),我们正在尝试计算航向,俯仰,幅度.

对于下面的示例,让我们使用此图,其中左轴为X,上轴为Y,右轴为Z.点(1,2,3),则由蓝色球体表示.

1.找到大小

首先,让我们找到最简单的值,即幅度.对我们来说幸运的是,无论我们处于多少维度,都可以轻松找到任意两点之间的大小(长度),只需使用勾股定​​理.由于我们处于3D状态,并且我们正在计算从原点到我们的点的距离,因此我们的距离公式变为:

magnitude = sqrt(x*x + y*y + z*z)

使用我们的实际值:

magnitude = sqrt(1*1 + 2*2 + 3*3)
          = 3.7416573868

因此,我们的幅度(或长度)为〜3.741.

2.找到标题

接下来,要找到标题,请注意,我们只关心绕XZ平面的旋转,而根本不关心Y轴.如果我们要将3D空间展平"为2D,则会很容易找到标题.

我们可以绘制一个与X轴成90度角的三角形(红色三角形),然后计算该角度.回想一下三角函数tan(angle) = opposite / adjacent,求解angle,我们得到angle = arctan(opposite / adjacent).

在这种情况下,相邻"是已知数量(redAdjacent = x = 1),而相反"也是已知数量(redOpposite = z = 3).但是,我们希望使用 atan2 而不是使用arctan来求解方程. x和y的所有不同情况对我们来说都是如此.

所以我们有:

heading = atan2(redOpposite, redAdjacent)

使用我们的实际值:

heading = atan2(3, 1)
        = 1.249045772398

因此我们的标题1.249 rad,即〜72°.

3.找到音高

最后,我们需要找到音高.与我们对标题进行的操作类似,我们可以沿包含以下三个点的平面将3D空间展平为2D:(A)原点(0,0,0),(B)我们的点(1,2,3)和(C)我们的点,因为它会投影到XZ平面(1,0,3)上(例如,通过将Y值设置为0).

如果我们在所有这三个点之间绘制一个三角形,您会注意到它们再次形成了直角三角形(绿色三角形).我们可以再次使用arctan2来简单地计算角度.

我们已经在第1步中计算了绿色斜边(即矢量的大小):

greenHypotenuse = sqrt(x*x + y*y + z*z)
                = 3.7416573868

我们还知道绿色三角形的反面与y值相同:

greenOpposite = y
              = 2

使用勾股定理,我们可以找到相邻角的长度:

greenOpposite^2 + greenAdjacent^2 = greenHypotenuse^2
y*y + greenAdjacent^2 = x*x + y*y + z*z
greenAdjacent^2 = x*x + z*z
greenAdjacent = sqrt(x*x + z*z)

请注意,计算绿色三角形的相邻长度的另一种方法是注意redHypotenuse == greenAdjacent,我们可以使用以下方法找到redHypotenuse:

redHypotenuse^2 = redAdjacent^2 + redOpposite^2
                = x*x + z*z
redHypotenuse = sqrt(x*x + z*z)

插入实际值,我们得到:

greenAdjacent = sqrt(1*1 + 3*3)
              = 3.1622776602

因此,现在我们知道绿色三角形的相邻长度和相反长度,我们可以再次使用arctan2:

pitch = atan2(greenOpposite, greenAdjacent)
      = atan2(2, 3.1622776602)
      = 0.563942641356

所以我们的间距0.5634弧度,或大约32°.

结论

如果要从原点画一条直线,长度为3.741,标题为1.249 rad,螺距为0.564 rad,它将从(0,0,0)延伸到(1,2,3).

Given a point in 3D space, what are the three angles (e.g. Euler angles) needed to transform a line to point to that object?

Imagine I have a line (or a box) in 3D and I want to transform its heading, pitch, and bank to point to the 3D point from the origin, what values would I use for those angles?

I can't figure out the math to calculate the angles to point to a location such as (1,2,3).

解决方案

Note: Instead of "yaw, pitch, roll", I'm going to use the conventions "heading, pitch, bank" as defined by 3D Math Primer for Graphics and Game Development by Fletcher Dunn.

Firstly, notice that in a 2D coordinate system, you only need a single angle + magnitude to "point" to any point in 2D.

Similarly, in a 3D coordinate system, you only need two angles + magnitude to "point" to any point in 3D. The last angle ("bank" or "roll") does not affect the location of a point in 3D. Instead it "spins" the arrow that would point to it. If the object is 360 degrees symmetrical, you won't see spin affecting the object at all. If the object is not symmetrical (e.g. an airplane) it will affect the object (e.g. tilting one wing towards the ground and the other towards the sky).

So the original question actually becomes, "how do I find the heading angle, pitch angle, and magnitude to "point" to any point in 3D space?"

You can easily figure this out using trigonometry functions. Imagine we have the point (1,2,3) and we're trying to calculate the heading, pitch, magnitude.

For the following example, let's use this diagram, where the left axis is X, up is Y, and right is Z. The point (1,2,3), then is represented by the blue sphere.

1. Find the magnitude

First, let's find the easiest value, the magnitude. Luckily for us, the magnitude (length) between any two points is easy to find no matter how many dimensions we are in, simply by using the Pythagorean theorem. Since we are in 3D and we're calculating the distance from the origin to our point, our distance formula becomes:

magnitude = sqrt(x*x + y*y + z*z)

Plugging in our actual values:

magnitude = sqrt(1*1 + 2*2 + 3*3)
          = 3.7416573868

So our magnitude (or length) is ~3.741.

2. Find the heading

Next, to find the heading, notice that we just care about rotation about the XZ plane, and we don't care about the Y-axis at all. If we were to "flatten" the 3D space into 2D, it becomes trivial to find the heading.

We can draw a triangle that forms a 90 degree angle with the X-axis (red triangle) and then calculate that angle. Recall from trigonometry tan(angle) = opposite / adjacent, and solving for angle, we get angle = arctan(opposite / adjacent).

In this case "adjacent" is a known quantity (redAdjacent = x = 1), and "opposite" is known too (redOpposite = z = 3). Instead of using arctan to solve the equation though, we want to use atan2 since it'll handle all the different cases of x and y for us.

So we have:

heading = atan2(redOpposite, redAdjacent)

Plugging in our actual values:

heading = atan2(3, 1)
        = 1.249045772398

so our heading is 1.249 rad, or ~72°.

3. Find the pitch

Finally we need to find the pitch. Similarly to what we did with the heading, we can flatten the the 3D space into 2D along the plane that contains these three points: (A) the origin (0,0,0), (B) our point (1,2,3), and (C) our point as it would project onto the XZ plane (1,0,3) (e.g. by setting 0 for the Y-value).

If we draw a triangle between all 3 of these points, you will notice that they form a right-triangle again (green triangle). We can simply calculate the angle using arctan2 again.

We already calculated the green hypotenuse in step 1 (i.e. the magnitude of our vector):

greenHypotenuse = sqrt(x*x + y*y + z*z)
                = 3.7416573868

We also know the opposite of the green triangle is the same as the y-value:

greenOpposite = y
              = 2

Using the pythagorean theorem, we can find the length of the adjacent angle:

greenOpposite^2 + greenAdjacent^2 = greenHypotenuse^2
y*y + greenAdjacent^2 = x*x + y*y + z*z
greenAdjacent^2 = x*x + z*z
greenAdjacent = sqrt(x*x + z*z)

Notice that another way to calculate the adjacent length of the green triangle is to notice that redHypotenuse == greenAdjacent, and we could find redHypotenuse using:

redHypotenuse^2 = redAdjacent^2 + redOpposite^2
                = x*x + z*z
redHypotenuse = sqrt(x*x + z*z)

Plugging in actual values, we get:

greenAdjacent = sqrt(1*1 + 3*3)
              = 3.1622776602

So now that we know the adjacent and opposite lengths of the green triangle, we can use arctan2 again:

pitch = atan2(greenOpposite, greenAdjacent)
      = atan2(2, 3.1622776602)
      = 0.563942641356

So our pitch is 0.5634 radians, or about 32°.

Conclusion

If you were to draw a line from the origin, with length 3.741, heading 1.249 rad, and pitch 0.564 rad, it would extend from (0,0,0) to (1,2,3).

这篇关于如何计算3D点的偏航角,俯仰角和侧倾角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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