zoukankan      html  css  js  c++  java
  • Unity的旋转-四元数,欧拉角用法简介

    当初弄不明白旋转。。居然找不到资料四元数应该用轴角相乘。。。

    通过两种旋转的配合,可以告别世界空间和本地空间矩阵转换了,大大提升效率。

    每个轴相乘即可,可以任意轴,无限乘。无万向节锁问题

    四元数旋转:

    using UnityEngine;
    using System.Collections;
    
    public class RotationTest : MonoBehaviour
    {
        public float x, y, z;
        
        
        void Start ()
        {
        
        }
        
        void Update ()
        {
            var xRot = Quaternion.AngleAxis(x, new Vector3(1,0,0));
            var yRot = Quaternion.AngleAxis(y, new Vector3(0,1,0));
            var zRot = Quaternion.AngleAxis(z, new Vector3(0,0,1));
            
            transform.rotation = xRot*yRot*zRot;
        }
    }
    View Code

    欧拉角旋转适合本地坐标旋转,按世界坐标轴旋转用四元数旋转,并且需要注意万向节锁问题

    欧拉角旋转:

    using UnityEngine;
    using System.Collections;
    
    public class RotationTest : MonoBehaviour
    {
        public float x, y, z;
        
        
        void Start ()
        {
        
        }
        
        void Update ()
        {
            transform.rotation = Quaternion.Euler(new Vector3(x,y,z));
            
            Debug.Log("xyz : "+transform.rotation.eulerAngles);
        }
    }
    View Code
  • 相关阅读:
    day20(顺时针打印矩阵)
    day18(树的子结构)
    JAVA WEB应用
    hexo 写作
    解决
    Github构建个人主页之写作
    Github构建个人主页之建站
    hive HQL数据库操作笔记02
    python scrapy爬虫笔记01
    spark笔记01
  • 原文地址:https://www.cnblogs.com/hont/p/3485832.html
Copyright © 2011-2022 走看看