我在OpenGL(java LWGJL)中处理一些简单的3D图形,我试图弄清楚如何将偏航,俯仰和滚动转换为我的运动的x,y和z分量向量。我知道如何通过俯仰和偏航来做到这一点(正如here所解释的那样),但我还没有找到任何解释如何将滚动整合到这个公式中。
我知道在3d空间中定义矢量需要偏航和俯仰,但在这个例子中我也需要滚动。在基本的WASD配置中,我有相对于相机的不同动作的键( A 是本地左, W 是本地前进, SPACE 是本地向上),因此滚动会影响相机的移动方式(例如,用一卷pi / 2(默认值)按 D 会使相机向右移动(在世界坐标系中),但是按 D 用一卷pi将相机移动到世界坐标上))。
这是我到目前为止的代码:
//b = back
//f = forward
//r = right
//l = left
//u = up
//d = down
private void move()
{
double dX = 0, dY = 0, dZ = 0;
if (f ^ b)
{
dZ += cos(yaw) * cos(pitch) * (b ? 1 : -1);
dX += sin(yaw) * cos(pitch) * (b ? 1 : -1);
dY += -sin(pitch) * (b ? 1 : -1);
}
if (l ^ r)
{
dZ += sin(yaw) * sin(roll) * (l ? 1 : -1);
dX += cos(yaw) * - sin(roll) * (l ? 1 : -1);
dY += cos(roll) * (l ? 1 : -1);
}
if (u ^ d) //this part is particularly screwed up
{
dZ += sin(pitch) * sin(roll) * (u ? 1 : -1);
dX += cos(roll) * (u ? 1 : -1);
dY += cos(pitch) * sin(roll) * (u ? 1 : -1);
}
motion.x = (float) dX;
motion.y = (float) dY;
motion.z = (float) dZ;
if (motion.length() != 0)
{
motion.normalise();
motion.scale(2);
}
x += motion.x;
y += motion.y;
z += motion.z;
这适用于一些旋转,但对于许多人而言,它会产生不正确的结果。
所以问题是:
如何修改我的代码,使其根据我想要的方向(按下什么键)成功计算运动矢量的x,y和z分量,计算我的偏航,音高, AND 滚动?
我使用原始trig(我试图这样做),一个涉及矩阵的解决方案,或几乎任何东西。
编辑:
请不要通过链接到关于Euler Angles的维基百科文章来回答。我已经阅读过了,而且我没有足够强的数学背景来理解如何将它应用到我的情况中。
编辑#2:
我只使用欧拉角来存储我的方向,重新定位相机。对于实际的相机操作,我使用旋转矩阵。如果需要,我可以放下欧拉角,只需使用矩阵。重要的是我可以从我的方向转换为矢量。
编辑#3:
通过将我的前向矢量乘以我的旋转矩阵找到一个解决方案,如下所述 注释:
//b = back
//f = forward
//r = right
//l = left
//u = up
//d = down
private Vector3f motion;
protected void calcMotion()
{
//1 for positive motion along the axis, -1 for negative motion, 0 for no motion
motion.x = r&&!l ? -1 : l ? 1 : 0;
motion.y = u&&!d ? 1 : d ? -1 : 0;
motion.z = f&&!b ? 1 : b ? -1 : 0;
if (motion.length() == 0)
{
return;
}
motion.normalise();
//transform.getRotation() returns a Matrix3f containing the current orientation
Matrix3f.transform(transform.getRotation(), motion, motion);
}
但this仍有问题。
答案 0 :(得分:3)
我认为你不会找到一个纯粹的触发答案。无论如何,这不是一个优雅的。
欧拉角(俯仰/偏航/滚动)不适合这项工作。 Gimble-lock将是一个问题,以及操作顺序的模糊性。
我建议将对象的当前旋转状态存储在Matrix或四元数中。仅对相对较小的增量使用欧拉角。
答案 1 :(得分:0)
您可以使用均匀变换矩阵方法,该方法广泛用于计算所有旋转后的最终位置。