coltest.h 11 KB

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