防止四元数绕某些轴旋转 [英] Prevent rotation around certain axis with Quaternion

查看:237
本文介绍了防止四元数绕某些轴旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用鼠标编程相机控件.我希望用户能够向上或向下或向左或向右查找,但不能向一侧倾斜".换句话说,我想允许Yaw和Pitch更改,但将Roll限制为0.

I'm programming camera controls with a mouse. I want the user to be able to look up and down or left and right but not be able to "tilt" to the side. Put another way, I want to allow Yaw and Pitch to change but restrict Roll to 0.

如何防止四元数绕某个轴旋转?当我只允许自己进行x轴旋转或y轴旋转时,一切正常,但是当由于某种原因同时允许两者旋转时,y旋转会受到影响并且相机会向侧面倾斜.

How can I prevent a Quaternion from rotating around a certain axis? When I only allow x-axis rotation or y-axis rotation by themselves, everything works fine, but when I allow both at the same time for some reason the y-rotation is affected and the camera tilts to the side.

推荐答案

如CNuts所述,串联多个偏航和俯仰旋转会导致侧倾旋转.但是,如果偏航和俯仰旋转严格按照乘法顺序分开,则不会发生这种情况.例如

As CNuts explained, concatenating multiple yaw and pitch rotations can result in a roll rotation. However, this does not occur if the yaw and pitch rotations are strictly separated in the multiplication order. E.g.

Yaw1 * Yaw2 * Yaw3 * Pitch1 * Pitch2

仅当在末尾添加另一个偏航旋转时,才会出现问题.但是您可以轻松地避免这种情况:如果要添加偏航旋转,请将其乘以左侧.如果要添加音高旋转,请将其乘以右侧. IE.如果当前相机旋转角度为R,则:

There will be problems only when another yaw rotation is added to the end. But you can easily prevent this: If you want to add a yaw rotation, multiply it to the left. If you want to add a pitch rotation, multiply it to the right. I.e. if the current camera rotation is R, then:

R = additionalYaw * R

R = R * additionalPitch

由于所有偏航旋转都围绕相同的轴,因此这些变换实际上是通勤的.因此,它们出现的顺序无关紧要(相同的所有其他绕同一轴的旋转也是如此).当然,您也可以切换顺序(即先音调,然后偏航).取决于您想要哪种控件.

Since all the yaw rotations are about the same axis, these transforms actually commute. So it does not matter in what order they appear (same applies to all other rotations about the same axis). Of course, you can also switch the order (i.e. pitch first, then yaw). Depending on what kind of control you want.

CNuts还描述了一种替代方法.如果您具有转换层次结构,那么您的相机转换可以有两个节点.然后,向其中一个添加偏航旋转,向另一个添加偏航旋转.这将具有与上述相同的效果,有效地分开了绕不同轴的旋转.

CNuts also described an alternative approach. If you have a transform hierarchy, you can have two nodes for your camera transform. Then, add yaw rotations to one of them and pitch rotations to the other. This will have the same effect as described above, effectively separating rotations about different axes.

这篇关于防止四元数绕某些轴旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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