/SketchEditor/NUnit/BoxCollisionShapeTests.cs

https://bitbucket.org/spanky/valor3d · C# · 209 lines · 148 code · 61 blank · 0 comment · 0 complexity · a73c7fb2fc13eaf965398c0941c97aaf MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using NUnit.Framework;
  5. using Editor;
  6. namespace NUnitTests
  7. {
  8. [TestFixture]
  9. public class BoxCollisionShapeTests
  10. {
  11. public Mogre.AxisAlignedBox GetUnitCube()
  12. {
  13. return new Mogre.AxisAlignedBox(-0.5f, -0.5f, -0.5f, 0.5f, 0.5f, 0.5f);
  14. }
  15. #region Outside Axis Aligned Colliding Tests
  16. [Test]
  17. public void RayFromOutsideAgainstPositiveXSide()
  18. {
  19. BoxCollisionShape box = new BoxCollisionShape(new BoxCollisionShape.RetrieveLocalBoundsHandler(GetUnitCube));
  20. IntersectionResult result = box.Intersect(null, new Mogre.Vector3(1.0f, 0.0f, 0.0f), new Mogre.Vector3(-1.0f, 0.0f, 0.0f), false);
  21. Assert.IsTrue(result.IsValid, "The intersection result was not valid");
  22. Assert.AreEqual(0.5f, result.Distance, "The intersection location was not at the correct location");
  23. }
  24. [Test]
  25. public void RayFromOutsideAgainstPositiveYSide()
  26. {
  27. BoxCollisionShape box = new BoxCollisionShape(new BoxCollisionShape.RetrieveLocalBoundsHandler(GetUnitCube));
  28. IntersectionResult result = box.Intersect(null, new Mogre.Vector3(0.0f, 1.0f, 0.0f), new Mogre.Vector3(0.0f, -1.0f, 0.0f), false);
  29. Assert.IsTrue(result.IsValid, "The intersection result was not valid");
  30. Assert.AreEqual(0.5f, result.Distance, "The intersection location was not at the correct location");
  31. }
  32. [Test]
  33. public void RayFromOutsideAgainstPositiveZSide()
  34. {
  35. BoxCollisionShape box = new BoxCollisionShape(new BoxCollisionShape.RetrieveLocalBoundsHandler(GetUnitCube));
  36. IntersectionResult result = box.Intersect(null, new Mogre.Vector3(0.0f, 0.0f, 1.0f), new Mogre.Vector3(0.0f, 0.0f, -1.0f), false);
  37. Assert.IsTrue(result.IsValid, "The intersection result was not valid");
  38. Assert.AreEqual(0.5f, result.Distance, "The intersection location was not at the correct location");
  39. }
  40. [Test]
  41. public void RayFromOutsideAgainstNegativeXSide()
  42. {
  43. BoxCollisionShape box = new BoxCollisionShape(new BoxCollisionShape.RetrieveLocalBoundsHandler(GetUnitCube));
  44. IntersectionResult result = box.Intersect(null, new Mogre.Vector3(-1.0f, 0.0f, 0.0f), new Mogre.Vector3(1.0f, 0.0f, 0.0f), false);
  45. Assert.IsTrue(result.IsValid, "The intersection result was not valid");
  46. Assert.AreEqual(0.5f, result.Distance, "The intersection location was not at the correct location");
  47. }
  48. [Test]
  49. public void RayFromOutsideAgainstNegativeYSide()
  50. {
  51. BoxCollisionShape box = new BoxCollisionShape(new BoxCollisionShape.RetrieveLocalBoundsHandler(GetUnitCube));
  52. IntersectionResult result = box.Intersect(null, new Mogre.Vector3(0.0f, -1.0f, 0.0f), new Mogre.Vector3(0.0f, 1.0f, 0.0f), false);
  53. Assert.IsTrue(result.IsValid, "The intersection result was not valid");
  54. Assert.AreEqual(0.5f, result.Distance, "The intersection location was not at the correct location");
  55. }
  56. [Test]
  57. public void RayFromOutsideAgainstNegativeZSide()
  58. {
  59. BoxCollisionShape box = new BoxCollisionShape(new BoxCollisionShape.RetrieveLocalBoundsHandler(GetUnitCube));
  60. IntersectionResult result = box.Intersect(null, new Mogre.Vector3(0.0f, 0.0f, -1.0f), new Mogre.Vector3(0.0f, 0.0f, 1.0f), false);
  61. Assert.IsTrue(result.IsValid, "The intersection result was not valid");
  62. Assert.AreEqual(0.5f, result.Distance, "The intersection location was not at the correct location");
  63. }
  64. #endregion
  65. #region Corner Testing
  66. [Test]
  67. public void TopLeftFrontCorner()
  68. {
  69. BoxCollisionShape box = new BoxCollisionShape(new BoxCollisionShape.RetrieveLocalBoundsHandler(GetUnitCube));
  70. Mogre.Vector3 rayDirection = new Mogre.Vector3(1.0f, -1.0f, -1.0f).NormalisedCopy;
  71. Mogre.Vector3 rayOrigin = new Mogre.Vector3(-0.5f, 0.5f, 0.5f) + (-rayDirection * 0.5f);
  72. IntersectionResult result = box.Intersect(null, rayOrigin, rayDirection, false);
  73. Assert.IsTrue(result.IsValid, "The intersection result was not valid");
  74. Assert.AreEqual(0.5f, result.Distance, "The intersection location was not at the correct location");
  75. }
  76. [Test]
  77. public void BottomLeftFrontCorner()
  78. {
  79. BoxCollisionShape box = new BoxCollisionShape(new BoxCollisionShape.RetrieveLocalBoundsHandler(GetUnitCube));
  80. Mogre.Vector3 rayDirection = new Mogre.Vector3(1.0f, 1.0f, -1.0f).NormalisedCopy;
  81. Mogre.Vector3 rayOrigin = new Mogre.Vector3(-0.5f, -0.5f, 0.5f) + (-rayDirection * 0.5f);
  82. IntersectionResult result = box.Intersect(null, rayOrigin, rayDirection, false);
  83. Assert.IsTrue(result.IsValid, "The intersection result was not valid");
  84. Assert.AreEqual(0.5f, result.Distance, "The intersection location was not at the correct location");
  85. }
  86. [Test]
  87. public void TopRightFrontCorner()
  88. {
  89. BoxCollisionShape box = new BoxCollisionShape(new BoxCollisionShape.RetrieveLocalBoundsHandler(GetUnitCube));
  90. Mogre.Vector3 rayDirection = new Mogre.Vector3(-1.0f, -1.0f, -1.0f).NormalisedCopy;
  91. Mogre.Vector3 rayOrigin = new Mogre.Vector3(0.5f, 0.5f, 0.5f) + (-rayDirection * 0.5f);
  92. IntersectionResult result = box.Intersect(null, rayOrigin, rayDirection, false);
  93. Assert.IsTrue(result.IsValid, "The intersection result was not valid");
  94. Assert.AreEqual(0.5f, result.Distance, "The intersection location was not at the correct location");
  95. }
  96. [Test]
  97. public void BottomRightFrontCorner()
  98. {
  99. BoxCollisionShape box = new BoxCollisionShape(new BoxCollisionShape.RetrieveLocalBoundsHandler(GetUnitCube));
  100. Mogre.Vector3 rayDirection = new Mogre.Vector3(-1.0f, -1.0f, -1.0f).NormalisedCopy;
  101. Mogre.Vector3 rayOrigin = new Mogre.Vector3(0.5f, 0.5f, 0.5f) + (-rayDirection * 0.5f);
  102. IntersectionResult result = box.Intersect(null, rayOrigin, rayDirection, false);
  103. Assert.IsTrue(result.IsValid, "The intersection result was not valid");
  104. Assert.AreEqual(0.5f, result.Distance, "The intersection location was not at the correct location");
  105. }
  106. [Test]
  107. public void TopLeftBackCorner()
  108. {
  109. BoxCollisionShape box = new BoxCollisionShape(new BoxCollisionShape.RetrieveLocalBoundsHandler(GetUnitCube));
  110. Mogre.Vector3 rayDirection = new Mogre.Vector3(1.0f, -1.0f, 1.0f).NormalisedCopy;
  111. Mogre.Vector3 rayOrigin = new Mogre.Vector3(-0.5f, 0.5f, -0.5f) + (-rayDirection * 0.5f);
  112. IntersectionResult result = box.Intersect(null, rayOrigin, rayDirection, false);
  113. Assert.IsTrue(result.IsValid, "The intersection result was not valid");
  114. Assert.AreEqual(0.5f, result.Distance, "The intersection location was not at the correct location");
  115. }
  116. [Test]
  117. public void BottomLeftBackCorner()
  118. {
  119. BoxCollisionShape box = new BoxCollisionShape(new BoxCollisionShape.RetrieveLocalBoundsHandler(GetUnitCube));
  120. Mogre.Vector3 rayDirection = new Mogre.Vector3(1.0f, 1.0f, 1.0f).NormalisedCopy;
  121. Mogre.Vector3 rayOrigin = new Mogre.Vector3(-0.5f, -0.5f, -0.5f) + (-rayDirection * 0.5f);
  122. IntersectionResult result = box.Intersect(null, rayOrigin, rayDirection, false);
  123. Assert.IsTrue(result.IsValid, "The intersection result was not valid");
  124. Assert.AreEqual(0.5f, result.Distance, "The intersection location was not at the correct location");
  125. }
  126. [Test]
  127. public void TopRightBackCorner()
  128. {
  129. BoxCollisionShape box = new BoxCollisionShape(new BoxCollisionShape.RetrieveLocalBoundsHandler(GetUnitCube));
  130. Mogre.Vector3 rayDirection = new Mogre.Vector3(-1.0f, -1.0f, 1.0f).NormalisedCopy;
  131. Mogre.Vector3 rayOrigin = new Mogre.Vector3(0.5f, 0.5f, -0.5f) + (-rayDirection * 0.5f);
  132. IntersectionResult result = box.Intersect(null, rayOrigin, rayDirection, false);
  133. Assert.IsTrue(result.IsValid, "The intersection result was not valid");
  134. Assert.AreEqual(0.5f, result.Distance, "The intersection location was not at the correct location");
  135. }
  136. [Test]
  137. public void BottomRightBackCorner()
  138. {
  139. BoxCollisionShape box = new BoxCollisionShape(new BoxCollisionShape.RetrieveLocalBoundsHandler(GetUnitCube));
  140. Mogre.Vector3 rayDirection = new Mogre.Vector3(-1.0f, -1.0f, 1.0f).NormalisedCopy;
  141. Mogre.Vector3 rayOrigin = new Mogre.Vector3(0.5f, 0.5f, -0.5f) + (-rayDirection * 0.5f);
  142. IntersectionResult result = box.Intersect(null, rayOrigin, rayDirection, false);
  143. Assert.IsTrue(result.IsValid, "The intersection result was not valid");
  144. Assert.AreEqual(0.5f, result.Distance, "The intersection location was not at the correct location");
  145. }
  146. #endregion
  147. }
  148. }