/unity/Assets/Futile/Core/FMatrix.cs

https://bitbucket.org/ironpencil/tankfight · C# · 273 lines · 220 code · 46 blank · 7 comment · 0 complexity · 5288e791dafe9044b9e8b7be466a1bb1 MD5 · raw file

  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. //My own matrix thing based on stuff I learned here:
  5. //http://www.senocular.com/flash/tutorials/transformmatrix/
  6. public class FMatrix
  7. {
  8. static public FMatrix tempMatrix = new FMatrix(); //useful for doing calulations without allocating a new matrix every time
  9. public float a = 1; //scaleX
  10. public float b = 0; //skewY
  11. public float c = 0; //skewX
  12. public float d = 1; //scaleY
  13. public float tx = 0; //translationX
  14. public float ty = 0; //translationY
  15. //a b u
  16. //c d v
  17. //tx ty z
  18. //x = x*a + y*c + tx
  19. //y = x*b + y*d + ty
  20. public FMatrix ()
  21. {
  22. }
  23. public FMatrix Clone()
  24. {
  25. FMatrix result = new FMatrix();
  26. result.a = a;
  27. result.b = b;
  28. result.c = c;
  29. result.d = d;
  30. result.tx = tx;
  31. result.ty = ty;
  32. return result;
  33. }
  34. public void CopyValues(FMatrix sourceMatrix)
  35. {
  36. a = sourceMatrix.a;
  37. b = sourceMatrix.b;
  38. c = sourceMatrix.c;
  39. d = sourceMatrix.d;
  40. tx = sourceMatrix.tx;
  41. ty = sourceMatrix.ty;
  42. }
  43. public void SetRotateThenScale(float x, float y, float scaleX, float scaleY, float rotationInRadians) //rotates then scales
  44. {
  45. float sin = Mathf.Sin(rotationInRadians);
  46. float cos = Mathf.Cos(rotationInRadians);
  47. a = scaleX * cos;
  48. c = scaleX * -sin;
  49. b = scaleY * sin;
  50. d = scaleY * cos;
  51. tx = x;
  52. ty = y;
  53. }
  54. public void SetScaleThenRotate(float x, float y, float scaleX, float scaleY, float rotationInRadians) //scales then rotates
  55. {
  56. float sin = Mathf.Sin(rotationInRadians);
  57. float cos = Mathf.Cos(rotationInRadians);
  58. a = scaleX*cos;
  59. b = scaleX*sin;
  60. c = scaleY*-sin;
  61. d = scaleY*cos;
  62. tx = x;
  63. ty = y;
  64. }
  65. public void Translate(float deltaX, float deltaY)
  66. {
  67. tx += deltaX;
  68. ty += deltaY;
  69. }
  70. public void Scale(float scaleX, float scaleY)
  71. {
  72. a *= scaleX;
  73. c *= scaleX;
  74. tx *= scaleX;
  75. b *= scaleY;
  76. d *= scaleY;
  77. ty *= scaleY;
  78. }
  79. public void Rotate(float rotationInRadians)
  80. {
  81. float sin = Mathf.Sin(rotationInRadians);
  82. float cos = Mathf.Cos(rotationInRadians);
  83. float oldA = a;
  84. float oldB = b;
  85. float oldC = c;
  86. float oldD = d;
  87. float oldTX = tx;
  88. float oldTY = ty;
  89. a = oldA*cos - oldB*sin;
  90. b = oldA*sin + oldB*cos;
  91. c = oldC*cos - oldD*sin;
  92. d = oldC*sin + oldD*cos;
  93. tx = oldTX*cos - oldTY*sin;
  94. ty = oldTX*sin + oldTY*cos;
  95. }
  96. public void RotateInPlace(float rotationInRadians)
  97. {
  98. float sin = Mathf.Sin(rotationInRadians);
  99. float cos = Mathf.Cos(rotationInRadians);
  100. float oldA = a;
  101. float oldB = b;
  102. float oldC = c;
  103. float oldD = d;
  104. a = oldA*cos - oldB*sin;
  105. b = oldA*sin + oldB*cos;
  106. c = oldC*cos - oldD*sin;
  107. d = oldC*sin + oldD*cos;
  108. }
  109. public float GetScaleX()
  110. {
  111. return Mathf.Sqrt(a*a + b*b);
  112. }
  113. public float GetScaleY()
  114. {
  115. return Mathf.Sqrt(c*c + d*d);
  116. }
  117. public float GetRotation()
  118. {
  119. Vector2 newVector = GetNewTransformedVector(new Vector2(0,1));
  120. return Mathf.Atan2(newVector.y-ty, newVector.x-tx) - RXMath.HALF_PI;
  121. }
  122. public void Concat(FMatrix other)
  123. {
  124. float oldA = a;
  125. float oldB = b;
  126. float oldC = c;
  127. float oldD = d;
  128. float oldTX = tx;
  129. float oldTY = ty;
  130. a = oldA*other.a + oldB*other.c;
  131. b = oldA*other.b + oldB*other.d;
  132. c = oldC*other.a + oldD*other.c;
  133. d = oldC*other.b + oldD*other.d;
  134. tx = oldTX*other.a + oldTY*other.c + other.tx;
  135. ty = oldTX*other.b + oldTY*other.d + other.ty;
  136. }
  137. public void ConcatOther(FMatrix other) //the opposite order of Concat
  138. {
  139. float oldA = a;
  140. float oldB = b;
  141. float oldC = c;
  142. float oldD = d;
  143. float oldTX = tx;
  144. float oldTY = ty;
  145. a = other.a*oldA + other.b*oldC;
  146. b = other.a*oldB + other.b*oldD;
  147. c = other.c*oldA + other.d*oldC;
  148. d = other.c*oldB + other.d*oldD;
  149. tx = other.tx*oldA + other.ty*oldC + oldTX;
  150. ty = other.tx*oldB + other.ty*oldD + oldTY;
  151. }
  152. public void ConcatAndCopyValues(FMatrix first, FMatrix second)
  153. {
  154. a = first.a*second.a + first.b*second.c;
  155. b = first.a*second.b + first.b*second.d;
  156. c = first.c*second.a + first.d*second.c;
  157. d = first.c*second.b + first.d*second.d;
  158. tx = first.tx*second.a + first.ty*second.c + second.tx;
  159. ty = first.tx*second.b + first.ty*second.d + second.ty;
  160. }
  161. public void Invert()
  162. {
  163. float oldA = a;
  164. float oldB = b;
  165. float oldC = c;
  166. float oldD = d;
  167. float oldTX = tx;
  168. float oldTY = ty;
  169. float bottom = 1.0f/(a*d-b*c);
  170. a = oldD*bottom;
  171. b = -oldB*bottom;
  172. c = -oldC*bottom;
  173. d = oldA*bottom;
  174. tx = (oldC*oldTY-oldD*oldTX)*bottom;
  175. ty = -(oldA*oldTY-oldB*oldTX)*bottom;
  176. }
  177. public void InvertAndCopyValues(FMatrix other)
  178. {
  179. float bottom = 1.0f/(other.a*other.d-other.b*other.c);
  180. a = other.d*bottom;
  181. b = -other.b*bottom;
  182. c = -other.c*bottom;
  183. d = other.a*bottom;
  184. tx = (other.c*other.ty-other.d*other.tx)*bottom;
  185. ty = -(other.a*other.ty-other.b*other.tx)*bottom;
  186. }
  187. public Vector2 GetNewTransformedVector(Vector2 vector)
  188. {
  189. return new Vector2
  190. (
  191. vector.x*a + vector.y*c + tx,
  192. vector.x*b + vector.y*d + ty
  193. );
  194. }
  195. public Vector2 GetTransformedUnitVector()
  196. {
  197. return new Vector2(a+c+tx, b+d+ty);
  198. }
  199. public Vector3 GetVector3FromLocalVector2(Vector2 localVector, float z)
  200. {
  201. return new Vector3
  202. (
  203. localVector.x*a + localVector.y*c + tx,
  204. localVector.x*b + localVector.y*d + ty,
  205. z
  206. );
  207. }
  208. public void ApplyVector3FromLocalVector2(ref Vector3 outVector, Vector2 localVector, float z)
  209. {
  210. outVector.x = localVector.x*a + localVector.y*c + tx;
  211. outVector.y = localVector.x*b + localVector.y*d + ty;
  212. outVector.z = z;
  213. }
  214. public void ResetToIdentity()
  215. {
  216. a = 1;
  217. b = 0;
  218. c = 0;
  219. d = 1;
  220. tx = 0;
  221. ty = 0;
  222. }
  223. override public string ToString()
  224. {
  225. return string.Format("[[Matrix A:{0} B:{1} C:{2} D:{3} TX:{4} TY:{5} ]]", a, b, c, d, tx,ty);
  226. }
  227. }