coltest.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 : WW3D *
  23. * *
  24. * $Archive:: /VSS_Sync/ww3d2/coltest.h $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 9/12/01 10:01p $*
  29. * *
  30. * $Revision:: 4 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if defined(_MSC_VER)
  36. #pragma once
  37. #endif
  38. #ifndef COLTEST_H
  39. #define COLTEST_H
  40. #include "always.h"
  41. #include "castres.h"
  42. #include "lineseg.h"
  43. #include "aabox.h"
  44. #include "obbox.h"
  45. #include "tri.h"
  46. #include "colmath.h"
  47. #include "coltype.h"
  48. class RenderObjClass;
  49. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  50. // CollisionTestClass
  51. //
  52. // Each type of collision test will have an associated class which
  53. // ties together all of the information necessary for the test.
  54. // These classes also provide a perfect place to add any information
  55. // which can be pre-calculated.
  56. //
  57. // The base class: CollisionTestClass simply contains a pointer to
  58. // the user's CastResultStruct which will contain the results of
  59. // the collision test. I store a pointer to a result structure
  60. // because in some cases, new CollisionTestClasses are created in
  61. // the process of checking (e.g. if the test needs to be transformed
  62. // into another coordinate system) and I did not want to be
  63. // constantly copying the result structure around. So, the user
  64. // must allocate a result structure (usually on the stack) and keep it
  65. // until the CollisionTestClass is discarded.
  66. //
  67. // Every CollisionTestClass should have the following functions:
  68. //
  69. // bool Cull(const Vector3 & min,const Vector3 & max);
  70. // bool Cull(const AABoxClass & box);
  71. // bool Cast_To_Triangle(const TriClass & tri);
  72. //
  73. // These are not virtual because I don't want to pay the price of virtual function
  74. // calls at the point in the code where these are used. It may be possible to
  75. // write template functions if we use these exact function prototpyes for all
  76. // collision test classes though.
  77. //
  78. //
  79. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  80. class CollisionTestClass
  81. {
  82. public:
  83. CollisionTestClass(CastResultStruct * res,int collision_type);
  84. CollisionTestClass(const CollisionTestClass & that);
  85. public:
  86. CastResultStruct * Result;
  87. int CollisionType;
  88. RenderObjClass * CollidedRenderObj;
  89. };
  90. inline CollisionTestClass::CollisionTestClass(CastResultStruct * res,int collision_type) :
  91. Result(res),
  92. CollisionType(collision_type),
  93. CollidedRenderObj(NULL)
  94. {
  95. }
  96. inline CollisionTestClass::CollisionTestClass(const CollisionTestClass & that) :
  97. Result(that.Result),
  98. CollisionType(that.CollisionType),
  99. CollidedRenderObj(that.CollidedRenderObj)
  100. {
  101. }
  102. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  103. // RayCollisionTestClass
  104. //
  105. // Contains a linesegment to be tested and of course the inherited
  106. // pointer to a CollisionStruct for the result
  107. //
  108. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  109. class RayCollisionTestClass : public CollisionTestClass
  110. {
  111. public:
  112. RayCollisionTestClass(const LineSegClass & ray,CastResultStruct * res,int collision_type = COLLISION_TYPE_0,bool ignore_translucent_meshes=false);
  113. RayCollisionTestClass(const RayCollisionTestClass & raytest,const Matrix3D & tm);
  114. bool Cull(const Vector3 & min,const Vector3 & max);
  115. bool Cull(const AABoxClass & box);
  116. bool Cast_To_Triangle(const TriClass & tri);
  117. public:
  118. LineSegClass Ray;
  119. bool IgnoreTranslucentMeshes;
  120. private:
  121. // not implemented
  122. RayCollisionTestClass(const RayCollisionTestClass &);
  123. RayCollisionTestClass & operator = (const RayCollisionTestClass &);
  124. };
  125. inline RayCollisionTestClass::RayCollisionTestClass(const LineSegClass & ray,CastResultStruct * res,int collision_type,bool ignore_translucent_meshes) :
  126. CollisionTestClass(res,collision_type),
  127. Ray(ray),
  128. IgnoreTranslucentMeshes(ignore_translucent_meshes)
  129. {
  130. }
  131. inline RayCollisionTestClass::RayCollisionTestClass(const RayCollisionTestClass & raytest,const Matrix3D & tm) :
  132. CollisionTestClass(raytest),
  133. Ray(raytest.Ray,tm),
  134. IgnoreTranslucentMeshes(raytest.IgnoreTranslucentMeshes)
  135. {
  136. }
  137. inline bool RayCollisionTestClass::Cull(const Vector3 & min,const Vector3 & max)
  138. {
  139. return (CollisionMath::Overlap_Test(min,max,Ray) == CollisionMath::POS);
  140. }
  141. inline bool RayCollisionTestClass::Cull(const AABoxClass & box)
  142. {
  143. return (CollisionMath::Overlap_Test(box,Ray) == CollisionMath::POS);
  144. }
  145. inline bool RayCollisionTestClass::Cast_To_Triangle(const TriClass & tri)
  146. {
  147. return CollisionMath::Collide(Ray,tri,Result);
  148. }
  149. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  150. // AABoxCollisionTestClass
  151. //
  152. // Contains an Axis Aligned Box and a movement vector to be tested
  153. // for collision. Also adds Min and Max vectors both for the initial
  154. // box and for the volume swept out by the box.
  155. //
  156. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  157. class AABoxCollisionTestClass : public CollisionTestClass
  158. {
  159. public:
  160. AABoxCollisionTestClass(const AABoxClass & aabox,const Vector3 & move,CastResultStruct * res,int collision_type = COLLISION_TYPE_0);
  161. AABoxCollisionTestClass(const AABoxCollisionTestClass & that);
  162. enum ROTATION_TYPE
  163. {
  164. ROTATE_NONE = 0,
  165. ROTATE_Z90,
  166. ROTATE_Z180,
  167. ROTATE_Z270
  168. };
  169. bool Cull(const Vector3 & min,const Vector3 & max);
  170. bool Cull(const AABoxClass & box);
  171. bool Cast_To_Triangle(const TriClass & tri);
  172. void Translate(const Vector3 & translation);
  173. void Rotate(ROTATION_TYPE rotation);
  174. void Transform(const Matrix3D & tm);
  175. public:
  176. AABoxClass Box;
  177. Vector3 Move;
  178. Vector3 SweepMin;
  179. Vector3 SweepMax;
  180. private:
  181. // not implemented
  182. AABoxCollisionTestClass & operator = (const AABoxCollisionTestClass &);
  183. };
  184. inline void AABoxCollisionTestClass::Translate(const Vector3 & translation)
  185. {
  186. // translate the test by the desired translation vector
  187. Box.Center += translation;
  188. SweepMin += translation;
  189. SweepMax += translation;
  190. }
  191. inline bool AABoxCollisionTestClass::Cull(const Vector3 & min,const Vector3 & max)
  192. {
  193. if ((SweepMin.X > max.X) || (SweepMax.X < min.X)) {
  194. return true;
  195. }
  196. if ((SweepMin.Y > max.Y) || (SweepMax.Y < min.Y)) {
  197. return true;
  198. }
  199. if ((SweepMin.Z > max.Z) || (SweepMax.Z < min.Z)) {
  200. return true;
  201. }
  202. return false;
  203. }
  204. inline bool AABoxCollisionTestClass::Cast_To_Triangle(const TriClass & tri)
  205. {
  206. return CollisionMath::Collide(Box,Move,tri,Result);
  207. }
  208. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  209. // OBBoxCollisionTestClass
  210. //
  211. // Contains an Oriented Bounding Box and a movement vector to be tested
  212. // for collision. Also adds Min and Max vectors (axis aligned box)
  213. // both for the initial box and for the volume swept out by the box.
  214. //
  215. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  216. class OBBoxCollisionTestClass : public CollisionTestClass
  217. {
  218. public:
  219. OBBoxCollisionTestClass(const OBBoxClass & obbox,const Vector3 & move,CastResultStruct * res,int type = COLLISION_TYPE_0);
  220. OBBoxCollisionTestClass(const OBBoxCollisionTestClass & that);
  221. OBBoxCollisionTestClass(const OBBoxCollisionTestClass & that,const Matrix3D & tm);
  222. OBBoxCollisionTestClass(const AABoxCollisionTestClass & that,const Matrix3D & tm);
  223. bool Cull(const Vector3 & min,const Vector3 & max);
  224. bool Cull(const AABoxClass & box);
  225. bool Cast_To_Triangle(const TriClass & tri);
  226. public:
  227. OBBoxClass Box;
  228. Vector3 Move;
  229. Vector3 SweepMin;
  230. Vector3 SweepMax;
  231. private:
  232. // not implemented
  233. OBBoxCollisionTestClass & operator = (const OBBoxCollisionTestClass &);
  234. };
  235. inline bool OBBoxCollisionTestClass::Cull(const Vector3 & min,const Vector3 & max)
  236. {
  237. if ((SweepMin.X > max.X) || (SweepMax.X < min.X)) {
  238. return true;
  239. }
  240. if ((SweepMin.Y > max.Y) || (SweepMax.Y < min.Y)) {
  241. return true;
  242. }
  243. if ((SweepMin.Z > max.Z) || (SweepMax.Z < min.Z)) {
  244. return true;
  245. }
  246. return false;
  247. }
  248. inline bool OBBoxCollisionTestClass::Cast_To_Triangle(const TriClass & tri)
  249. {
  250. return CollisionMath::Collide(Box,Move,tri,Vector3(0,0,0),Result);
  251. }
  252. #endif