• unity Quaternion(四元数)与Transform


    Transform

    属性 说明
    Matrix4x4 localToWorldMatrix: 本地坐标->世界坐标的矩阵信息。
    Matrix4x4 worldToLocalMatrix: 世界坐标->本地坐标的矩阵信息。
    方法 说明
    Vector3 TransformDirection(Vector3 direction): 本地坐标->世界坐标,不受位置和缩放影响(只旋转坐标方向)。
    Vector3 TransformPoint(Vector3 position): 本地坐标->世界坐标,受位置和缩放影响。
    Vector3 TransformVector(Vector3 vector): 本地坐标->世界坐标,不受位置影响,受缩放影响。
    方法 说明
    Vector3 InverseTransformDirection(Vector3 direction): 世界坐标->本地坐标,不受位置和缩放影响(只旋转坐标方向)。
    Vector3 InverseTransformPoint(Vector3 position): 世界坐标->本地坐标,受位置和缩放影响。
    Vector3 InverseTransformVector(Vector3 vector): 世界坐标->本地坐标,不受位置影响,受缩放影响。
    transform.position=new Vector3(10,0,0);
    transform.eulerAngles=new Vector3(0,90,0);
    transform.localScale=new Vector3(0.5f,0,0.5f);
    Vector3 relative=transform.InverseTransformDirection(new Vector3(5,0,5));//转换结果见下图A
    Debug.Log(relative);//output: (-5, 0, 5)
    relative=transform.InverseTransformPoint(new Vector3(5,0,5));//转换结果见下图B
    Debug.Log(relative);//output:(-10, 0, -10)
    relative=transform.InverseTransformVector(new Vector3(5,0,5));//转换结果见下图C
    Debug.Log(relative);//output:(-10, 0, 10)
    

    图A:

    图B:

    图C:

    旋转向量

    //将向量的方向转换为四元数
    //x、y、z旋转角度都为0,就是Vector3.forward的方向
    Quaternion qua0=new Quaternion();
    Debug.Log(qua0.eulerAngles);//输出:(0.0, 0.0, 0.0)
    //同上
    Quaternion qua1=Quaternion.LookRotation(Vector3.forward);
    Debug.Log(qua1.eulerAngles);//输出:(0.0, 0.0, 0.0)
    //将向量Vector3.right的方向转换为四元数
    Quaternion qua2=Quaternion.LookRotation(Vector3.right);
    Debug.Log(qua2.eulerAngles);//输出: (0.0, 90.0, 0.0)
    
    //向量(1,0,0)绕Y轴旋转90度。
    Vector3 a=new Vector3(1,0,0);
    Vector3 b=Quaternion.AngleAxis(90, Vector3.up)*a;
    Debug.Log(b);//输出:(0.0, 0.0, -1.0)
    
    //向量(1,1,0)旋转到Vector3.right的方向(左视图)
    Vector3 e=new Vector3(1,1,0);
    Vector3 f=Quaternion.LookRotation(Vector3.right)*e;
    Debug.Log(f);//输出: (0.0, 1.0, -1.0)
    
    //将一个向量旋转到Transform组件的旋转角度
    Vector3 f=new Vector3(0,0,1);
    f=transform.rotation*f;
    

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

    现在有两个对象,如下图:



    梯子:旋转欧拉角度为x=0,y=90,z=22。
    狗:旋转欧拉角度为x=0,y=0,z=0。
    求狗爬上梯子时的旋转欧拉角?

    var ladder=new GameObject();
    ladder.transform.eulerAngles=new Vector3(0f,90f,22f);
    Debug.Log(ladder.transform.up);//ouput:(0.0,0.9,0.4)
    Debug.Log(ladder.transform.right);//ouput:(0.0,0.1,-0.9)
    
    var rotation=Quaternion.LookRotation(ladder.transform.up,ladder.transform.right);
    //dog.transform.rotation=rotation;
    Debug.Log(rotation.eulerAngles);//output:(292,0.0,0.0)
    
  • 相关阅读:
    BZOJ4152: [AMPPZ2014]The Captain
    BZOJ4025: 二分图
    BZOJ1453: [Wc]Dface双面棋盘
    BZOJ3238: [Ahoi2013]差异
    BZOJ3165: [Heoi2013]Segment
    BZOJ4556: [Tjoi2016&Heoi2016]字符串
    BZOJ2668: [cqoi2012]交换棋子
    UVa-10652 包装木板
    HDU1599-Find the mincost route
    HDU-3339 IN ACTION(Dijkstra +01背包)
  • 原文地址:https://www.cnblogs.com/kingBook/p/11423272.html
Copyright © 2020-2023  润新知