coltest.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 : WW3D *
  23. * *
  24. * $Archive:: /VSS_Sync/ww3d2/coltest.h $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 8/29/01 7:29p $*
  29. * *
  30. * $Revision:: 3 $*
  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 check_translucent=false, bool check_hidden=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 CheckTranslucent;
  120. bool CheckHidden;
  121. private:
  122. // not implemented
  123. RayCollisionTestClass(const RayCollisionTestClass &);
  124. RayCollisionTestClass & operator = (const RayCollisionTestClass &);
  125. };
  126. inline RayCollisionTestClass::RayCollisionTestClass(const LineSegClass & ray,CastResultStruct * res,int collision_type,bool check_translucent, bool check_hidden) :
  127. CollisionTestClass(res,collision_type),
  128. Ray(ray),
  129. CheckTranslucent(check_translucent),
  130. CheckHidden(check_hidden)
  131. {
  132. }
  133. inline RayCollisionTestClass::RayCollisionTestClass(const RayCollisionTestClass & raytest,const Matrix3D & tm) :
  134. CollisionTestClass(raytest),
  135. Ray(raytest.Ray,tm),
  136. CheckTranslucent(raytest.CheckTranslucent),
  137. CheckHidden(raytest.CheckHidden)
  138. {
  139. }
  140. inline bool RayCollisionTestClass::Cull(const Vector3 & min,const Vector3 & max)
  141. {
  142. return (CollisionMath::Overlap_Test(min,max,Ray) == CollisionMath::POS);
  143. }
  144. inline bool RayCollisionTestClass::Cull(const AABoxClass & box)
  145. {
  146. return (CollisionMath::Overlap_Test(box,Ray) == CollisionMath::POS);
  147. }
  148. inline bool RayCollisionTestClass::Cast_To_Triangle(const TriClass & tri)
  149. {
  150. return CollisionMath::Collide(Ray,tri,Result);
  151. }
  152. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  153. // AABoxCollisionTestClass
  154. //
  155. // Contains an Axis Aligned Box and a movement vector to be tested
  156. // for collision. Also adds Min and Max vectors both for the initial
  157. // box and for the volume swept out by the box.
  158. //
  159. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  160. class AABoxCollisionTestClass : public CollisionTestClass
  161. {
  162. public:
  163. AABoxCollisionTestClass(const AABoxClass & aabox,const Vector3 & move,CastResultStruct * res,int collision_type = COLLISION_TYPE_0);
  164. AABoxCollisionTestClass(const AABoxCollisionTestClass & that);
  165. enum ROTATION_TYPE
  166. {
  167. ROTATE_NONE = 0,
  168. ROTATE_Z90,
  169. ROTATE_Z180,
  170. ROTATE_Z270
  171. };
  172. bool Cull(const Vector3 & min,const Vector3 & max);
  173. bool Cull(const AABoxClass & box);
  174. bool Cast_To_Triangle(const TriClass & tri);
  175. void Translate(const Vector3 & translation);
  176. void Rotate(ROTATION_TYPE rotation);
  177. void Transform(const Matrix3D & tm);
  178. public:
  179. AABoxClass Box;
  180. Vector3 Move;
  181. Vector3 SweepMin;
  182. Vector3 SweepMax;
  183. private:
  184. // not implemented
  185. AABoxCollisionTestClass & operator = (const AABoxCollisionTestClass &);
  186. };
  187. inline void AABoxCollisionTestClass::Translate(const Vector3 & translation)
  188. {
  189. // translate the test by the desired translation vector
  190. Box.Center += translation;
  191. SweepMin += translation;
  192. SweepMax += translation;
  193. }
  194. inline bool AABoxCollisionTestClass::Cull(const Vector3 & min,const Vector3 & max)
  195. {
  196. if ((SweepMin.X > max.X) || (SweepMax.X < min.X)) {
  197. return true;
  198. }
  199. if ((SweepMin.Y > max.Y) || (SweepMax.Y < min.Y)) {
  200. return true;
  201. }
  202. if ((SweepMin.Z > max.Z) || (SweepMax.Z < min.Z)) {
  203. return true;
  204. }
  205. return false;
  206. }
  207. inline bool AABoxCollisionTestClass::Cast_To_Triangle(const TriClass & tri)
  208. {
  209. return CollisionMath::Collide(Box,Move,tri,Result);
  210. }
  211. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  212. // OBBoxCollisionTestClass
  213. //
  214. // Contains an Oriented Bounding Box and a movement vector to be tested
  215. // for collision. Also adds Min and Max vectors (axis aligned box)
  216. // both for the initial box and for the volume swept out by the box.
  217. //
  218. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  219. class OBBoxCollisionTestClass : public CollisionTestClass
  220. {
  221. public:
  222. OBBoxCollisionTestClass(const OBBoxClass & obbox,const Vector3 & move,CastResultStruct * res,int type = COLLISION_TYPE_0);
  223. OBBoxCollisionTestClass(const OBBoxCollisionTestClass & that);
  224. OBBoxCollisionTestClass(const OBBoxCollisionTestClass & that,const Matrix3D & tm);
  225. OBBoxCollisionTestClass(const AABoxCollisionTestClass & that,const Matrix3D & tm);
  226. bool Cull(const Vector3 & min,const Vector3 & max);
  227. bool Cull(const AABoxClass & box);
  228. bool Cast_To_Triangle(const TriClass & tri);
  229. public:
  230. OBBoxClass Box;
  231. Vector3 Move;
  232. Vector3 SweepMin;
  233. Vector3 SweepMax;
  234. private:
  235. // not implemented
  236. OBBoxCollisionTestClass & operator = (const OBBoxCollisionTestClass &);
  237. };
  238. inline bool OBBoxCollisionTestClass::Cull(const Vector3 & min,const Vector3 & max)
  239. {
  240. if ((SweepMin.X > max.X) || (SweepMax.X < min.X)) {
  241. return true;
  242. }
  243. if ((SweepMin.Y > max.Y) || (SweepMax.Y < min.Y)) {
  244. return true;
  245. }
  246. if ((SweepMin.Z > max.Z) || (SweepMax.Z < min.Z)) {
  247. return true;
  248. }
  249. return false;
  250. }
  251. inline bool OBBoxCollisionTestClass::Cast_To_Triangle(const TriClass & tri)
  252. {
  253. return CollisionMath::Collide(Box,Move,tri,Vector3(0,0,0),Result);
  254. }
  255. #endif