简体   繁体   English

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

[英]How to get Yaw, Pitch and Roll from a 3D vector

I have a direction vector that applied to a position gives me the point at which the camera should look.我有一个应用到 position 的方向向量给了我相机应该看的点。 How can I get from that yaw, pitch and roll in order to use glRotatef properly?为了正确使用 glRotatef,我怎样才能从偏航、俯仰和滚动中获得?

Thanks in advance提前致谢

You cannot get yaw, pitch and roll from a direction vector as the direction vector will only tell which direction to look in (yaw and pitch) 您无法从方向向量获得偏航,俯仰和滚动,因为方向向量只会告诉您要看哪个方向(偏航和俯仰)

To get the yaw and pitch you use trigonometry - I assume you have some working knowledge. 要获得偏航角和俯仰角,您可以使用三角学-我假设您已掌握一些工作知识。 Check out this wiki page for some useful diagrams to visualize the angles. 此Wiki页面上查看一些有用的图表以可视化角度。

Letting Y = yaw, P = pitch. 令Y =偏航,P =俯仰。

First to get yaw you want: 首先要获得偏航:

tan(Y) = x/(-y)

Now to get pitch: 现在开始推销:

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

To get the actual values for Y and P you'll need to use inverse tan, I've written it above using tan to make the derivation clearer. 为了获得Y和P的实际值,您需要使用反正切,我已经在上面使用tan编写了它,以使推导更清晰。

Note that the minus signs depend on how you define you angles and axes, but you should get the idea. 请注意,减号取决于您定义角度和轴的方式,但是您应该明白这一点。

You can then set roll to be 0 or whatever you like. 然后,您可以将roll设置为0或任意设置。

None of these equations are 'wrong' but all are a little clumsy. 这些方程式都不是“错误的”,但都有些笨拙。 Ryder052, you example does not account certain cases as you've commented. Ryder052,您的示例并未说明您所评论的某些情况。 Why not use atan2? 为什么不使用atan2?

Given unit (normalized) direction vector d 给定单位(规范化)方向矢量d

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

You probably don't actually want the yaw, pitch and roll. 您可能实际上并不想要偏航,俯仰和滚动。 You just need the correct transformation. 您只需要正确的转换即可。 Try using gluLookAt to build it. 尝试使用gluLookAt构建它。 Documentation . 文件资料

pheelicks's equations are wrong. 菲利克斯的方程式是错误的。 Dear future googlers, here you got what's working: 亲爱的未来的Google员工,您在这里工作了:

Assuming pitch: rotation by X axis, yaw: rotation by Y axis, roll: rotation by Z axis. 假设俯仰:X轴旋转,偏航:Y轴旋转,侧倾:Z轴旋转。 Direction vector V(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;

Well, I am not sure what any of these answers are about because I could not get any of them to work.好吧,我不确定这些答案是关于什么的,因为我无法让它们中的任何一个起作用。

I created my own solution...我创建了自己的解决方案...

// 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);

you now have the world target direction normal in my space你现在在我的空间中拥有正常的世界目标方向

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

As per direction normals all values are -1 to 1 so if you want degrees simply * 90.根据方向法线,所有值都是-1到1,所以如果你想要度数只是* 90。

Not saying this is the best solution but it is the only one I could get to work after spending hours searching online and wading through copious amounts of obtuse and nebulous crud.并不是说这是最好的解决方案,但它是我花费数小时在网上搜索并涉足大量迟钝和模糊不清的杂物后唯一可以开始工作的解决方案。

I hope you, someone, or anyone, will enjoy simply rotating the target direction normal making it relative to myspace, and find it easy and helpful:)我希望您、某人或任何人会喜欢简单地将目标方向正常旋转,使其相对于 myspace,并发现它既简单又有用:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM