四元数的运用与总结

using UnityEngine;

public class Hamilton1843 : MonoBehaviour //★哈密尔顿1843
{
    public enum TurnStyle //▲旋转风格
    {
        None, //----------------△无旋转
        Euler, //---------------△欧拉角
        LookRotation, //--------△注视旋转
        AngleAxis, //-----------△角度轴
        FromToRotation, //------△旋转到
    }
    public TurnStyle turnStyle; //---------●旋转风格

    public bool isInverse = false; //------●旋转朝向取逆判定
    
    void Update ()
    {//■更新■
        InputDetection(); //模糊输入检测,进行用户测试输入判定
        
        Quaternion rotation = new Quaternion(); //四元数角度缓存
        switch (turnStyle) //根据旋转风格判定
        {
            case TurnStyle.None://----------------△无旋转
                rotation = Quaternion.identity;
                break;

            case TurnStyle.Euler://---------------△欧拉角
                rotation = Quaternion.Euler(-90, 0, 0);
                break;

            case TurnStyle.LookRotation://--------△注视旋转
                rotation = Quaternion.LookRotation(Vector3.up, Vector3.up);
                break;

            case TurnStyle.AngleAxis://-----------△角度轴
                rotation = Quaternion.AngleAxis(-90, Vector3.right);
                break;

            case TurnStyle.FromToRotation://------△同向
                rotation = Quaternion.FromToRotation(Vector3.up, Vector3.back);
                break;

        }
        //应用旋转,并顺便判定顺逆
        transform.rotation = isInverse ? Quaternion.Inverse(rotation) : rotation;
    }

    void InputDetection()
    {//■模糊输入检测■
        if (Input.inputString != "") //如果有输入
        {
            char c = Input.inputString[0]; //获取输入的首字符
            if (c >= 48 && c <= 57) //如果按下的是 0~9
            {
                int i = int.Parse(Input.inputString); //字符串转整数
                if (i >= 0 && i <= 4) //如果按下的是 0~4
                {
                    turnStyle = (TurnStyle)i; //强转输入为旋转风格
                }
            }
            else if (c == 32) //如果按下的是 空格键
            {
                isInverse = !isInverse; //切换取反
            }
        }
    }
}

 

posted @ 2018-05-17 13:09  彭然心跳  Views(557)  Comments(0Edit  收藏  举报