【Unity学习笔记】第十七 Quaternion 中 LookRotation、Lerp、Slerp、RotateTowards等方法辨析与验证

转载请注明出处: https://blog.csdn.net/weixin_44013533/article/details/138909256

作者:CSDN@|Ringleader|

主要参考:

  1. Unity手册 & Quaternion API
  2. Unity3D - 详解Quaternion类(二)
  3. 【Unity编程】Unity中关于四元数的API详解

Quaternion API 速览

创建旋转:

  • FromToRotation 创建一个从 fromDirection 旋转到 toDirection 的旋转。
  • LookRotation 使用指定的 forward 和 upwards 方向创建旋转。
  • AngleAxis 创建一个围绕 axis 旋转 angle 度的旋转。
  • Angle 返回两个旋转 a 和 b 之间的角度(以度为单位)。(180°以内)

操作旋转:

  • Lerp 在 a 和 b 之间插入 t,然后对结果进行标准化处理。参数 t 被限制在 [0, 1] 范围内。
  • Slerp 在四元数 a 与 b 之间按比率 t 进行球形插值。参数 t 限制在范围 [0, 1] 内。
  • RotateTowards 将旋转 from 向 to 旋转。

Transform 类还提供了一些方法可用于处理 Quaternion 旋转:Transform.Rotate & Transform.RotateAround

FromToRotation在Transform中的应用

Transform中有很多Quaternion的应用,比如获取对象的xyz轴向量在世界坐标系下的表示,就用到了FromToRotation:

   ///<para>The red axis of the transform in world space.</para>
    public Vector3 right
    {
      get => this.rotation * Vector3.right;
      set => this.rotation = Quaternion.FromToRotation(Vector3.right, value);
    }
    ///<para>The green axis of the transform in world space.</para>
    public Vector3 up
    {
      get => this.rotation * Vector3.up;
      set => this.rotation = Quaternion.FromToRotation(Vector3.up, value);
    }

    ///<para>Returns a normalized vector representing the blue axis of the transform in world space.
    public Vector3 forward
    {
      get => this.rotation * Vector3.forward;
      set => this.rotation = Quaternion.LookRotation(value);
    }

LookRotation 中upwards取Vector3.up和 transform.up的区别

public static Quaternion LookRotation (Vector3 forward, Vector3 upwards= Vector3.up);

因为LookRotation 会使对象Z轴与forward参数向量对齐,X 轴与Vector3.Cross(upwards,forward)这个叉乘结果对齐,Y 轴与 Z 和 X 的叉乘(Vector3.Cross(transform.forward,transform.right) )对齐。(注意unity左手坐标系,叉乘方向)

所以会看到当upwards取世界空间的向上和模型空间的向上是有区别的。

或者说,upwards取模型空间的向上时对象可以绕自身z轴旋转,对象状态并不固定。
在这里插入图片描述

public class LookRotationTest : MonoBehaviour
{
    public Transform obt_forward;
    public Transform obt_worldUp;
    public Transform obt_selfUp;
    
    public bool showCrossResult = false;// 验证叉乘方向用

    private void Update()
    {
        // upwards取世界空间的向上
        LookForward(obt_worldUp, obt_forward.position - obt_worldUp.position, Vector3.up);
        // upwards取模型空间的向上
        LookForward(obt_selfUp,obt_forward.position - obt_selfUp.position, obt_selfUp.up);
    }

    private void LookForward(Transform obt, Vector3 forward,Vector3 upwards)
    {
        obt.rotation = Quaternion.LookRotation(forward, upwards);
        var position = obt.position;
        Debug.DrawLine(obt_forward.position, position);

        Debug.DrawLine(position, position + obt.right * 5, Color.red);
        Debug.DrawLine(position, position + obt.up * 5, Color.green);
        Debug.DrawLine(position, position + obt.forward * 5, Color.blue);
        
        // 验证叉乘方向, 叉乘结果与LookFoward结果一致
        if (showCrossResult)
        {
            var X_cross = Vector3.Cross(upwards,forward);
            var Y_cross = Vector3.Cross(obt.forward,obt.right);
        
            Debug.DrawLine(position, position + X_cross * 10, Color.cyan);
            Debug.DrawLine(position, position + Y_cross * 10, Color.yellow);
        }
    }
}

旋转时如何保持Y轴不变,但朝向目标旋转呢?

使用Vector3.ProjectOnPlane将目标方向投影到xz平面。
在这里插入图片描述

Lookat, FromToRotation and LookRotation?

不同旋转方法案例

下面代码比较了transform.forward 、Transform.LookAt、Quaternion.FromToRotation、LookRotation、Quaternion.RotateTowards、Vector3.ProjectOnPlane等不同方法

public class CompareImmediateAndStep : MonoBehaviour
{
    public Transform turret;
    public Transform enemy;
    private string str;

    private void Update()
    {
        var targetTowards = enemy.position - turret.position;
        // 立刻跟随敌人
        if (Input.GetKey(KeyCode.Alpha1))
        {
            turret.forward = targetTowards;
            str = "使用turret.forward = targetTowards;";
        }

        // 上面本质也是使用FromToRotation方法
        if (Input.GetKey(KeyCode.Alpha2))
        {
            turret.rotation = Quaternion.FromToRotation(Vector3.forward, targetTowards);
            str = "使用turret.rotation = Quaternion.FromToRotation(Vector3.forward, targetTowards);";
        }

        if (Input.GetKey(KeyCode.Alpha3))
        {
            //当FromToRotation的fromDirection参数是forward轴时,可以用LookRotation
            turret.rotation = Quaternion.LookRotation(targetTowards);
            str = "turret.rotation = Quaternion.LookRotation(targetTowards);";
        }
        if (Input.GetKey(KeyCode.Alpha4))
        {
            turret.LookAt(enemy.transform);
            str = "turret.LookAt(enemy.transform);";
        }

        // 插值方式,加入旋转速度
        if (Input.GetKey(KeyCode.Alpha5))
        {
            var fromToRotation = Quaternion.LookRotation(targetTowards);
            turret.rotation = Quaternion.RotateTowards(turret.rotation, fromToRotation, 45 * Time.deltaTime);

            str = "使用worldUp的Quaternion.RotateTowards";
        }
        // 保持对象Y轴朝向不变,将targetTowards进行投影
        if (Input.GetKey(KeyCode.Alpha6))
        {
            var fromToRotation = Quaternion.LookRotation(Vector3.ProjectOnPlane(targetTowards,Vector3.up),Vector3.up);
            turret.rotation = Quaternion.RotateTowards(turret.rotation, fromToRotation, 45 * Time.deltaTime);
            
            str = "保持Y轴朝向不变,将targetTowards进行投影";
        }

        DrawAxis(turret);
        DrawAxis(enemy);
    }

    private void DrawAxis(Transform obt)
    {
        DrawAxis(obt, Color.red, Color.green, Color.blue, 5);
    }

    private void DrawAxis(Transform obt, Color xc, Color yc, Color zc, float length)
    {
        if (obt.gameObject.activeInHierarchy)
        {
            var position = obt.position;
            Debug.DrawLine(position, position + obt.right * length, xc);
            Debug.DrawLine(position, position + obt.up * length, yc);
            Debug.DrawLine(position, position + obt.forward * length, zc);
        }
    }
    
    private Rect rect = new Rect(100, 100, 600, 50);

    private void OnGUI()
    {
        DrawLabel(rect, str);
    }

    private static void DrawLabel(Rect rect1, String str)
    {
        var style = new GUIStyle
        {
            fontSize = 38,
            wordWrap = true
        };
        GUI.Label(rect1, str, style);
    }
}

lerp与LerpUnclamped区别

区别就是Unclam不会钳值,而且取负数时会从to→from旋转。
在这里插入图片描述

Vector3: Lerp vs LerpUnclamped

Quaternion.Slerp 、Quaternion.RotateTowards区别

RotateTowards本质也是使用了SlerpUnclamped方法,但其旋转速度恒定,不会因target变化而改变。

public static Quaternion RotateTowards(Quaternion from, Quaternion to, float maxDegreesDelta)
    {
      float num = Quaternion.Angle(from, to);
      return (double) num == 0.0 ? to : Quaternion.SlerpUnclamped(from, to, Mathf.Min(1f, maxDegreesDelta / num));
    }

这篇帖子 Use Quaternion.RotateTowards() instead of Quaternion.Slerp() 提到了:

如果旋转操作的from和to都已知,使用 Quaternion.Slerp() - 例如打开一扇门或箱子盖。
如果要以恒定角速度转向某物,则使用 Quaternion.RotateTowards() - 例如,在塔防类游戏中要转向塔的炮塔。

Lerp、Slerp比较

Quaternion的插值分析及总结
Lerp求得的是四元数在圆上的弦上的等分,而Slerp求得的是四元数载圆上的圆弧的等分
在这里插入图片描述

进行代码验证:

public class SlerpTest : MonoBehaviour
{
    public Transform obtLerp;
    public Transform obtSlerp;
    public Transform towardsObt;
    public bool towardsNotChanged;
    private Quaternion currentLookRotation,lastLookRotation;
    private Quaternion initRotationLerp, initRotationSlerp;
    
    public float speed = 0.1f;
    float total = 0.0f;

    private void Start()
    {
        lastLookRotation = Quaternion.LookRotation(towardsObt.position-obtLerp.position);
        initRotationLerp = obtLerp.rotation;
        initRotationSlerp = obtSlerp.rotation;
    }

    void Update()
    {
        CompareSlerpAndLerp();
    }

    private void CompareSlerpAndLerp()
    {
        currentLookRotation = Quaternion.LookRotation(towardsObt.position - obtLerp.position);
        towardsNotChanged = currentLookRotation == lastLookRotation;

        // 改变朝向时,重置初始位置、重置total
        if (!towardsNotChanged)
        {
            lastLookRotation = currentLookRotation;
            // 重置执行Lerp、Slerp时的初始旋转
            initRotationLerp = obtLerp.rotation;
            initRotationSlerp = obtSlerp.rotation;
            total = 0;
            return;
        }

        lastLookRotation = currentLookRotation;
        total += Time.deltaTime * speed;
        if (total >= 1.0f)
            total = 1.0f;

        obtLerp.rotation = Quaternion.Lerp(initRotationLerp, currentLookRotation, total);
        obtSlerp.rotation = Quaternion.Slerp(initRotationSlerp, currentLookRotation, total);
        DrawAxis(obtLerp);
        DrawAxis(obtSlerp, Color.cyan, Color.magenta, Color.yellow, 10);
    }

    private void DrawAxis(Transform obt)
    {
        DrawAxis(obt, Color.red, Color.green, Color.blue, 5);
    }
    private void DrawAxis(Transform obt,Color xc,Color yc,Color zc,float length)
    {
        if (obt.gameObject.activeInHierarchy)
        {
            var position = obt.position;
            Debug.DrawLine(position, position + obt.right * length, xc);
            Debug.DrawLine(position, position + obt.up * length, yc);
            Debug.DrawLine(position, position + obt.forward * length, zc);
        }
    }
    private Rect rect = new Rect(100, 100, 600, 50);
    private void OnGUI()
    {
        DrawLabel(rect,"Total:"+total);
    }

    private void DrawLabel(Rect rect1, String str)
    {
        var style = new GUIStyle
        {
            fontSize = 38,
            wordWrap = true
        };
        GUI.Label(rect1, str, style);
    }
}

如下图,RGB颜色的轴是Lerp方法,青紫黄轴是Slerp方法。可以看到Lerp相对Slerp来说先慢,中间快,最后慢。和上面的弦等分和弧等分理论一致。
请添加图片描述

Quaternion * operator

在这里插入图片描述
关于Quaternion 左乘右乘和坐标系的关系看我这篇:【Unity学习笔记】第十六 World space、Parent space和Self space及Quaternion左乘右乘辨析

总结

本文主要辨析Quaternion中Lerp、Slerp、RotateTowards等方法,并进行代码验证。至此,对Quaternion核心方法的理解已比较清晰,但其中的数学原理比如四元数、和欧拉角的关系、万向锁、逆和共轭等问题还是有待进一步学习。

相关推荐

  1. Python学习笔记三天(OpenCV简介)

    2024-05-16 06:24:23       41 阅读
  2. Python学习笔记四天(OpenCV安装)

    2024-05-16 06:24:23       38 阅读
  3. Python学习笔记九天(OpenCV轨迹栏)

    2024-05-16 06:24:23       24 阅读
  4. C++学习笔记

    2024-05-16 06:24:23       30 阅读
  5. web学习笔记

    2024-05-16 06:24:23       31 阅读
  6. Python学习笔记天(OpenCV绘画功能)

    2024-05-16 06:24:23       39 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-16 06:24:23       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-16 06:24:23       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-16 06:24:23       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-16 06:24:23       18 阅读

热门阅读

  1. ifconfig 无输出

    2024-05-16 06:24:23       12 阅读
  2. pthread_setname_np 线程设置名字 c++

    2024-05-16 06:24:23       13 阅读
  3. 百度:文心大模型日均处理Tokens文本已达2490亿

    2024-05-16 06:24:23       11 阅读
  4. PDF 生成目录和页码 点击跳转(新)

    2024-05-16 06:24:23       12 阅读
  5. Azure SQL server database 权限管理-赋予权限

    2024-05-16 06:24:23       17 阅读
  6. STM32多个外部中断可能共享同一个中断向量

    2024-05-16 06:24:23       10 阅读
  7. STM32 HAL TM1637使用

    2024-05-16 06:24:23       11 阅读
  8. Nginx-04-Docker Nginx

    2024-05-16 06:24:23       12 阅读
  9. socket介绍

    2024-05-16 06:24:23       12 阅读