0

我正在使用 C#,在我的程序中,我在 Quaternions 中收到一个 Ridigbody 的四元数,但轴与我使用的轴的方向不对应,所以我想旋转四元数。为了做到这一点,我将四元数转换为欧拉角,切换俯仰、偏航和滚动,使其与我的坐标系相对应,将其转换回四元数,然后生成一个旋转矩阵来变换位置。

但是出了点问题,当我将相同的输入从四元数转换为欧拉并返回时,我得到另一个四元数......所以其中一个函数出错了,我不知道在哪里......

Quaternion q = new Quaternion(-0.4, -0.7, -0.8, 0.5);

double yaw = 0, pitch = 0, roll = 0;
toEuler(q, ref yaw, ref pitch, ref roll);

Quaternion quat = ToQ((float)(yaw), (float)(pitch), (float)(roll));





private void toEuler(Quaternion q, ref double x, ref double y, ref double z)
        {
            double test = q.X * q.Y + q.Z * q.W;
            if (test > 0.499)                            // singularity at north pole
            {
                y = 2.0F * System.Math.Atan2(q.X, q.W);
                z = Math.PI / 2.0F;
                x = 0.0F;
                return;
            }

            if (test < -0.499)                           // singularity at south pole
            {
                y = -2.0F * System.Math.Atan2(q.X, q.W);
                z = -Math.PI / 2.0F;
                x = 0.0F;
                return;
            }

            double sqx = q.X * q.X;
            double sqy = q.Y * q.Y;
            double sqz = q.Z * q.Z;
            y = System.Math.Atan2(2.0F * q.Y * q.W - 2.0 * q.X * q.Z, 1.0F - 2.0 * sqy - 2.0 * sqz);
            z = System.Math.Asin(2.0F * test);
            x = System.Math.Atan2(2.0 * q.X * q.W - 2.0 * q.Y * q.Z, 1.0F - 2.0 * sqx - 2.0 * sqz);
        }

 public Quaternion ToQ(float yaw, float pitch, float roll)
        {
            float rollOver2 = roll * 0.5f;
            float sinRollOver2 = (float)Math.Sin((double)rollOver2);
            float cosRollOver2 = (float)Math.Cos((double)rollOver2);
            float pitchOver2 = pitch * 0.5f;
            float sinPitchOver2 = (float)Math.Sin((double)pitchOver2);
            float cosPitchOver2 = (float)Math.Cos((double)pitchOver2);
            float yawOver2 = yaw * 0.5f;
            float sinYawOver2 = (float)Math.Sin((double)yawOver2);
            float cosYawOver2 = (float)Math.Cos((double)yawOver2);
            Quaternion result = new Quaternion();
            result.W = cosYawOver2 * cosPitchOver2 * cosRollOver2 + sinYawOver2 * sinPitchOver2 * sinRollOver2;
            result.X = cosYawOver2 * sinPitchOver2 * cosRollOver2 + sinYawOver2 * cosPitchOver2 * sinRollOver2;
            result.Y = sinYawOver2 * cosPitchOver2 * cosRollOver2 - cosYawOver2 * sinPitchOver2 * sinRollOver2;
            result.Z = cosYawOver2 * cosPitchOver2 * sinRollOver2 - sinYawOver2 * sinPitchOver2 * cosRollOver2;

            return result;
        }
4

3 回答 3

2

你的四元数似乎没有标准化。代表旋转的四元数应该有范数 1。

于 2015-10-09T20:01:37.983 回答
1

为了扩展 Ben 的答案,通过除以平方和的平方根来归一化四元数。

如果您需要旋转四元数并保持其长度,我建议使用四元数进行旋转。维基百科对此有一个很棒的页面: https ://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation

于 2017-11-20T14:41:29.320 回答
0

确保在转换“to”和“from”时使用相同的 Euler Angels。

您可以从 Ken Shoemake http://tog.acm.org/resources/GraphicsGems/gemsiv/euler_angle/找到很好的参考代码

以及来自“Graphics Gems”系列的文章“欧拉角转换”Ken Shoemake

于 2014-03-07T10:08:43.623 回答