colmathplane.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WWMath *
  23. * *
  24. * $Archive:: /Commando/Code/wwmath/colmathplane.cpp $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 3/29/00 4:41p $*
  29. * *
  30. * $Revision:: 9 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "colmath.h"
  36. #include "colmathplane.h"
  37. #include "aaplane.h"
  38. #include "plane.h"
  39. #include "lineseg.h"
  40. #include "tri.h"
  41. #include "sphere.h"
  42. #include "aabox.h"
  43. #include "obbox.h"
  44. #include "wwdebug.h"
  45. CollisionMath::OverlapType
  46. CollisionMath::Overlap_Test(const AAPlaneClass & plane,const Vector3 & point)
  47. {
  48. float delta = point[plane.Normal] - plane.Dist;
  49. if (delta > COINCIDENCE_EPSILON) {
  50. return POS;
  51. }
  52. if (delta < -COINCIDENCE_EPSILON) {
  53. return NEG;
  54. }
  55. return ON;
  56. }
  57. CollisionMath::OverlapType
  58. CollisionMath::Overlap_Test(const AAPlaneClass & plane,const LineSegClass & line)
  59. {
  60. int mask = 0;
  61. mask |= CollisionMath::Overlap_Test(plane,line.Get_P0());
  62. mask |= CollisionMath::Overlap_Test(plane,line.Get_P1());
  63. return eval_overlap_mask(mask);
  64. }
  65. CollisionMath::OverlapType
  66. CollisionMath::Overlap_Test(const AAPlaneClass & plane,const TriClass & tri)
  67. {
  68. int mask = 0;
  69. mask |= CollisionMath::Overlap_Test(plane,*tri.V[0]);
  70. mask |= CollisionMath::Overlap_Test(plane,*tri.V[1]);
  71. mask |= CollisionMath::Overlap_Test(plane,*tri.V[2]);
  72. return eval_overlap_mask(mask);
  73. }
  74. CollisionMath::OverlapType
  75. CollisionMath::Overlap_Test(const AAPlaneClass & plane,const SphereClass & sphere)
  76. {
  77. float delta = sphere.Center[plane.Normal] - plane.Dist;
  78. if (delta > sphere.Radius) {
  79. return POS;
  80. }
  81. if (delta < sphere.Radius) {
  82. return NEG;
  83. }
  84. return BOTH;
  85. }
  86. CollisionMath::OverlapType
  87. CollisionMath::Overlap_Test(const AAPlaneClass & plane,const AABoxClass & box)
  88. {
  89. float delta;
  90. int mask = 0;
  91. // check the 'min' side of the box
  92. delta = (box.Center[plane.Normal] - box.Extent[plane.Normal]) - plane.Dist;
  93. if (delta > WWMATH_EPSILON) {
  94. mask |= POS;
  95. } else if (delta < -WWMATH_EPSILON) {
  96. mask |= NEG;
  97. } else {
  98. mask |= ON;
  99. }
  100. // check the 'max' side of the box
  101. delta = (box.Center[plane.Normal] + box.Extent[plane.Normal]) - plane.Dist;
  102. if (delta > WWMATH_EPSILON) {
  103. mask |= POS;
  104. } else if (delta < -WWMATH_EPSILON) {
  105. mask |= NEG;
  106. } else {
  107. mask |= ON;
  108. }
  109. return eval_overlap_mask(mask);
  110. }
  111. CollisionMath::OverlapType
  112. CollisionMath::Overlap_Test(const AAPlaneClass & /*plane*/,const OBBoxClass & /*box*/)
  113. {
  114. // TODO
  115. WWASSERT(0);
  116. return POS;
  117. }
  118. // Plane functions. Where is operand B with respect to the plane
  119. CollisionMath::OverlapType
  120. CollisionMath::Overlap_Test(const PlaneClass & plane,const LineSegClass & line)
  121. {
  122. int mask = 0;
  123. mask |= CollisionMath::Overlap_Test(plane,line.Get_P0());
  124. mask |= CollisionMath::Overlap_Test(plane,line.Get_P1());
  125. return eval_overlap_mask(mask);
  126. }
  127. CollisionMath::OverlapType
  128. CollisionMath::Overlap_Test(const PlaneClass & plane,const TriClass & tri)
  129. {
  130. int mask = 0;
  131. mask |= CollisionMath::Overlap_Test(plane,*tri.V[0]);
  132. mask |= CollisionMath::Overlap_Test(plane,*tri.V[1]);
  133. mask |= CollisionMath::Overlap_Test(plane,*tri.V[2]);
  134. return eval_overlap_mask(mask);
  135. }
  136. CollisionMath::OverlapType
  137. CollisionMath::Overlap_Test(const PlaneClass & plane,const SphereClass & sphere)
  138. {
  139. float dist = Vector3::Dot_Product(sphere.Center,plane.N) - plane.D;
  140. if (dist > sphere.Radius) {
  141. return POS;
  142. }
  143. if (dist < -sphere.Radius) {
  144. return NEG;
  145. }
  146. return BOTH;
  147. }
  148. CollisionMath::OverlapType
  149. CollisionMath::Overlap_Test(const PlaneClass & plane,const OBBoxClass & box)
  150. {
  151. // rotate the plane normal into box coordinates
  152. Vector3 local_normal;
  153. Vector3 posfarpt;
  154. Vector3 negfarpt;
  155. Matrix3::Transpose_Rotate_Vector(box.Basis,plane.N,&local_normal);
  156. get_far_extent(local_normal,box.Extent,&posfarpt);
  157. // transform the two extreme box coordinates into world space
  158. Matrix3::Rotate_Vector(box.Basis,posfarpt,&posfarpt);
  159. negfarpt = -posfarpt;
  160. posfarpt += box.Center;
  161. negfarpt += box.Center;
  162. // overlap test
  163. if (Overlap_Test(plane,negfarpt) == POS) {
  164. return POS;
  165. }
  166. if (Overlap_Test(plane,posfarpt) == NEG) {
  167. return NEG;
  168. }
  169. return BOTH;
  170. }