PageRenderTime 26ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/starLiGHT.Engine/starLiGHT.Collision/starLiGHT.Collision/Transform2D.cs

#
C# | 299 lines | 230 code | 35 blank | 34 comment | 27 complexity | e596d14f34056317755520ebd3a249b1 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0
  1. #region COPYRIGHT
  2. //--------------------------------------------------------------------------------
  3. // <copyright file="Transform2D.cs" company="starLiGHT Entertainment Studios">
  4. // Copyright (c) 2010, 2011
  5. // Roland Rosenkranz (Glatzemann@email.de)
  6. // </copyright>
  7. // <license>
  8. // This file is part of starLiGHT.Collision.
  9. //
  10. // starLiGHT.Collision is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU Lesser General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // starLiGHT.Collision is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU Lesser General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU Lesser General Public License
  21. // along with starLiGHT.Collision. If not, see http://www.gnu.org/licenses/.
  22. //
  23. // ADDITIONAL (commercial) LICENSES for starLiGHT.Collision are available on request.
  24. // </license>
  25. // <version>
  26. // **************[ starLiGHT.Engine SVN ]**********************
  27. // * $Rev:: 3345 $: Revision of last commit *
  28. // * $Author:: unknown $: Author of last commit *
  29. // * $Date:: 2011-01-13 16:01:08 #$: Date of last commit *
  30. // ************************************************************
  31. // </version>
  32. //--------------------------------------------------------------------------------
  33. #endregion
  34. namespace starLiGHT.Collision
  35. {
  36. #region Using Statement
  37. using System;
  38. using System.Collections.Generic;
  39. using System.Text;
  40. using Microsoft.Xna.Framework;
  41. #endregion
  42. public class Transform2D : IEquatable<Transform2D>
  43. {
  44. private Matrix transformationMatrix;
  45. private bool dirty;
  46. private Matrix originMatrix;
  47. private bool originMatrixDirty;
  48. private Matrix scaleMatrix;
  49. private bool scaleMatrixDirty;
  50. private Matrix translationMatrix;
  51. private bool translationMatrixDirty;
  52. private Matrix rotationMatrix;
  53. private bool rotationMatrixDirty;
  54. private Vector2? origin;
  55. private Vector2? position;
  56. private Vector2? scale;
  57. private float orientation;
  58. public Transform2D(Vector2 position)
  59. {
  60. this.dirty = true;
  61. this.position = position;
  62. this.translationMatrixDirty = true;
  63. this.origin = null;
  64. this.originMatrixDirty = true;
  65. this.scale = null;
  66. this.scaleMatrixDirty = true;
  67. this.orientation = 0f;
  68. this.rotationMatrixDirty = true;
  69. this.transformationMatrix = Matrix.Identity;
  70. this.originMatrix = Matrix.Identity;
  71. this.scaleMatrix = Matrix.Identity;
  72. this.translationMatrix = Matrix.Identity;
  73. this.rotationMatrix = Matrix.Identity;
  74. }
  75. public Transform2D(Vector2 position, float orientation)
  76. : this(position)
  77. {
  78. this.orientation = orientation;
  79. }
  80. public Transform2D(Vector2 position, float orientation, float scale)
  81. : this(position, orientation)
  82. {
  83. this.scale = new Vector2(scale, scale);
  84. }
  85. public Transform2D(Vector2 position, float orientation, Vector2 scale)
  86. : this(position, orientation)
  87. {
  88. this.scale = scale;
  89. }
  90. public Transform2D(Vector2 position, Vector2 origin)
  91. : this(position)
  92. {
  93. this.origin = origin;
  94. }
  95. public Transform2D(Vector2 position, Vector2 origin, float orientation)
  96. : this(position, orientation)
  97. {
  98. this.origin = origin;
  99. }
  100. public Transform2D(Vector2 position, Vector2 origin, float orientation, float scale)
  101. : this(position, orientation, scale)
  102. {
  103. this.origin = origin;
  104. }
  105. public Transform2D(Vector2 position, Vector2 origin, float orientation, Vector2 scale)
  106. : this(position, orientation, scale)
  107. {
  108. this.origin = origin;
  109. }
  110. public event EventHandler<EventArgs> Changed;
  111. /// <summary>
  112. /// Gets a value indicating whether the orientation is not equal zero or the scale is not one.
  113. /// </summary>
  114. public bool IsTransformed
  115. {
  116. get
  117. {
  118. return this.orientation != 0f || (this.scale.HasValue && this.scale.Value != Vector2.One);
  119. }
  120. }
  121. public Vector2? Origin
  122. {
  123. get
  124. {
  125. return this.origin;
  126. }
  127. set
  128. {
  129. if (this.origin != value)
  130. {
  131. this.originMatrixDirty |= true;
  132. this.origin = value;
  133. if (this.Changed != null)
  134. {
  135. this.Changed(this, null);
  136. }
  137. }
  138. }
  139. }
  140. public Vector2? Position
  141. {
  142. get
  143. {
  144. return this.position;
  145. }
  146. set
  147. {
  148. if (this.position != value)
  149. {
  150. this.translationMatrixDirty |= true;
  151. this.position = value;
  152. if (this.Changed != null)
  153. {
  154. this.Changed(this, null);
  155. }
  156. }
  157. }
  158. }
  159. public Vector2? Scale
  160. {
  161. get
  162. {
  163. return this.scale;
  164. }
  165. set
  166. {
  167. if (this.scale != value)
  168. {
  169. this.scaleMatrixDirty |= true;
  170. this.scale = value;
  171. if (this.Changed != null)
  172. {
  173. this.Changed(this, null);
  174. }
  175. }
  176. }
  177. }
  178. public float Orientation
  179. {
  180. get
  181. {
  182. return this.orientation;
  183. }
  184. set
  185. {
  186. if (Math.Abs(this.orientation - value) > float.Epsilon)
  187. {
  188. this.rotationMatrixDirty |= true;
  189. this.orientation = value;
  190. if (this.Changed != null)
  191. {
  192. this.Changed(this, null);
  193. }
  194. }
  195. }
  196. }
  197. public Matrix TransformationMatrix
  198. {
  199. get
  200. {
  201. this.UpdateMatrix();
  202. return this.transformationMatrix;
  203. }
  204. }
  205. public void Clear()
  206. {
  207. this.dirty = true;
  208. this.origin = null;
  209. this.originMatrixDirty = true;
  210. this.position = null;
  211. this.translationMatrixDirty = true;
  212. this.scale = null;
  213. this.scaleMatrixDirty = true;
  214. this.orientation = 0f;
  215. this.rotationMatrixDirty = true;
  216. if (this.Changed != null)
  217. {
  218. this.Changed(this, null);
  219. }
  220. }
  221. public bool Equals(Transform2D other)
  222. {
  223. return this.origin.Equals(other.origin) &&
  224. Math.Abs(this.orientation - other.orientation) < float.Epsilon &&
  225. this.scale.Equals(other.scale) &&
  226. this.position.Equals(other.position);
  227. }
  228. private void UpdateMatrix()
  229. {
  230. if (this.originMatrixDirty)
  231. {
  232. this.originMatrix = this.origin.HasValue ? Matrix.CreateTranslation(new Vector3(-this.origin.Value, 0f)) : Matrix.Identity;
  233. this.originMatrixDirty = false;
  234. this.dirty = true;
  235. }
  236. if (this.scaleMatrixDirty)
  237. {
  238. this.scaleMatrix = this.scale.HasValue ? Matrix.CreateScale(new Vector3(this.scale.Value, 0f)) : Matrix.Identity;
  239. this.scaleMatrixDirty = false;
  240. this.dirty = true;
  241. }
  242. if (this.translationMatrixDirty)
  243. {
  244. this.translationMatrix = this.position.HasValue ? Matrix.CreateTranslation(new Vector3(this.position.Value, 0f)) : Matrix.Identity;
  245. this.translationMatrixDirty = false;
  246. this.dirty = true;
  247. }
  248. if (this.rotationMatrixDirty)
  249. {
  250. this.rotationMatrix = this.orientation != 0f ? Matrix.CreateRotationZ(this.orientation) : Matrix.Identity;
  251. this.rotationMatrixDirty = false;
  252. this.dirty = true;
  253. }
  254. if (this.dirty)
  255. {
  256. this.transformationMatrix = this.originMatrix *
  257. this.scaleMatrix *
  258. this.rotationMatrix *
  259. this.translationMatrix;
  260. this.dirty = false;
  261. }
  262. }
  263. }
  264. }