美文网首页
旋转矩阵公式推导

旋转矩阵公式推导

作者: 醉酒青牛_fa4e | 来源:发表于2019-04-14 13:45 被阅读0次
    image.png image.png

    image.png

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class VertexAnimalMatrix : MonoBehaviour {

    float tmpFloat;
    
    Mesh myMesh;
    
    float timer;
    
    Vector3[] myList;
    void Start () {
        myMesh = transform.GetComponent<MeshFilter>().mesh;
        //for(int i=0;i< myMesh.vertices.Length; i++)
        //{
        //    myList[i] = myMesh.vertices[i];
        //}
        
    
    }
    
    void Update () {
        myList = myMesh.vertices;
        tmpFloat = Input.GetAxis("Horizontal");
        for (int i = 0; i < myMesh.vertices.Length; i++)
        {
            //这里旋转的层数不多是因为顶点少
            myList[i]= DoTwistX(tmpFloat* myList[i].x*0.1f, myList[i]);
        }
        myMesh.vertices = myList;
        //myMesh.SetVertices(DoTwistX(tmpFloat, gameObject.transform.position))
    
    }
    /// <summary>
    /// 根据矩阵,围绕X轴旋转
    /// </summary>
    /// <param name="angle">要旋转的角度</param>
    /// <param name="pos">obj的位置</param>
    /// <returns></returns>
    public Vector3 DoTwistX(float angle,Vector3 pos)
    {
        float cos = Mathf.Cos(angle);
    
        float sin = Mathf.Sin(angle);
    
        Vector3 result = pos;
    
        result.y = pos.y * cos + pos.z * sin;
    
        result.z = pos.y * (-sin) + pos.z * cos;
        return result;
    }
    /// <summary>
    /// 根据矩阵,围绕Y轴旋转
    /// </summary>
    /// <param name="angle">要旋转的角度</param>
    /// <param name="pos">要旋转的obj的位置</param>
    /// <returns></returns>
    public Vector3 DoTwistY(float angle,Vector3 pos)
    {
        float cos = Mathf.Cos(angle);
        float sin = Mathf.Sin(angle);
    
        Vector3 result = pos;
    
        result.x = pos.x * cos + pos.y * (-sin);
    
        result.z = pos.x * sin + pos.y * cos;
        return result;
    }
    /// <summary>
    /// 根据矩阵,围绕Z轴旋转
    /// </summary>
    /// <param name="angle">要旋转的角度</param>
    /// <param name="pos">要旋转的obj的位置</param>
    /// <returns></returns>
    public Vector3 DoTwistZ(float angle,Vector3 pos)
    {
        float cos = Mathf.Cos(angle);
        float sin = Mathf.Sin(angle);
    
        Vector3 result = pos;
    
        result.x = pos.x * cos + pos.y * sin;
        result.y = pos.x * (-sin) + pos.y * cos;
    
        return result;
    }
    

    }

    相关文章

      网友评论

          本文标题:旋转矩阵公式推导

          本文链接:https://www.haomeiwen.com/subject/guxsmqtx.html