opengl 如何从3D矢量中获取偏航、俯仰和滚动

ikfrs5lh  于 2022-11-04  发布在  其他
关注(0)|答案(5)|浏览(153)

我有一个方向矢量,它应用于一个位置,给我一个相机应该看的点。我如何从这个偏航,俯仰和滚动,以正确地使用glRotatef?
先谢谢你

rhfm7lfc

rhfm7lfc1#

这些方程没有一个是“错误的”,但都有点笨拙。Ryder052,你的例子没有考虑到你所评论的某些情况。为什么不使用atan2呢?
给定单位(归一化)方向矢量d

pitch = asin(-d.Y);
yaw = atan2(d.X, d.Z)
axkjgtzd

axkjgtzd2#

您无法从方向向量取得偏航、俯仰和滚动,因为方向向量只会告知要查看的方向(偏航和俯仰)
要得到偏航和俯仰,你可以使用三角学-我假设你有一些工作知识。查看this wiki page,获得一些有用的图表来可视化Angular 。
令Y =偏航,P =俯仰。
首先得到偏航的是你想要的:

tan(Y) = x/(-y)

现在要获得音高:

tan(P) = sqrt(x^2 + y^2)/z

为了得到Y和P的实际值,你需要使用反正切函数,我在上面用正切函数来写,以使推导更清楚。
请注意,减号取决于您如何定义Angular 和轴,但您应该了解。
然后,您可以将roll设置为0或任何您喜欢的值。

f1tvaqid

f1tvaqid3#

你可能并不需要偏航、俯仰和滚转,你只需要正确的转换。试着用gluLookAt来构建它。Documentation

n9vozmp4

n9vozmp44#

亲爱的未来的谷歌人,这里有一个行之有效的方法:
假设螺距:沿X轴旋转,偏航:沿Y轴旋转,滚动:沿Z轴旋转。方向矢量V(x,y,z)

pitch = asin(V.y / length(V));
yaw = asin( V.x / (cos(pitch)*length(V)) ); //Beware cos(pitch)==0, catch this exception!
roll = 0;
t9eec4r0

t9eec4r05#

我不知道这些答案是什么,因为我不能让它们中的任何一个工作。
我创造了自己的解决方案...

// get world target offset
// convert world target offset to world direction normal
// get my world transposed (inverted)
// rotate world direction normal to my space normal

Vector3D lWorldTargetOffset = gWorldTargetLocation - gWorldMyLocation;
Vector3D lWorldTargetDirection = lWorldTargetOffset.Normalize();
MatrixD lMyWorldTransposed = MatrixD.Transpose(MyWorldMatrix);
Vector3D lMySpaceTargetDirection = Vector3D.Rotate(lWorldTargetDirection, lMyWorldTransposed);

现在,世界目标方向法线位于我的空间中

lMySpaceTargetDirection.X = pitch
lMySpaceTargetDirection.Y = yaw.
lMySpaceTargetDirection.Z = <0 infront >0 behind.

根据方向法线,所有值都是-1到1,因此,如果您想要度数,只需 * 90。
不是说这是最好的解决方案,但它是唯一的一个我可以得到工作后,花了几个小时在网上搜索和涉水通过大量的迟钝和模糊的污垢。
我希望你,某人,或任何人,会喜欢简单地旋转目标方向法线使其相对于myspace,并发现它很容易和有帮助:)

相关问题