Unity2D游戏中Matrix2D的创建
来源:互联网 发布:蓝牙音响 知乎 编辑:程序博客网 时间:2024/05/17 08:58
namespace UniSeks{ using System; using System.IO; using System.Runtime.InteropServices; using UnityEngine; public struct Matrix2D { public float A; public float B; public float C; public float D; public float Tx; public float Ty; public static readonly Matrix2D Identity; public Matrix2D(float tx, float ty, float a = 1f, float b = 0f, float c = 0f, float d = 1f) { this.Tx = tx; this.Ty = ty; this.A = a; this.B = b; this.C = c; this.D = d; } static Matrix2D() { Identity = new Matrix2D(0f, 0f, 1f, 0f, 0f, 1f); } public void ConcatRight(ref Matrix2D right) { float a = this.A; float b = this.B; float c = this.C; float d = this.D; float tx = this.Tx; float ty = this.Ty; this.A = (right.A * a) + (right.C * b); this.B = (a * right.B) + (b * right.D); this.C = (c * right.A) + (d * right.C); this.D = (c * right.B) + (d * right.D); this.Tx = ((right.A * tx) + (right.C * ty)) + right.Tx; this.Ty = ((right.B * tx) + (right.D * ty)) + right.Ty; } /// <summary> /// 矩阵的右乘 /// </summary> /// <param name="right"></param> public void ConcatRight(Matrix2D right) { float a = this.A; float b = this.B; float c = this.C; float d = this.D; float tx = this.Tx; float ty = this.Ty; this.A = (right.A * a) + (right.C * b); this.B = (a * right.B) + (b * right.D); this.C = (c * right.A) + (d * right.C); this.D = (c * right.B) + (d * right.D); this.Tx = ((right.A * tx) + (right.C * ty)) + right.Tx; this.Ty = ((right.B * tx) + (right.D * ty)) + right.Ty; } public void Invert(ref Matrix2D m) { float num = 1f / ((m.A * m.D) - (m.C * m.B)); this.A = num * m.D; this.B = -num * m.B; this.C = -num * m.C; this.D = num * m.A; this.Tx = num * ((m.B * m.Ty) - (m.D * m.Tx)); this.Ty = num * ((m.C * m.Tx) - (m.A * m.Ty)); } /// <summary> /// 逆矩阵 /// </summary> public void Invert() { float a = this.A; float b = this.B; float c = this.C; float d = this.D; float tx = this.Tx; float ty = this.Ty; //1/det float num7 = 1f / ((a * d) - (c * b)); this.A = num7 * d; this.B = -num7 * b; this.C = -num7 * c; this.D = num7 * a; this.Tx = num7 * ((b * ty) - (d * tx)); this.Ty = num7 * ((c * tx) - (a * ty)); } /// <summary> /// 初始化矩阵 /// </summary> public void MakeIdentity() { this.A = 1f; this.B = 0f; this.C = 0f; this.D = 1f; this.Tx = 0f; this.Ty = 0f; } /// <summary> /// 移动点 /// </summary> /// <param name="p"></param> /// <returns></returns> public Vector2 TransformPoint(ref Vector2 p) { return new Vector2(((p.x * this.A) + (p.y * this.B)) + this.Tx, ((p.x * this.C) + (p.y * this.D)) + this.Ty); } public Vector2 TransformPoint(float x, float y) { return new Vector2(((x * this.A) + (y * this.B)) + this.Tx, ((x * this.C) + (y * this.D)) + this.Ty); } public Vector2 Translation { get { return new Vector2(this.Tx, this.Ty); } set { this.Tx = value.x; this.Ty = value.y; } } /// <summary> /// 复制 /// </summary> /// <param name="m"></param> public void CopyFrom(ref Matrix2D m) { this.A = m.A; this.B = m.B; this.C = m.C; this.D = m.D; this.Tx = m.Tx; this.Ty = m.Ty; } public bool Equals(ref Matrix2D m) { return (((((this.A == m.A) && (this.B == m.B)) && ((this.C == m.C) && (this.D == m.D))) && (this.Tx == m.Tx)) && (this.Ty == m.Ty)); } public override bool Equals(object other) { if (!(other is Matrix2D)) { return false; } Matrix2D matrixd = (Matrix2D) other; return (((((this.A == matrixd.A) && (this.B == matrixd.B)) && ((this.C == matrixd.C) && (this.D == matrixd.D))) && (this.Tx == matrixd.Tx)) && (this.Ty == matrixd.Ty)); } public override int GetHashCode() { return (((((this.A.GetHashCode() ^ this.B.GetHashCode()) ^ this.C.GetHashCode()) ^ this.D.GetHashCode()) ^ this.Tx.GetHashCode()) ^ this.Ty.GetHashCode()); } public float ScaleX { get { float num = FastMath.SqrtRough((this.A * this.A) + (this.C * this.C)); if (this.A < 0.0) { num = -num; } return num; } } public float ScaleY { get { float num = FastMath.SqrtRough((this.B * this.B) + (this.D * this.D)); if (this.D < 0.0) { num = -num; } return num; } } public float Rotation { get { return (Mathf.Atan2(-this.C, this.A) * 57.29578f); } } public void MakeTransform(float rotation, float scaleX, float scaleY) { float num = FastMath.Sin(rotation); float num2 = FastMath.Cos(rotation); this.A = scaleX * num2; this.B = scaleY * num; this.C = -scaleX * num; this.D = scaleY * num2; } public void Read(BinaryReader reader) { MatrixFields fields = (MatrixFields) reader.ReadByte(); if (fields != MatrixFields.None) { if (((byte) (fields & MatrixFields.A)) != 0) { this.A = reader.ReadSingle(); } if (((byte) (fields & MatrixFields.B)) != 0) { this.B = reader.ReadSingle(); } if (((byte) (fields & MatrixFields.C)) != 0) { this.C = reader.ReadSingle(); } if (((byte) (fields & MatrixFields.D)) != 0) { this.D = reader.ReadSingle(); } if (((byte) (fields & (MatrixFields.None | MatrixFields.Tx))) != 0) { this.Tx = reader.ReadSingle(); } if (((byte) (fields & (MatrixFields.None | MatrixFields.Ty))) != 0) { this.Ty = reader.ReadSingle(); } } } [Flags] private enum MatrixFields : byte { A = 1, B = 2, C = 4, D = 8, None = 0, Tx = 0x10, Ty = 0x20 } }}
阅读全文
0 0
- Unity2D游戏中Matrix2D的创建
- <Unity笔记>Unity2D游戏的平滑旋转
- Unity2D游戏入门教程(一)宝石迷阵(1)创建项目
- Unity2d游戏开发:UGUI 与 Spine 的完美结合
- Unity2D平台跳跃游戏摄像机跟踪的区域限制
- 「Unity2D」使用Unity创建一个2D游戏系列-1
- 「Unity2D」使用Unity创建一个2D游戏系列-2
- 「Unity2D」使用Unity创建一个2D游戏系列-3
- 「Unity2D」使用Unity创建一个2D游戏系列-4
- 「Unity2D」使用Unity创建一个2D游戏系列-5
- 「Unity2D」使用Unity创建一个2D游戏系列-6
- 「Unity2D」使用Unity创建一个2D游戏系列-7
- 「Unity2D」使用Unity创建一个2D游戏系列-8
- 「Unity2D」使用Unity创建一个2D游戏系列-9
- 「Unity2D」使用Unity创建一个2D游戏系列-10
- 第一个游戏动作开发日志——Unity2D的血条制作
- Unity2d 5.1 弱联网游戏制作 (一)场景创建、添加组件、精灵移动、主摄像机跟随
- unity2D 4.3版 游戏制作教程
- 括号匹配
- H5新表单
- linux利用pxe实现自动装机
- kernel 启动流程之 【设备驱动加载】 学习笔记_好
- LeakCanary 学习记录
- Unity2D游戏中Matrix2D的创建
- Date
- 现场赛经验 by CQU ACM/ICPC
- uva12096心得
- 排序算法---冒泡排序(Bubble Sort)
- sqlite列出所有表信息
- Set集合小应用(图书购买系统)
- 【搜索入门专题1】I
- linuxPXE自动化部署服务器