CollideModel.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // CollideModel.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. #endregion
  17. namespace RobotGameData.Collision
  18. {
  19. /// <summary>
  20. /// It's a collision model.
  21. /// </summary>
  22. public class CollideModel : CollideElement
  23. {
  24. #region Fields
  25. protected Vector3[] vertices = null;
  26. protected Vector3[] normal = null;
  27. protected QuadTree quadTree = null;
  28. #endregion
  29. #region Properties
  30. public Vector3[] Vertices
  31. {
  32. get { return vertices; }
  33. }
  34. public Vector3[] Normal
  35. {
  36. get { return normal; }
  37. }
  38. public QuadTree QuadTree
  39. {
  40. get { return quadTree; }
  41. }
  42. #endregion
  43. /// <summary>
  44. /// Constructor.
  45. /// no quad tree.
  46. /// </summary>
  47. /// <param name="vertices">model's vertices</param>
  48. public CollideModel(Vector3[] vertices)
  49. : base()
  50. {
  51. this.vertices = vertices;
  52. // Creates vector array by triangle's vertices count
  53. this.normal = new Vector3[this.vertices.Length / 3];
  54. // Creates normal vector by each triangle
  55. for (int i = 0; i < this.normal.Length; i++)
  56. {
  57. Vector3 v1 = this.vertices[i * 3];
  58. Vector3 v2 = this.vertices[i * 3 + 1];
  59. Vector3 v3 = this.vertices[i * 3 + 2];
  60. this.normal[i] = Vector3.Normalize(Vector3.Cross(v3 - v1, v2 - v1));
  61. }
  62. }
  63. /// <summary>
  64. /// Constructor.
  65. /// use quad tree.
  66. /// </summary>
  67. /// <param name="vertices">model's vertices</param>
  68. /// <param name="buildQuadTreeDepth">quad tree depth count</param>
  69. public CollideModel(Vector3[] vertices, int buildQuadTreeDepth) : base()
  70. {
  71. this.vertices = vertices;
  72. // Creates vector array by triangle's vertices count
  73. this.normal = new Vector3[this.vertices.Length / 3];
  74. // Creates normal vector by each triangle
  75. for (int i = 0; i < this.normal.Length; i++)
  76. {
  77. Vector3 v1 = this.vertices[i * 3];
  78. Vector3 v2 = this.vertices[i * 3 + 1];
  79. Vector3 v3 = this.vertices[i * 3 + 2];
  80. this.normal[i] = Vector3.Normalize(Vector3.Cross(v3 - v1, v2 - v1));
  81. }
  82. // builds quad tree.
  83. if (buildQuadTreeDepth > 0)
  84. {
  85. this.quadTree = new QuadTree();
  86. this.quadTree.Build(this.Vertices, buildQuadTreeDepth);
  87. }
  88. }
  89. /// <summary>
  90. /// Set to new transform matrix.
  91. /// </summary>
  92. public override void Transform(Matrix matrix)
  93. {
  94. if (this.QuadTree != null)
  95. {
  96. int nodeDepth = this.QuadTree.DepthLevel;
  97. Matrix newTransform = matrix;
  98. // if changed matrix, re-build quad tree and collison vertices
  99. if (this.TransformMatrix != newTransform)
  100. {
  101. for (int i=0; i < this.vertices.Length; i++)
  102. {
  103. this.vertices[i] =
  104. Vector3.Transform(this.vertices[i], newTransform);
  105. }
  106. for (int i = 0; i < this.normal.Length; i++)
  107. {
  108. this.normal[i] =
  109. Vector3.Transform(this.normal[i], newTransform);
  110. }
  111. // re-build quad tree.
  112. this.quadTree = null;
  113. this.quadTree = new QuadTree();
  114. this.quadTree.Build(this.Vertices, nodeDepth);
  115. }
  116. }
  117. base.Transform(matrix);
  118. }
  119. /// <summary>
  120. /// Return a transformed vertex.
  121. /// </summary>
  122. /// <param name="index">vertex index</param>
  123. /// <returns>transformed vertex vector</returns>
  124. public Vector3 GetTransformedVertex(int index)
  125. {
  126. if (index > this.vertices.Length)
  127. throw new MemberAccessException("Overflow index (" + index + ")");
  128. return Vector3.Transform(this.vertices[index], TransformMatrix);
  129. }
  130. /// <summary>
  131. /// Return a transformed normal vector.
  132. /// </summary>
  133. /// <param name="index">vertex index</param>
  134. /// <returns>transformed normal vector</returns>
  135. public Vector3 GetTransformedNormal(int index)
  136. {
  137. if (index > this.normal.Length)
  138. throw new MemberAccessException("Overflow index (" + index + ")");
  139. return Vector3.Transform(this.normal[index], TransformMatrix);
  140. }
  141. }
  142. }