colmathsphere.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. ** Command & Conquer Generals(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/colmathsphere.cpp $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 4/25/01 2:05p $*
  29. * *
  30. * $Revision:: 6 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * CollisionMath::Intersection_Test -- Sphere - AAbox intersection *
  35. * CollisionMath::Intersection_Test -- Sphere - OBBox intersection *
  36. * CollisionMath::Overlap_Test -- Sphere - Point overlap test *
  37. * CollisionMath::Overlap_Test -- sphere line overlap test *
  38. * CollisionMath::Overlap_Test -- sphere triangle overlap test *
  39. * CollisionMath::Overlap_Test -- Sphere - Sphere overlap test *
  40. * CollisionMath::Overlap_Test -- Sphere - AABox overlap test *
  41. * CollisionMath::Overlap_Test -- Sphere - OBBox overlap test *
  42. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  43. #include "colmath.h"
  44. #include "aaplane.h"
  45. #include "plane.h"
  46. #include "lineseg.h"
  47. #include "tri.h"
  48. #include "sphere.h"
  49. #include "aabox.h"
  50. #include "obbox.h"
  51. #include "wwdebug.h"
  52. // Sphere Intersection fucntions. Does the sphere intersect the passed in object
  53. /***********************************************************************************************
  54. * CollisionMath::Intersection_Test -- Sphere - AAbox intersection *
  55. * *
  56. * INPUT: *
  57. * *
  58. * OUTPUT: *
  59. * *
  60. * WARNINGS: *
  61. * *
  62. * HISTORY: *
  63. * 4/25/2001 gth : Created. *
  64. *=============================================================================================*/
  65. bool CollisionMath::Intersection_Test(const SphereClass & sphere,const AABoxClass & box)
  66. {
  67. /*
  68. ** Simple but slightly inaccurate test, expand the box by the sphere's radius, then
  69. ** test whether the sphere is contained in that new box. This is actually testing
  70. ** against a cube which encloses the sphere...
  71. */
  72. Vector3 dc = box.Center - sphere.Center;
  73. if (WWMath::Fabs(dc.X) < box.Extent.X + sphere.Radius) return false;
  74. if (WWMath::Fabs(dc.Y) < box.Extent.Y + sphere.Radius) return false;
  75. if (WWMath::Fabs(dc.Z) < box.Extent.Z + sphere.Radius) return false;
  76. return true;
  77. }
  78. /***********************************************************************************************
  79. * CollisionMath::Intersection_Test -- Sphere - OBBox intersection *
  80. * *
  81. * INPUT: *
  82. * *
  83. * OUTPUT: *
  84. * *
  85. * WARNINGS: *
  86. * *
  87. * HISTORY: *
  88. * 4/25/2001 gth : Created. *
  89. *=============================================================================================*/
  90. bool CollisionMath::Intersection_Test(const SphereClass & sphere,const OBBoxClass & box)
  91. {
  92. /*
  93. ** Compute the sphere's position in the box's coordinate system
  94. */
  95. Matrix3D tm(box.Basis,box.Center);
  96. Vector3 box_rel_center;
  97. Matrix3D::Inverse_Transform_Vector(tm,sphere.Center,&box_rel_center);
  98. if (box.Extent.X < WWMath::Fabs(box_rel_center.X)) return false;
  99. if (box.Extent.Y < WWMath::Fabs(box_rel_center.Y)) return false;
  100. if (box.Extent.Z < WWMath::Fabs(box_rel_center.Z)) return false;
  101. return true;
  102. }
  103. // Sphere Overlap functions. Where is operand B with respect to the sphere
  104. /***********************************************************************************************
  105. * CollisionMath::Overlap_Test -- Sphere - Point overlap test *
  106. * *
  107. * INPUT: *
  108. * *
  109. * OUTPUT: *
  110. * *
  111. * WARNINGS: *
  112. * *
  113. * HISTORY: *
  114. * 4/25/2001 gth : Created. *
  115. *=============================================================================================*/
  116. CollisionMath::OverlapType
  117. CollisionMath::Overlap_Test(const SphereClass & sphere,const Vector3 & point)
  118. {
  119. float r2 = (point - sphere.Center).Length2();
  120. if (r2 < sphere.Radius * sphere.Radius - COINCIDENCE_EPSILON) {
  121. return NEG;
  122. }
  123. if (r2 > sphere.Radius * sphere.Radius + COINCIDENCE_EPSILON) {
  124. return POS;
  125. }
  126. return ON;
  127. }
  128. /***********************************************************************************************
  129. * CollisionMath::Overlap_Test -- sphere line overlap test *
  130. * *
  131. * INPUT: *
  132. * *
  133. * OUTPUT: *
  134. * *
  135. * WARNINGS: *
  136. * *
  137. * HISTORY: *
  138. * 4/25/2001 gth : Created. *
  139. *=============================================================================================*/
  140. CollisionMath::OverlapType
  141. CollisionMath::Overlap_Test(const SphereClass & /*sphere*/,const LineSegClass & /*line*/)
  142. {
  143. WWASSERT(0); //TODO
  144. return POS;
  145. }
  146. /***********************************************************************************************
  147. * CollisionMath::Overlap_Test -- sphere triangle overlap test *
  148. * *
  149. * INPUT: *
  150. * *
  151. * OUTPUT: *
  152. * *
  153. * WARNINGS: *
  154. * *
  155. * HISTORY: *
  156. * 4/25/2001 gth : Created. *
  157. *=============================================================================================*/
  158. CollisionMath::OverlapType
  159. CollisionMath::Overlap_Test(const SphereClass & /*sphere*/,const TriClass & /*tri*/)
  160. {
  161. WWASSERT(0); //TODO
  162. return POS;
  163. }
  164. /***********************************************************************************************
  165. * CollisionMath::Overlap_Test -- Sphere - Sphere overlap test *
  166. * *
  167. * INPUT: *
  168. * *
  169. * OUTPUT: *
  170. * *
  171. * WARNINGS: *
  172. * *
  173. * HISTORY: *
  174. * 4/25/2001 gth : Created. *
  175. *=============================================================================================*/
  176. CollisionMath::OverlapType
  177. CollisionMath::Overlap_Test(const SphereClass & sphere,const SphereClass & sphere2)
  178. {
  179. CollisionMath::OverlapType retval = OUTSIDE;
  180. float radius = sphere.Radius + sphere2.Radius;
  181. float dist2 = (sphere2.Center - sphere.Center).Length2();
  182. if (dist2 == 0 && sphere.Radius == sphere2.Radius) {
  183. retval = OVERLAPPED;
  184. } else if (dist2 <= radius * radius - COINCIDENCE_EPSILON) {
  185. retval = INSIDE;
  186. }
  187. return retval;
  188. }
  189. /***********************************************************************************************
  190. * CollisionMath::Overlap_Test -- Sphere - AABox overlap test *
  191. * *
  192. * INPUT: *
  193. * *
  194. * OUTPUT: *
  195. * *
  196. * WARNINGS: *
  197. * *
  198. * HISTORY: *
  199. * 4/25/2001 gth : Created. *
  200. *=============================================================================================*/
  201. CollisionMath::OverlapType
  202. CollisionMath::Overlap_Test(const SphereClass & sphere,const AABoxClass & aabox)
  203. {
  204. // TODO: overlap function that detects containment?
  205. return ( Intersection_Test(sphere,aabox) ? BOTH : POS );
  206. }
  207. /***********************************************************************************************
  208. * CollisionMath::Overlap_Test -- Sphere - OBBox overlap test *
  209. * *
  210. * INPUT: *
  211. * *
  212. * OUTPUT: *
  213. * *
  214. * WARNINGS: *
  215. * *
  216. * HISTORY: *
  217. * 4/25/2001 gth : Created. *
  218. *=============================================================================================*/
  219. CollisionMath::OverlapType
  220. CollisionMath::Overlap_Test(const SphereClass & sphere,const OBBoxClass & obbox)
  221. {
  222. // TODO: overlap function that detects containment?
  223. return ( Intersection_Test(sphere,obbox) ? BOTH : POS );
  224. }