QuadNode.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // QuadNode.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Text;
  13. using Microsoft.Xna.Framework;
  14. using Microsoft.Xna.Framework.Content;
  15. using Microsoft.Xna.Framework.Graphics;
  16. using RobotGameData.GameObject;
  17. using RobotGameData.Helper;
  18. using RobotGameData.GameInterface;
  19. #endregion
  20. namespace RobotGameData.Collision
  21. {
  22. /// <summary>
  23. /// This is a quad tree's node only colliding. not support render model.
  24. /// </summary>
  25. public class QuadNode : INamed
  26. {
  27. #region Fields
  28. string name = String.Empty;
  29. int depth = 0;
  30. protected BoundingBox containBox;
  31. protected Vector3[] vertices = null;
  32. protected int vertextCount = 0;
  33. protected QuadNode parentNode = null;
  34. protected QuadNode upperLeftNode = null;
  35. protected QuadNode upperRightNode = null;
  36. protected QuadNode lowerLeftNode = null;
  37. protected QuadNode lowerRightNode = null;
  38. protected bool visit = false;
  39. #endregion
  40. #region Properties
  41. public string Name
  42. {
  43. get { return name; }
  44. set { name = value; }
  45. }
  46. public int Depth
  47. {
  48. get { return depth; }
  49. set { depth = value; }
  50. }
  51. public int VertextCount
  52. {
  53. get { return vertextCount; }
  54. }
  55. public Vector3[] Vertices
  56. {
  57. get { return vertices; }
  58. }
  59. public QuadNode Parent
  60. {
  61. get { return parentNode; }
  62. set { parentNode = value; }
  63. }
  64. public QuadNode UpperLeftNode
  65. {
  66. get { return upperLeftNode; }
  67. set { upperLeftNode = value; }
  68. }
  69. public QuadNode UpperRightNode
  70. {
  71. get { return upperRightNode; }
  72. set { upperRightNode = value; }
  73. }
  74. public QuadNode LowerLeftNode
  75. {
  76. get { return lowerLeftNode; }
  77. set { lowerLeftNode = value; }
  78. }
  79. public QuadNode LowerRightNode
  80. {
  81. get { return lowerRightNode; }
  82. set { lowerRightNode = value; }
  83. }
  84. public bool IsLeafNode
  85. {
  86. get
  87. {
  88. return (upperLeftNode == null && upperRightNode == null &&
  89. lowerLeftNode == null && lowerRightNode == null);
  90. }
  91. }
  92. #endregion
  93. /// <summary>
  94. /// constructor.
  95. /// </summary>
  96. /// <param name="min"></param>
  97. /// <param name="max"></param>
  98. public QuadNode(Vector3 min, Vector3 max)
  99. {
  100. containBox.Min = min;
  101. containBox.Max = max;
  102. this.vertextCount = 0;
  103. }
  104. /// <summary>
  105. /// create vertices.
  106. /// </summary>
  107. public void CreateVertices(Vector3[] values)
  108. {
  109. int count = values.Length;
  110. this.vertices = new Vector3[count];
  111. for (int i = 0; i < count; i++)
  112. {
  113. this.vertices[i] = values[i];
  114. this.vertextCount++;
  115. }
  116. }
  117. /// <summary>
  118. /// add vertices.
  119. /// </summary>
  120. public void AddVertex(Vector3[] values)
  121. {
  122. ResizeVertexArray(values.Length);
  123. int lastIndex = this.vertextCount;
  124. for (int i = 0; i < values.Length; i++)
  125. {
  126. this.vertices[i + lastIndex] = values[i];
  127. this.vertextCount++;
  128. }
  129. }
  130. /// <summary>
  131. /// resize vertex array.
  132. /// </summary>
  133. /// <param name="resizingCount"></param>
  134. public void ResizeVertexArray(int resizingCount)
  135. {
  136. if (this.vertices.Length < this.vertextCount + resizingCount)
  137. {
  138. Vector3[] newVertices =
  139. new Vector3[this.vertices.Length + resizingCount];
  140. // move to new array
  141. for (int i = 0; i < this.vertextCount; i++)
  142. newVertices[i] = this.vertices[i];
  143. this.vertices = null;
  144. this.vertices = newVertices;
  145. }
  146. }
  147. /// <summary>
  148. /// Finds a contaning quad node.
  149. /// </summary>
  150. public QuadNode GetNodeContaining(ref CollideElement bounds)
  151. {
  152. if (this.IsLeafNode == false)
  153. {
  154. // checks with upper left node.
  155. if (UpperLeftNode != null)
  156. {
  157. if (UpperLeftNode.Contains(ref bounds) )
  158. return UpperLeftNode.GetNodeContaining(ref bounds);
  159. }
  160. // checks with upper right node.
  161. if (UpperRightNode != null)
  162. {
  163. if (UpperRightNode.Contains(ref bounds) )
  164. return UpperRightNode.GetNodeContaining(ref bounds);
  165. }
  166. // checks with lower left node.
  167. if (LowerLeftNode != null)
  168. {
  169. if (LowerLeftNode.Contains(ref bounds) )
  170. return LowerLeftNode.GetNodeContaining(ref bounds);
  171. }
  172. // checks with lower right node.
  173. if (LowerLeftNode != null)
  174. {
  175. if (LowerLeftNode.Contains(ref bounds) )
  176. return LowerLeftNode.GetNodeContaining(ref bounds);
  177. }
  178. }
  179. // checks with this node.
  180. if( this.Contains(ref bounds) )
  181. return this;
  182. return null;
  183. }
  184. /// <summary>
  185. /// checks contaning.
  186. /// </summary>
  187. public bool Contains(ref CollideElement bounds)
  188. {
  189. if (bounds is CollideBox)
  190. {
  191. CollideBox target = bounds as CollideBox;
  192. return containBox.Intersects(target.BoundingBox);
  193. }
  194. else if (bounds is CollideSphere)
  195. {
  196. CollideSphere target = bounds as CollideSphere;
  197. return containBox.Intersects(target.BoundingSphere);
  198. }
  199. else if (bounds is CollideRay)
  200. {
  201. CollideRay target = bounds as CollideRay;
  202. return (containBox.Intersects(target.Ray) != null);
  203. }
  204. return false;
  205. }
  206. /// <summary>
  207. /// display build information to debug output window.
  208. /// </summary>
  209. /// <returns>total vertex count</returns>
  210. public int Dump()
  211. {
  212. int vertexCount = this.VertextCount;
  213. System.Diagnostics.Debug.WriteLine(
  214. this.Name + "[" + this.Depth + "] vertex = " +
  215. this.VertextCount.ToString() + ")");
  216. if (UpperLeftNode != null)
  217. {
  218. vertexCount += UpperLeftNode.Dump();
  219. }
  220. if (UpperRightNode != null)
  221. {
  222. vertexCount += UpperRightNode.Dump();
  223. }
  224. if (LowerLeftNode != null)
  225. {
  226. vertexCount += LowerLeftNode.Dump();
  227. }
  228. if (LowerRightNode != null)
  229. {
  230. vertexCount += LowerRightNode.Dump();
  231. }
  232. return vertexCount;
  233. }
  234. }
  235. }