coltest.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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:: /Commando/Code/ww3d2/coltest.cpp $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 5/07/01 10:26a $*
  29. * *
  30. * $Revision:: 2 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "coltest.h"
  36. AABoxCollisionTestClass::AABoxCollisionTestClass(const AABoxCollisionTestClass & that) :
  37. CollisionTestClass(that),
  38. Box(that.Box),
  39. Move(that.Move),
  40. SweepMin(that.SweepMin),
  41. SweepMax(that.SweepMax)
  42. {
  43. }
  44. AABoxCollisionTestClass::AABoxCollisionTestClass(const AABoxClass & aabox,const Vector3 & move,CastResultStruct * res,int collision_type) :
  45. CollisionTestClass(res,collision_type),
  46. Box(aabox),
  47. Move(move)
  48. {
  49. SweepMin = Box.Center - Box.Extent;
  50. SweepMax = Box.Center + Box.Extent;
  51. Vector3 endmin = Box.Center + move - Box.Extent;
  52. Vector3 endmax = Box.Center + move + Box.Extent;
  53. if (endmax.X > SweepMax.X) SweepMax.X = endmax.X;
  54. if (endmax.Y > SweepMax.Y) SweepMax.Y = endmax.Y;
  55. if (endmax.Z > SweepMax.Z) SweepMax.Z = endmax.Z;
  56. if (endmin.X < SweepMin.X) SweepMin.X = endmin.X;
  57. if (endmin.Y < SweepMin.Y) SweepMin.Y = endmin.Y;
  58. if (endmin.Z < SweepMin.Z) SweepMin.Z = endmin.Z;
  59. }
  60. bool AABoxCollisionTestClass::Cull(const AABoxClass & box)
  61. {
  62. // const float MOVE_THRESHOLD = 2.0f;
  63. // if (WWMath::Fabs(Move.X) + WWMath::Fabs(Move.Y) + WWMath::Fabs(Move.Z) > MOVE_THRESHOLD) {
  64. // CastResultStruct res;
  65. // return !Box.Cast_To_Box(Move,box,&res);
  66. // } else {
  67. Vector3 min_corner;
  68. Vector3 max_corner;
  69. Vector3::Subtract(box.Center,box.Extent,&min_corner);
  70. Vector3::Add(box.Center,box.Extent,&max_corner);
  71. if ((SweepMin.X > max_corner.X) || (SweepMax.X < min_corner.X)) {
  72. return true;
  73. }
  74. if ((SweepMin.Y > max_corner.Y) || (SweepMax.Y < min_corner.Y)) {
  75. return true;
  76. }
  77. if ((SweepMin.Z > max_corner.Z) || (SweepMax.Z < min_corner.Z)) {
  78. return true;
  79. }
  80. return false;
  81. // }
  82. }
  83. void AABoxCollisionTestClass::Rotate(ROTATION_TYPE rotation)
  84. {
  85. #ifndef NDEBUG
  86. int i;
  87. Matrix3D tm(1);
  88. switch(rotation) {
  89. case ROTATE_NONE:
  90. break;
  91. case ROTATE_Z90:
  92. tm = Matrix3D::RotateZ90;
  93. break;
  94. case ROTATE_Z180:
  95. tm = Matrix3D::RotateZ180;
  96. break;
  97. case ROTATE_Z270:
  98. tm = Matrix3D::RotateZ270;
  99. break;
  100. }
  101. #ifdef ALLOW_TEMPORARIES
  102. Vector3 realcenter = tm * Box.Center;
  103. #else
  104. Vector3 realcenter;
  105. tm.mulVector3(Box.Center, realcenter);
  106. #endif
  107. Vector3 pts[8];
  108. Vector3 & min = SweepMin;
  109. Vector3 & max = SweepMax;
  110. pts[0].Set(min.X,min.Y,min.Z);
  111. pts[1].Set(min.X,max.Y,min.Z);
  112. pts[2].Set(max.X,max.Y,min.Z);
  113. pts[3].Set(max.X,min.Y,min.Z);
  114. pts[4].Set(min.X,min.Y,max.Z);
  115. pts[5].Set(min.X,max.Y,max.Z);
  116. pts[6].Set(max.X,max.Y,max.Z);
  117. pts[7].Set(max.X,min.Y,max.Z);
  118. // for (i=0; i<8; i++) {
  119. // pts[i] = tm * pts[i];
  120. // }
  121. tm.mulVector3Array(pts, 8);
  122. Vector3 realmin = pts[0];
  123. Vector3 realmax = pts[0];
  124. for (i=1; i<8; i++) {
  125. if (realmin.X >= pts[i].X) realmin.X = pts[i].X;
  126. if (realmin.Y >= pts[i].Y) realmin.Y = pts[i].Y;
  127. if (realmin.Z >= pts[i].Z) realmin.Z = pts[i].Z;
  128. if (realmax.X <= pts[i].X) realmax.X = pts[i].X;
  129. if (realmax.Y <= pts[i].Y) realmax.Y = pts[i].Y;
  130. if (realmax.Z <= pts[i].Z) realmax.Z = pts[i].Z;
  131. }
  132. #endif
  133. // rotate the test by the desired rotation about the Z axis, special cased for
  134. // 90 degree rotations about Z. arbitrary rotations cause the axis aligned
  135. // box to not be aligned any more :-)
  136. float tmp,minx,miny,maxx,maxy;
  137. switch(rotation) {
  138. case ROTATE_NONE:
  139. break;
  140. case ROTATE_Z90:
  141. // rotate the center point and the move vector
  142. tmp = Box.Center.X; Box.Center.X = -Box.Center.Y; Box.Center.Y = tmp;
  143. tmp = Move.X; Move.X = -Move.Y; Move.Y = tmp;
  144. // swap x and y for the extent
  145. tmp = Box.Extent.X; Box.Extent.X = Box.Extent.Y; Box.Extent.Y = tmp;
  146. // update sweep bounding box
  147. minx = SweepMin.X; miny = SweepMin.Y; maxx = SweepMax.X; maxy = SweepMax.Y;
  148. SweepMin.X = -maxy;
  149. SweepMin.Y = minx;
  150. SweepMax.X = -miny;
  151. SweepMax.Y = maxx;
  152. break;
  153. case ROTATE_Z180:
  154. // rotate center and move vector
  155. Box.Center.X = -Box.Center.X;
  156. Box.Center.Y = -Box.Center.Y;
  157. Move.X = -Move.X;
  158. Move.Y = -Move.Y;
  159. // update min/max boxes
  160. minx = SweepMin.X; miny = SweepMin.Y; maxx = SweepMax.X; maxy = SweepMax.Y;
  161. SweepMin.X = -maxx;
  162. SweepMin.Y = -maxy;
  163. SweepMax.X = -minx;
  164. SweepMax.Y = -miny;
  165. break;
  166. case ROTATE_Z270:
  167. // rotate center and move.
  168. tmp = Box.Center.X; Box.Center.X = Box.Center.Y; Box.Center.Y = -tmp;
  169. tmp = Move.X; Move.X = Move.Y; Move.Y = -tmp;
  170. // update extent (x and y axis swap)
  171. tmp = Box.Extent.X; Box.Extent.X = Box.Extent.Y; Box.Extent.Y = tmp;
  172. // update min/max boxes
  173. minx = SweepMin.X; miny = SweepMin.Y; maxx = SweepMax.X; maxy = SweepMax.Y;
  174. SweepMin.X = miny;
  175. SweepMin.Y = -maxx;
  176. SweepMax.X = maxy;
  177. SweepMax.Y = -minx;
  178. break;
  179. }
  180. #ifndef NDEBUG
  181. assert((Box.Center - realcenter).Length() < 0.001f);
  182. assert((SweepMin - realmin).Length() < 0.001f);
  183. assert((SweepMax - realmax).Length() < 0.001f);
  184. #endif
  185. }
  186. void AABoxCollisionTestClass::Transform(const Matrix3D & tm)
  187. {
  188. // NOTE: this function will expand the box to enclose the rotated
  189. // form of the original box. In practice, this function was only
  190. // implemented to double-check the results of the Translate and Rotate
  191. // functions.
  192. int i;
  193. Vector3 tmpcenter = Box.Center;
  194. Vector3 tmpextent = Box.Extent;
  195. tm.Transform_Center_Extent_AABox(tmpcenter,tmpextent,&Box.Center,&Box.Extent);
  196. Move = tm.Rotate_Vector(Move);
  197. Vector3 pts[8];
  198. Vector3 & min = SweepMin;
  199. Vector3 & max = SweepMax;
  200. pts[0].Set(min.X,min.Y,min.Z);
  201. pts[1].Set(min.X,max.Y,min.Z);
  202. pts[2].Set(max.X,max.Y,min.Z);
  203. pts[3].Set(max.X,min.Y,min.Z);
  204. pts[4].Set(min.X,min.Y,max.Z);
  205. pts[5].Set(min.X,max.Y,max.Z);
  206. pts[6].Set(max.X,max.Y,max.Z);
  207. pts[7].Set(max.X,min.Y,max.Z);
  208. // for (i=0; i<8; i++) {
  209. // pts[i] = tm * pts[i];
  210. // }
  211. tm.mulVector3Array(pts, 8);
  212. Vector3 realmin = pts[0];
  213. Vector3 realmax = pts[0];
  214. for (i=1; i<8; i++) {
  215. if (realmin.X >= pts[i].X) realmin.X = pts[i].X;
  216. if (realmin.Y >= pts[i].Y) realmin.Y = pts[i].Y;
  217. if (realmin.Z >= pts[i].Z) realmin.Z = pts[i].Z;
  218. if (realmax.X <= pts[i].X) realmax.X = pts[i].X;
  219. if (realmax.Y <= pts[i].Y) realmax.Y = pts[i].Y;
  220. if (realmax.Z <= pts[i].Z) realmax.Z = pts[i].Z;
  221. }
  222. SweepMin = realmin;
  223. SweepMax = realmax;
  224. }
  225. OBBoxCollisionTestClass::OBBoxCollisionTestClass
  226. (
  227. const OBBoxClass & obbox,
  228. const Vector3 & move,
  229. CastResultStruct * res,
  230. int type
  231. ) :
  232. CollisionTestClass(res,type),
  233. Box(obbox),
  234. Move(move)
  235. {
  236. Vector3 max_extent;
  237. max_extent.X = WWMath::Fabs(Box.Basis[0][0] * Box.Extent.X) +
  238. WWMath::Fabs(Box.Basis[0][1] * Box.Extent.Y) +
  239. WWMath::Fabs(Box.Basis[0][2] * Box.Extent.Z) + 0.01f;
  240. max_extent.Y = WWMath::Fabs(Box.Basis[1][0] * Box.Extent.X) +
  241. WWMath::Fabs(Box.Basis[1][1] * Box.Extent.Y) +
  242. WWMath::Fabs(Box.Basis[1][2] * Box.Extent.Z) + 0.01f;
  243. max_extent.Z = WWMath::Fabs(Box.Basis[2][0] * Box.Extent.X) +
  244. WWMath::Fabs(Box.Basis[2][1] * Box.Extent.Y) +
  245. WWMath::Fabs(Box.Basis[2][2] * Box.Extent.Z) + 0.01f;
  246. SweepMin = Box.Center - max_extent;
  247. SweepMax = Box.Center + max_extent;
  248. Vector3 endmin = Box.Center + move - max_extent;
  249. Vector3 endmax = Box.Center + move + max_extent;
  250. if (endmax.X > SweepMax.X) SweepMax.X = endmax.X;
  251. if (endmax.Y > SweepMax.Y) SweepMax.Y = endmax.Y;
  252. if (endmax.Z > SweepMax.Z) SweepMax.Z = endmax.Z;
  253. if (endmin.X < SweepMin.X) SweepMin.X = endmin.X;
  254. if (endmin.Y < SweepMin.Y) SweepMin.Y = endmin.Y;
  255. if (endmin.Z < SweepMin.Z) SweepMin.Z = endmin.Z;
  256. }
  257. OBBoxCollisionTestClass::OBBoxCollisionTestClass(const OBBoxCollisionTestClass & that) :
  258. CollisionTestClass(that),
  259. Box(that.Box),
  260. Move(that.Move),
  261. SweepMin(that.SweepMin),
  262. SweepMax(that.SweepMax)
  263. {
  264. }
  265. OBBoxCollisionTestClass::OBBoxCollisionTestClass
  266. (
  267. const OBBoxCollisionTestClass & that,
  268. const Matrix3D & tm
  269. ) :
  270. CollisionTestClass(that)
  271. {
  272. tm.Transform_Min_Max_AABox(that.SweepMin,that.SweepMax,&SweepMin,&SweepMax);
  273. Matrix3D::Rotate_Vector(tm,that.Move,&Move);
  274. OBBoxClass::Transform(tm,that.Box,&Box);
  275. }
  276. OBBoxCollisionTestClass::OBBoxCollisionTestClass
  277. (
  278. const AABoxCollisionTestClass & that,
  279. const Matrix3D & tm
  280. ) :
  281. CollisionTestClass(that)
  282. {
  283. tm.Transform_Min_Max_AABox(that.SweepMin,that.SweepMax,&SweepMin,&SweepMax);
  284. Matrix3D::Rotate_Vector(tm,that.Move,&Move);
  285. Matrix3D::Transform_Vector(tm,that.Box.Center,&(Box.Center));
  286. Box.Extent = that.Box.Extent;
  287. Box.Basis = tm; // copies the 3x3 rotation portion of the transform
  288. }
  289. bool OBBoxCollisionTestClass::Cull(const AABoxClass & box)
  290. {
  291. Vector3 min_corner;
  292. Vector3 max_corner;
  293. Vector3::Subtract(box.Center,box.Extent,&min_corner);
  294. Vector3::Add(box.Center,box.Extent,&max_corner);
  295. if ((SweepMin.X > max_corner.X) || (SweepMax.X < min_corner.X)) {
  296. return true;
  297. }
  298. if ((SweepMin.Y > max_corner.Y) || (SweepMax.Y < min_corner.Y)) {
  299. return true;
  300. }
  301. if ((SweepMin.Z > max_corner.Z) || (SweepMax.Z < min_corner.Z)) {
  302. return true;
  303. }
  304. return false;
  305. }