【问题标题】:Quaternion lerp with different velocities for yaw/pitch/roll偏航/俯仰/滚动具有不同速度的四元数 lerp
【发布时间】:2018-10-06 21:21:34
【问题描述】:

我想在 unity3d 中的三个不同轴(偏航/俯仰/滚动)上以不同速度进行两次旋转,并尝试使用 Quaternion.LookRotation() 实现这一目标。

Vector3.lerp() 应该没问题,但在这种情况下,我需要在两个轴(X 和 Y)上相对于初始方向以不同的速度调整方向。

例如,我有一个面向目标的相机,然后目标向上和向右移动一点,现在我希望相机也慢慢向右倾斜,但要快一点到目标位置(保持它的自己的位置)。

如何让两个轴上不同速度的方向向量在Quaternion.LookRotation()中使用?

编辑: 将标题从 “X/Y 上具有不同速度的 Vector3 之间的 Lerp” 更改为 “Yaw/pitch/roll 具有不同速度的四元数 lerp” 并修改问题以匹配主题。

【问题讨论】:

  • 你试过 Quaternion.slerp 吗?
  • 是的,但是 Quaternion.Slerp() 将浮点数作为第三个参数,因此所有三个轴都以相同的“速度”进行调整。我还考虑过拆分这些轴,进行 lerp,然后将它们相加到一个新的旋转中,但要么我做的完全错误,要么效果不佳。我也用 Vector.Slerp() 进行了尝试,但在每种情况下,当我尝试在不同的轴上 lerp 时,世界 X/Y/Z 都会得到 lerp 而不是局部旋转轴。
  • 乍一看,CjLib(包括这种美妙的摇摆/扭转方法)对我来说似乎是一个完美的解决方案,但它并不是我正在寻找的。在我的情况下,它希望通过相对于观察方向调整所有三个轴来平滑地“查看”目标。我不是数学家,我从五个多星期以来一直在寻找,尝试这个和那个。另一种有效的方法(不知何故)是将目标的轴投影到观察者平面上,但这仅在世界空间中有效。 ...我真的被困住了。
  • 您是否尝试在 STERP 中使用不同的“t”lerp 参数?一种用于摆动和其他敌人扭曲。它应该完全按照您的描述进行。

标签: unity3d vector quaternions lerp


【解决方案1】:

感谢minorlogic 和CjLib,我尝试了以下方法:

public Quaternion QuaternionLerpOn3Axis(
    Quaternion rot1,
    Quaternion rot2,
    Vector3 lerpSpeed
) {
    if (rot1 != rot2) {
        float lerpSpeedPitch = lerpSpeed.x * Time.deltaTime;
        float lerpSpeedYaw = lerpSpeed.y * Time.deltaTime;
        float lerpSpeedRoll = lerpSpeed.z * Time.deltaTime;
        // Lerp up direction
        Vector3 vecUp = Vector3.Slerp(
            rot1 * Vector3.up,
            rot2 * Vector3.up,
            lerpSpeedRoll
        );
        // Get new rotation with lerped yaw/pitch
        Quaternion rotation = QuaternionUtil.Sterp(
            rot1,
            rot2,
            rot1 * Vector3.right,
            lerpSpeedYaw,
            lerpSpeedPitch,
            QuaternionUtil.SterpMode.Slerp
        );
        // Look at new direction and return rotation
        return Quaternion.LookRotation(
            rotation * rot1 * Vector3.forward,
            vecUp
        );
    } else {
        return rot1;
    }
}

要在不下载 CjLib 的情况下尝试此操作,这里是整个代码,包括用于解码摆动/扭曲的相关部分:

public Quaternion QuaternionLerpOn3Axis(
    Quaternion rot1,
    Quaternion rot2,
    Vector3 lerpSpeed
) {
    if (rot1 != rot2) {
        float lerpSpeedPitch = lerpSpeed.x * Time.deltaTime;
        float lerpSpeedYaw = lerpSpeed.y * Time.deltaTime;
        float lerpSpeedRoll = lerpSpeed.z * Time.deltaTime;
        // Lerp up direction
        Vector3 vecUp = Vector3.Slerp(
            rot1 * Vector3.up,
            rot2 * Vector3.up,
            lerpSpeedRoll
        );
        // Get difference between two rotations
        Quaternion q = rot2 * Quaternion.Inverse(rot1);
        // Decompose quaternion into two axis
        Quaternion rotYaw;
        Quaternion rotPitch;
        DecomposeSwingTwist(
            q,
            rot1 * Vector3.right,
            out rotYaw,
            out rotPitch
        );
        // Lerp yaw & pitch
        rotYaw = Quaternion.Slerp(Quaternion.identity, rotYaw, lerpSpeedYaw);
        rotPitch = Quaternion.Slerp(Quaternion.identity, rotPitch, lerpSpeedPitch);
        // Look at new direction and return rotation
        return Quaternion.LookRotation(
            rotPitch * rotYaw * rot1 * Vector3.forward,
            vecUp
        );
    } else {
        return rot1;
    }
}
public static void DecomposeSwingTwist(
    Quaternion q,
    Vector3 twistAxis,
    out Quaternion swing,
    out Quaternion twist
) {
    Vector3 r = new Vector3(q.x, q.y, q.z); // (rotation axis) * cos(angle / 2)
    float Epsilon = 1.0e-16f;

    // Singularity: rotation by 180 degree
    if (r.sqrMagnitude < Epsilon) {
        Vector3 rotatedTwistAxis = q * twistAxis;
        Vector3 swingAxis = Vector3.Cross(twistAxis, rotatedTwistAxis);

        if (swingAxis.sqrMagnitude > Epsilon) {
            float swingAngle = Vector3.Angle(twistAxis, rotatedTwistAxis);
            swing = Quaternion.AngleAxis(swingAngle, swingAxis);
        } else {
            // More singularity: rotation axis parallel to twist axis
            swing = Quaternion.identity; // no swing
        }

        // Always twist 180 degree on singularity
        twist = Quaternion.AngleAxis(180.0f, twistAxis);
        return;
    }

    // Formula & proof: 
    // http://www.euclideanspace.com/maths/geometry/rotations/for/decomposition/
    Vector3 p = Vector3.Project(r, twistAxis);
    twist = new Quaternion(p.x, p.y, p.z, q.w);
    twist = Normalize(twist);
    swing = q * Quaternion.Inverse(twist);
}
public static Quaternion Normalize(Quaternion q) {
    float magInv = 1.0f / Magnitude(q);
    return new Quaternion(magInv * q.x, magInv * q.y, magInv * q.z, magInv * q.w);
}
public static float Magnitude(Quaternion q) {
    return Mathf.Sqrt(q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w);
}

到目前为止,这是我可以在三个不同轴上实现具有不同速度的四元数 (s)lerp 并获得合理可接受的结果的唯一方法。

但在我看来,这不是一个真正的数学解决方案,如果 lerp 值低于 ~1.5f(尤其是 Z/Roll 轴),它就不能很好地工作,而且开销很大。

任何想法如何用更少/更好的代码解决这个难题?

【讨论】:

    【解决方案2】:

    ...另一种方法:

    现在我尝试将分解摆动/扭转的概念扩展到分解偏航/俯仰/滚动。

    如果目标没有翻转 180°,这可以正常工作 (?),并且它仍然需要来自真正知道如何处理四元数旋转的人的一些输入/反馈。

    public Quaternion QuaternionLerpYawPitchRoll(
        Quaternion rot1,
        Quaternion rot2,
        Vector3 lerpSpeed
    ) {
        if (rot1 != rot2) {
            float lerpSpeedPitch = lerpSpeed.x * Time.deltaTime;
            float lerpSpeedYaw = lerpSpeed.y * Time.deltaTime;
            float lerpSpeedRoll = lerpSpeed.z * Time.deltaTime;
            // Decompose quaternion into yaw/pitch/roll
            Quaternion rotYaw;
            Quaternion rotPitch;
            Quaternion rotRoll;
            DecomposeYawPitchRoll(rot1, rot2, out rotYaw, out rotPitch, out rotRoll);
            // Lerp swing & twist
            rotYaw = Quaternion.Slerp(Quaternion.identity, rotYaw, lerpSpeedYaw);
            rotPitch = Quaternion.Slerp(Quaternion.identity, rotPitch, lerpSpeedPitch);
            rotRoll = Quaternion.Slerp(Quaternion.identity, rotRoll, lerpSpeedRoll);
            // Combine yaw/pitch/roll with current rotation
            return Quaternion.LookRotation(
                rotPitch * rotYaw * rot1 * Vector3.forward,
                rotRoll * rot1 * Vector3.up
            );
        } else {
            return rot1;
        }
    }
    public static void DecomposeYawPitchRoll(
        Quaternion rot1,
        Quaternion rot2,
        out Quaternion yaw,
        out Quaternion pitch,
        out Quaternion roll
    ) {
        Vector3 pitchAxis = rot1 * Vector3.right;
        Vector3 rollAxis = rot1 * Vector3.forward;
        Vector3 yawAxis = rot1 * Vector3.up;
        // Get difference between two rotations
        Quaternion diffQ = rot2 * Quaternion.Inverse(rot1);
    
        Vector3 r = new Vector3(diffQ.x, diffQ.y, diffQ.z); // (rotation axis) * cos(angle / 2)
        float Epsilon = 1.0e-16f;
    
        // Singularity: rotation by 180 degree
        if (r.sqrMagnitude < Epsilon) {
            Vector3 rotatedPitchAxis = diffQ * pitchAxis;
            Vector3 rotatedYawAxis = Vector3.Cross(pitchAxis, rotatedPitchAxis);
            Vector3 rotatedRollAxis = diffQ * rollAxis;
    
            if (rotatedYawAxis.sqrMagnitude > Epsilon) {
                float yawAngle = Vector3.Angle(pitchAxis, rotatedPitchAxis);
                yaw = Quaternion.AngleAxis(yawAngle, rotatedYawAxis);
            } else {
                // More singularity: yaw axis parallel to pitch axis
                yaw = Quaternion.identity; // No yaw
            }
            if (rotatedRollAxis.sqrMagnitude > Epsilon) {
                float rollAngle = Vector3.Angle(yawAxis, rotatedYawAxis);
                roll = Quaternion.AngleAxis(rollAngle, rotatedRollAxis);
            } else {
                // More singularity: roll axis parallel to yaw axis
                roll = Quaternion.identity; // No roll
            }
    
            // Always twist 180 degree on singularity
            pitch = Quaternion.AngleAxis(180.0f, pitchAxis);
        } else {
            // Formula & proof: 
            // http://www.euclideanspace.com/maths/geometry/rotations/for/decomposition/
            pitch = GetProjectedRotation(diffQ, pitchAxis);
            roll = GetProjectedRotation(diffQ, rollAxis);
            yaw = diffQ * Quaternion.Inverse(pitch);
        }
    }
    public static Quaternion GetProjectedRotation(Quaternion rotation, Vector3 direction) {
        Vector3 r = new Vector3(rotation.x, rotation.y, rotation.z);
        Vector3 proj = Vector3.Project(r, direction);
        rotation = new Quaternion(proj.x, proj.y, proj.z, rotation.w);
        return Normalize(rotation);
    }
    public static Quaternion Normalize(Quaternion q) {
        float magInv = 1.0f / Magnitude(q);
        return new Quaternion(magInv * q.x, magInv * q.y, magInv * q.z, magInv * q.w);
    }
    public static float Magnitude(Quaternion q) {
        return Mathf.Sqrt(q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w);
    }
    

    【讨论】:

      【解决方案3】:

      这里是 CjLib 的作者。

      听起来您实际上不需要摆动扭曲分解。 我会说只是得到当前四元数和所需四元数的分解偏航/俯仰/行。然后根据您希望它们单独跟踪目标值的速度更新 yaw/pitch/row 值,并从该组 yaw/pitch/row 值生成更新的四元数。

      设置最大速度上限(我称之为“寻求”)的 Lerping 可能没问题,但看起来并不顺畅。我建议使用临界阻尼数值弹簧。这是我写的关于这个主题的三部分系列的shameless placement。

      【讨论】:

      • 哇,一项重大荣誉!看来您是“真正知道如何处理四元数旋转的人”。我真的很喜欢“寻找”和“数字弹出”的想法,但老实说,现在我不知道如何用这些四元数旋转来解决第一个难题......
      • 从两个旋转中分解所有三个轴,然后以不同的速度对它们进行 lerp 的想法听起来很简单,我已经在很多方面尝试过。但正如我所说,我不是数学家,直到现在我通过反复试验完成了所有这一切......并取得了一定的成功。我可以从四元数中获得轴角的唯一方法是将其提取为欧拉,这会导致轴翻转 180° ......以及其他一些奇怪的结果。那么您能否概述一下分解假人的偏航/俯仰/滚动的正确方法,或者提供更准确的提示?
      • 实际上,你根本不需要四元数来找到两个向量之间的欧拉角。此外,从听起来你只需要修改相对偏航(垂直角度)和俯仰(水平角度)。您可以通过使用当前相机局部坐标(X = 相机右侧,Y = 相机向上,Z = 相机向前)找到这些相对角度,并将所需相机朝向的方向转换为相机的当前局部坐标(我们称之为 V)。
      • 设V在XZ平面上的投影为Vxz = (V.x, 0.0f, V.z),V在YZ平面上的投影为Vyz = (0.0f, V.y, V.z)。相对俯仰角将是 Vyz 和 Z 轴之间的角度,而相对俯仰角将是 Vxz 和 Z 轴之间的角度。确保使用 Unity 的有符号角度函数并提供旋转轴(X 轴表示俯仰,Y 轴表示偏航);否则,您将始终获得正的相对俯仰和偏航,这始终将相机朝第一个八分圆旋转,即向上和向右。
      • 您的意思是Vxz 和Z 之间的相对角度将是偏航,对吧? ...我之前尝试过投影那些轴,但也许我做错了什么。据我记得,我也有翻转轴的问题。但我喜欢这个谜题,我会再试一次。非常感谢您再次将我推向正确的方向;-)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-02
      • 2015-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多