gpt4 book ai didi

c# - 使用四元数正确旋转

转载 作者:太空宇宙 更新时间:2023-11-03 10:56:56 25 4
gpt4 key购买 nike

我在旋转弹珠时遇到了一些问题。
我已经用 Matrix.CreateFromYawPitchRollMatrix.CreateRotation 试过了,但有一些问题,我认为这是由于 Gimbal lock 的影响。

因此,我尝试使用四元数,但没有任何改变。
当仅在一个轴上移动时它工作正常,但当旋转发生在两个不同的轴上时,弹珠仍然在错误的轴上旋转。

这是我的代码:

// declarations
Vector3 Position = Vector3.Zero;
Vector3 Rotation = Vector3.Zero;
Quaternion qRotation = Quaternion.Identity;

AbsoluteBoneTransforms = new Matrix[Model.Bones.Count];
Model.CopyAbsoluteBoneTransformsTo(AbsoluteBoneTransforms);

更新方法中:

Position += speed;
Rotation = speed * MathHelper.ToRadians(-1.5f);

Quaternion rot = Quaternion.CreateFromAxisAngle(Vector3.Right, Rotation.Z) *
Quaternion.CreateFromAxisAngle(Vector3.Backward, Rotation.X);

qRotation *= rot;

Draw 方法中:

effect.World = AbsoluteBoneTransforms[mesh.ParentBone.Index] * 
Matrix.CreateFromQuaternion(qRotation) * Matrix.CreateTranslation(Position);

怎么了?在多个轴上使用 Quaternion.CreateFromAxisAngle 是错误的吗?

编辑

我试过直接计算弹珠的旋转轴,而不是使用多个轴的组合:

angle += speed.Length() * angularVelocity;
qRotation = Quaternion.CreateFromAxisAngle(Vector3.Cross(speed, Vector3.Up), angle);
qRotation.Normalize();

angle 是一个跟踪当前运动的 float 。
这个解决方案似乎没有创建 Gimbal lock,但是弹珠旋转不正确,旋转速度似乎不是恒定的,但随着时间的推移变得越来越快,我不明白为什么.

如果我“连接”四元数,我将使用

获取每一帧
qRotation *= Quaternion.CreateFromAxisAngle(Vector3.Cross(speed, Vector3.Up), angle)

万向节锁定效果仍然可见。

最佳答案

这是我解决这个问题的方法:

我假设 speed 是一个表示球滚动方向的向量,其大小表示它在该方向上行进的速率。

Vector3 axis = Vector3.Cross(speed, Vector3.Up);
float angle = speed.Length();//factor by delta time if neccesary

Quaternion rotationThisFrame = Quaternion.CreateFromAxisAngle(axis, angle * (1/radiusOfBall));

然后您可以将其连接到您的 qRotation。此外,您可能需要在串联后对四元数进行归一化。

更新:这个问题/线程的正确答案是颠倒四元数连接的顺序。关于 XNA,矩阵从左到右组合,四元数从右到左组合。

关于c# - 使用四元数正确旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19171183/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com