在OpenGL中获取X,Y和Z轴上的弧度相机旋转?

时间:2022-09-10 18:44:53

I'm trying to obtain the camera rotation on various axis in OpenGL (but using Java, LWJGL, and jME specifically). The camera object allows me to get the direction as a Vector3f, but this doesn't seem to work to get the componentised rotation; each axis appears tied to another axis. I found that toAngleAxis with the angle component with offset was a quick hack, but doesn't work properly in most situations. I'm not so good at maths unfortunately, otherwise I may have been able to work out this problem :) Again, I just need the X, Y and Z axes componentised and in radians, from 0 radians to 2 PI radians.

我正在尝试在OpenGL中的各个轴上获取相机旋转(但具体使用Java,LWJGL和jME)。相机对象允许我将方向作为Vector3f,但这似乎不能用于获得分量旋转;每个轴显示与另一个轴相关联。我发现带有偏移的角度分量toAngleAxis是一个快速的黑客,但在大多数情况下无法正常工作。不幸的是,我不太擅长数学,否则我可能已经解决了这个问题:)再次,我只需要X,Y和Z轴分量和弧度,从0弧度到2 PI弧度。

Can anyone help?

有人可以帮忙吗?

Cheers and thanks in advance, Chris

克里斯,先生干杯谢谢

2 个解决方案

#1


Obtaining the rotation angels requires just transforming the view vector given in cartesian coordinates into spherical coordinates. You can find the formulas in wikipedia.

获得旋转角度只需要将笛卡尔坐标中给出的视图矢量转换为球面坐标。您可以在*中找到公式。

viewvector = <x, y, z>

r = sqrt(x² + y² + z²)
phi = arctan2(y, x)
theta = arccos(z / r)

Note that you can only obtain two rotation angels form the view vector. Obtaining the third rotation angle requires knowing the projection plane x or y axis.

请注意,您只能从视图矢量中获取两个旋转角度。获得第三旋转角度需要知道投影平面x或y轴。

#2


I'd recommend reading about Euler Angles, yaw/pitch/roll, and quaternion orientation. These topics will help you understand everything involved. If I understand correctly, you're trying to construct Euler angles from a specified orientation.

我建议阅读有关欧拉角,偏航/俯仰/滚转和四元数方向的内容。这些主题将帮助您了解所涉及的一切。如果我理解正确,你试图从指定的方向构造欧拉角。

See this code for some algorithms for working with Euler angles. In particular, I believe what you want is the setDirection method.

有关使用欧拉角的一些算法,请参阅此代码。特别是,我相信你想要的是setDirection方法。

This will give you a yaw and pitch from a directional vector. Note that you only need 2 rotations, though, since "roll" would require a rotation about the directional vector (or your direction specified as a single quaternion rotation).

这将为您提供方向矢量的偏航和俯仰。请注意,您只需要2次旋转,因为“滚动”需要围绕方向矢量旋转(或指定为单个四元数旋转的方向)。

#1


Obtaining the rotation angels requires just transforming the view vector given in cartesian coordinates into spherical coordinates. You can find the formulas in wikipedia.

获得旋转角度只需要将笛卡尔坐标中给出的视图矢量转换为球面坐标。您可以在*中找到公式。

viewvector = <x, y, z>

r = sqrt(x² + y² + z²)
phi = arctan2(y, x)
theta = arccos(z / r)

Note that you can only obtain two rotation angels form the view vector. Obtaining the third rotation angle requires knowing the projection plane x or y axis.

请注意,您只能从视图矢量中获取两个旋转角度。获得第三旋转角度需要知道投影平面x或y轴。

#2


I'd recommend reading about Euler Angles, yaw/pitch/roll, and quaternion orientation. These topics will help you understand everything involved. If I understand correctly, you're trying to construct Euler angles from a specified orientation.

我建议阅读有关欧拉角,偏航/俯仰/滚转和四元数方向的内容。这些主题将帮助您了解所涉及的一切。如果我理解正确,你试图从指定的方向构造欧拉角。

See this code for some algorithms for working with Euler angles. In particular, I believe what you want is the setDirection method.

有关使用欧拉角的一些算法,请参阅此代码。特别是,我相信你想要的是setDirection方法。

This will give you a yaw and pitch from a directional vector. Note that you only need 2 rotations, though, since "roll" would require a rotation about the directional vector (or your direction specified as a single quaternion rotation).

这将为您提供方向矢量的偏航和俯仰。请注意,您只需要2次旋转,因为“滚动”需要围绕方向矢量旋转(或指定为单个四元数旋转的方向)。