使用四元数旋转 3D 欧拉点以避免万向节锁定 [英] Rotate 3D Euler point using Quaternions to avoid gimbal lock

查看:45
本文介绍了使用四元数旋转 3D 欧拉点以避免万向节锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我已经做了很多谷歌搜索并检查了其他关于此的 stackoverflow 帖子,但无法获得有效回复或工作代码片段.数学不是我的强项.

Firstly, I have done much googling and checking other stackoverflow posts about this, but cannot get a working reply or a snippet of working code. Maths is not my strength.

我需要一个例程来获取相机点(CX、CY、CZ)并将其围绕观察点(LX、LY、LZ)旋转三个旋转角度(RX、RY、RZ).在某些我需要避免的情况下,使用欧拉旋转会导致万向节锁定.所以我听说了使用四元数.

I need to have a routine that takes a camera point (CX,CY,CZ) and rotate it about a lookat point (LX,LY,LZ) by three rotation angles (RX,RY,RZ). Using euler rotations leads to gimbal lock in some cases which I need to avoid. So I heard about using quaternions.

我发现这可以将旋转转换为四元数http://www.euclideanspace.com/maths/geometry/rotations/conversions/eulerToQuaternion/index.htm

I found this to convert the rotations into a quaternion http://www.euclideanspace.com/maths/geometry/rotations/conversions/eulerToQuaternion/index.htm

这可以从四元数转换回欧拉 XYZ 旋转http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/index.htm

and this to convert from a quaternion back to euler XYZ rotations http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/index.htm

它们似乎工作正常,但我需要知道如何使用四元数围绕 LX、LY、LZ 旋转 CX、CY、CZ,然后返回新的 CX、CY、CZ,而不会出现万向节锁定问题.

They seem to work fine, but I need to know how to use the quaternion to rotate the CX,CY,CZ around LX,LY,LZ and then return the new CX,CY,CZ without issues of gimbal lock.

关于这方面的内容太多了,我相信一个好的解释和代码片段不仅对我有帮助,而且对未来的许多人都有帮助.

There is so much out there about this, that I am sure a good explanation and snippet of code will help not only me but many others in the future.

所以,如果可以,请帮助.非常感谢.

So please help if you can. Many thanks.

推荐答案

简短的回答,如果您的四元数是 Q 并且新的相机点是 C':

The short answer, if your quaternion is Q and the new camera point is C':

C' = Q*(C-L)*Q^-1 + L

其中的点增加了 Cw=0 和乘法和逆是根据四元数规则.

where the points are augmented with Cw=0 and multiplication and inverse are according to quaternion rules.

具体来说,让 D = C - L.然后我们让 F = Q*D:

Specifically, let D = C - L. Then we let F = Q*D:

Fw = Qw*0  - Qx*Dx - Qy*Dy - Qz*Dz
Fx = Qw*Dx + Qx*0  + Qy*Dz - Qz*Dy
Fy = Qw*Dy - Qx*Dz + Qy*0  + Qz*Dx
Fz = Qw*Dz + Qx*Dy - Qy*Dx + Qz*0

最后,我们得到C' = F*Q^-1 + L:

Cw' = 0
Cx' = Fw*Qx - Fx*Qw + Fy*Qz - Fz*Qy + Lx
Cy' = Fw*Qy - Fx*Qz - Fy*Qw + Fz*Qx + Ly
Cz' = Fw*Qz + Fx*Qy - Fy*Qx - Fz*Qw + Lz

但是,请注意,如果您从欧拉表示创建四元数,您最终仍会遇到万向节锁定.万向节锁是欧拉表示的一个属性,四元数只是表示相同的变换.要摆脱万向节锁定,您需要完全避免使用欧拉表示,除非我误解了您的使用方式.

However, be aware that if you're creating the quaternion from an Euler representation, you'll still end up with gimbal lock. The gimbal lock is a property of the Euler representation and the quaternion will just represent the same transformation. To get rid of gimbal lock, you'll need to avoid the Euler representation altogether, unless I misunderstand how you're using it.

这篇关于使用四元数旋转 3D 欧拉点以避免万向节锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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