colmathplane.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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. * Org Author:: Greg Hjelstrom *
  27. * *
  28. * Author : Kenny Mitchell *
  29. * *
  30. * $Modtime:: 06/26/02 4:04p $*
  31. * *
  32. * $Revision:: 10 $*
  33. * *
  34. * 06/26/02 KM Matrix name change to avoid MAX conflicts *
  35. *---------------------------------------------------------------------------------------------*
  36. * Functions: *
  37. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  38. #include "colmath.h"
  39. #include "colmathplane.h"
  40. #include "aaplane.h"
  41. #include "plane.h"
  42. #include "lineseg.h"
  43. #include "tri.h"
  44. #include "sphere.h"
  45. #include "aabox.h"
  46. #include "obbox.h"
  47. #include "wwdebug.h"
  48. CollisionMath::OverlapType
  49. CollisionMath::Overlap_Test(const AAPlaneClass & plane,const Vector3 & point)
  50. {
  51. float delta = point[plane.Normal] - plane.Dist;
  52. if (delta > COINCIDENCE_EPSILON) {
  53. return POS;
  54. }
  55. if (delta < -COINCIDENCE_EPSILON) {
  56. return NEG;
  57. }
  58. return ON;
  59. }
  60. CollisionMath::OverlapType
  61. CollisionMath::Overlap_Test(const AAPlaneClass & plane,const LineSegClass & line)
  62. {
  63. int mask = 0;
  64. mask |= CollisionMath::Overlap_Test(plane,line.Get_P0());
  65. mask |= CollisionMath::Overlap_Test(plane,line.Get_P1());
  66. return eval_overlap_mask(mask);
  67. }
  68. CollisionMath::OverlapType
  69. CollisionMath::Overlap_Test(const AAPlaneClass & plane,const TriClass & tri)
  70. {
  71. int mask = 0;
  72. mask |= CollisionMath::Overlap_Test(plane,*tri.V[0]);
  73. mask |= CollisionMath::Overlap_Test(plane,*tri.V[1]);
  74. mask |= CollisionMath::Overlap_Test(plane,*tri.V[2]);
  75. return eval_overlap_mask(mask);
  76. }
  77. CollisionMath::OverlapType
  78. CollisionMath::Overlap_Test(const AAPlaneClass & plane,const SphereClass & sphere)
  79. {
  80. float delta = sphere.Center[plane.Normal] - plane.Dist;
  81. if (delta > sphere.Radius) {
  82. return POS;
  83. }
  84. if (delta < sphere.Radius) {
  85. return NEG;
  86. }
  87. return BOTH;
  88. }
  89. CollisionMath::OverlapType
  90. CollisionMath::Overlap_Test(const AAPlaneClass & plane,const AABoxClass & box)
  91. {
  92. float delta;
  93. int mask = 0;
  94. // check the 'min' side of the box
  95. delta = (box.Center[plane.Normal] - box.Extent[plane.Normal]) - plane.Dist;
  96. if (delta > WWMATH_EPSILON) {
  97. mask |= POS;
  98. } else if (delta < -WWMATH_EPSILON) {
  99. mask |= NEG;
  100. } else {
  101. mask |= ON;
  102. }
  103. // check the 'max' side of the box
  104. delta = (box.Center[plane.Normal] + box.Extent[plane.Normal]) - plane.Dist;
  105. if (delta > WWMATH_EPSILON) {
  106. mask |= POS;
  107. } else if (delta < -WWMATH_EPSILON) {
  108. mask |= NEG;
  109. } else {
  110. mask |= ON;
  111. }
  112. return eval_overlap_mask(mask);
  113. }
  114. CollisionMath::OverlapType
  115. CollisionMath::Overlap_Test(const AAPlaneClass & /*plane*/,const OBBoxClass & /*box*/)
  116. {
  117. // TODO
  118. WWASSERT(0);
  119. return POS;
  120. }
  121. // Plane functions. Where is operand B with respect to the plane
  122. CollisionMath::OverlapType
  123. CollisionMath::Overlap_Test(const PlaneClass & plane,const LineSegClass & line)
  124. {
  125. int mask = 0;
  126. mask |= CollisionMath::Overlap_Test(plane,line.Get_P0());
  127. mask |= CollisionMath::Overlap_Test(plane,line.Get_P1());
  128. return eval_overlap_mask(mask);
  129. }
  130. CollisionMath::OverlapType
  131. CollisionMath::Overlap_Test(const PlaneClass & plane,const TriClass & tri)
  132. {
  133. int mask = 0;
  134. mask |= CollisionMath::Overlap_Test(plane,*tri.V[0]);
  135. mask |= CollisionMath::Overlap_Test(plane,*tri.V[1]);
  136. mask |= CollisionMath::Overlap_Test(plane,*tri.V[2]);
  137. return eval_overlap_mask(mask);
  138. }
  139. CollisionMath::OverlapType
  140. CollisionMath::Overlap_Test(const PlaneClass & plane,const SphereClass & sphere)
  141. {
  142. float dist = Vector3::Dot_Product(sphere.Center,plane.N) - plane.D;
  143. if (dist > sphere.Radius) {
  144. return POS;
  145. }
  146. if (dist < -sphere.Radius) {
  147. return NEG;
  148. }
  149. return BOTH;
  150. }
  151. CollisionMath::OverlapType
  152. CollisionMath::Overlap_Test(const PlaneClass & plane,const OBBoxClass & box)
  153. {
  154. // rotate the plane normal into box coordinates
  155. Vector3 local_normal;
  156. Vector3 posfarpt;
  157. Vector3 negfarpt;
  158. Matrix3x3::Transpose_Rotate_Vector(box.Basis,plane.N,&local_normal);
  159. get_far_extent(local_normal,box.Extent,&posfarpt);
  160. // transform the two extreme box coordinates into world space
  161. Matrix3x3::Rotate_Vector(box.Basis,posfarpt,&posfarpt);
  162. negfarpt = -posfarpt;
  163. posfarpt += box.Center;
  164. negfarpt += box.Center;
  165. // overlap test
  166. if (Overlap_Test(plane,negfarpt) == POS) {
  167. return POS;
  168. }
  169. if (Overlap_Test(plane,posfarpt) == NEG) {
  170. return NEG;
  171. }
  172. return BOTH;
  173. }