打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
【Unity人工智能学习
using UnityEngine;
using System.Collections;
public class C2DMatrix
{
    private struct Matrix
    {
        public float _11, _12, _13;
        public float _21, _22, _23;
        public float _31, _32, _33;
    };
    Matrix m_Matrix;
    public C2DMatrix()
    {
        //initialize the matrix to an identity matrix
        Identity();
    }
    /// <summary>
    /// 平移矩阵,移动矩阵到(x,y)位置
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    public void Translate(float x,float y)
    {
        Matrix mat=new Matrix();
        mat._11 = 1; mat._12 = 0; mat._13 = 0;
        mat._21 = 0; mat._22 = 1; mat._23 = 0;
        mat._31 = x; mat._32 = y; mat._32 = 1;
        //矩阵相乘
        MatrixMultiply(ref mat);
    }
    /// <summary>
    /// 创建一个单位矩阵
    /// </summary>
    public void Identity()
    {
        m_Matrix._11 = 1; m_Matrix._12 = 0; m_Matrix._13 = 0;
        m_Matrix._21 = 0; m_Matrix._22 = 1; m_Matrix._23 = 0;
        m_Matrix._31 = 0; m_Matrix._32 = 0; m_Matrix._33 = 1;
    }
    /// <summary>
    /// 当前矩阵A与B[vPoint.x,vPoint.y,1]矩阵相乘,最后把相乘的结果赋值回给相乘的矩阵B,得到顶点的变换
    /// </summary>
    /// <param name="vPoint"></param>
    public void TransformVector2Ds(ref Vector2 vPoint)
    {
        float tempX = (m_Matrix._11 * vPoint.x) + (m_Matrix._21 * vPoint.y) + (m_Matrix._31);
        float tempY = (m_Matrix._12 * vPoint.x) + (m_Matrix._22 * vPoint.y) + (m_Matrix._32);
        vPoint.x = tempX;
        vPoint.y = tempY;
    }
    /// <summary>
    /// 从一个2D矢量fwd的基础上创建一个旋转矩阵
    /// </summary>
    /// <param name="fwd"></param>
    /// <param name="side"></param>
    public void Rotate(ref Vector2 fwd, ref Vector2 side)
    {
        Matrix mat;
        mat._11 = fwd.x; mat._12 = fwd.y; mat._13 = 0;
        mat._21 = side.x; mat._22 = side.y; mat._23 = 0;
        mat._31 = 0; mat._32 = 0; mat._33 = 1;
        MatrixMultiply(ref mat);
    }
    /// <summary>
    /// 绕原点旋转rot角度
    /// </summary>
    /// <param name="rot">要旋转的角度</param>
    public void Rotate(float rot)
    {
        Matrix mat;
        float Sin = Mathf.Sin(rot);
        float Cos = Mathf.Cos(rot);
        mat._11 = Cos; mat._12 = Sin; mat._13 = 0;
        mat._21 = -Sin; mat._22 = Cos; mat._23 = 0;
        mat._31 = 0; mat._32 = 0; mat._33 = 1;
        MatrixMultiply(ref mat);
    }
    /// <summary>
    /// 当前矩阵乘以mIn矩阵并将结果赋给当前矩阵
    /// </summary>
    /// <param name="mIn"></param>
    private void MatrixMultiply(ref Matrix mIn)
    {
        Matrix mat_temp = new Matrix();
        //第一行
        mat_temp._11 = (m_Matrix._11 * mIn._11) + (m_Matrix._12 * mIn._21) + (m_Matrix._13 * mIn._31);
        mat_temp._12 = (m_Matrix._11 * mIn._12) + (m_Matrix._12 * mIn._22) + (m_Matrix._13 * mIn._32);
        mat_temp._13 = (m_Matrix._11 * mIn._13) + (m_Matrix._12 * mIn._23) + (m_Matrix._13 * mIn._33);
        //第二行
        mat_temp._21 = (m_Matrix._21 * mIn._11) + (m_Matrix._22 * mIn._21) + (m_Matrix._23 * mIn._31);
        mat_temp._22 = (m_Matrix._21 * mIn._12) + (m_Matrix._22 * mIn._22) + (m_Matrix._23 * mIn._32);
        mat_temp._23 = (m_Matrix._21 * mIn._13) + (m_Matrix._22 * mIn._23) + (m_Matrix._23 * mIn._33);
        //第三行
        mat_temp._31 = (m_Matrix._31 * mIn._11) + (m_Matrix._32 * mIn._21) + (m_Matrix._33 * mIn._31);
        mat_temp._32 = (m_Matrix._31 * mIn._12) + (m_Matrix._32 * mIn._22) + (m_Matrix._33 * mIn._32);
        mat_temp._33 = (m_Matrix._31 * mIn._13) + (m_Matrix._32 * mIn._23) + (m_Matrix._33 * mIn._33);
        m_Matrix = mat_temp;
    }
    #region 矩阵元素的属性
    public float _11
    {
        get
        {
            return m_Matrix._11;
        }
        set
        {
            m_Matrix._11 = value;
        }
    }
    public float _12
    {
        get
        {
            return m_Matrix._12;
        }
        set
        {
            m_Matrix._12 = value;
        }
    }
    public float _13
    {
        get
        {
            return m_Matrix._13;
        }
        set
        {
            m_Matrix._13 = value;
        }
    }
    public float _21
    {
        get
        {
            return m_Matrix._21;
        }
        set
        {
            m_Matrix._21 = value;
        }
    }
    public float _22
    {
        get
        {
            return m_Matrix._22;
        }
        set
        {
            m_Matrix._22 = value;
        }
    }
    public float _23
    {
        get
        {
            return m_Matrix._23;
        }
        set
        {
            m_Matrix._23 = value;
        }
    }
    public float _31
    {
        get
        {
            return m_Matrix._31;
        }
        set
        {
            m_Matrix._31 = value;
        }
    }
    public float _32
    {
        get
        {
            return m_Matrix._32;
        }
        set
        {
            m_Matrix._32 = value;
        }
    }
    public float _33
    {
        get
        {
            return m_Matrix._33;
        }
        set
        {
            m_Matrix._33 = value;
        }
    }
    #endregion
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
罗德里格斯公式
HLSL中的MUL指令深层剖析(矩阵)
一文学会Eigen库
卡尔曼滤波原理及实现
OpenCV-获取图像中直线上的数据
一看就懂的单目特征点恢复深度信息(三角化)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服