使用四元数旋转 3D 欧拉点以避免万向节锁定

发布于 2024-11-14 21:33:38 字数 850 浏览 8 评论 0原文

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

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

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

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

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

关于这一点的内容太多了,我确信一个好的解释和代码片段不仅会帮助我,而且会在未来帮助许多其他人。

所以如果可以的话请帮忙。非常感谢。

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.

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.

I found this to convert the rotations into a quaternion
http://www.euclideanspace.com/maths/geometry/rotations/conversions/eulerToQuaternion/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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆° 2024-11-21 21:33:38

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

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

其中点通过 Cw=0 进行增强,并且乘法和逆运算根据四元数规则。

具体来说,令D = C - L。然后我们让 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

但是,请注意,如果您要创建从欧拉表示的四元数,你仍然会得到万向节锁。万向节锁是欧拉表示的一个属性,四元数仅表示相同的变换。要摆脱万向节锁,您需要完全避免欧拉表示,除非我误解了您如何使用它。

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

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

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

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

Finally, we get 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文