coltest.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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:: /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. Vector3 realcenter = tm * Box.Center;
  102. Vector3 pts[8];
  103. Vector3 & min = SweepMin;
  104. Vector3 & max = SweepMax;
  105. pts[0].Set(min.X,min.Y,min.Z);
  106. pts[1].Set(min.X,max.Y,min.Z);
  107. pts[2].Set(max.X,max.Y,min.Z);
  108. pts[3].Set(max.X,min.Y,min.Z);
  109. pts[4].Set(min.X,min.Y,max.Z);
  110. pts[5].Set(min.X,max.Y,max.Z);
  111. pts[6].Set(max.X,max.Y,max.Z);
  112. pts[7].Set(max.X,min.Y,max.Z);
  113. for (i=0; i<8; i++) {
  114. pts[i] = tm * pts[i];
  115. }
  116. Vector3 realmin = pts[0];
  117. Vector3 realmax = pts[0];
  118. for (i=1; i<8; i++) {
  119. if (realmin.X >= pts[i].X) realmin.X = pts[i].X;
  120. if (realmin.Y >= pts[i].Y) realmin.Y = pts[i].Y;
  121. if (realmin.Z >= pts[i].Z) realmin.Z = pts[i].Z;
  122. if (realmax.X <= pts[i].X) realmax.X = pts[i].X;
  123. if (realmax.Y <= pts[i].Y) realmax.Y = pts[i].Y;
  124. if (realmax.Z <= pts[i].Z) realmax.Z = pts[i].Z;
  125. }
  126. #endif
  127. // rotate the test by the desired rotation about the Z axis, special cased for
  128. // 90 degree rotations about Z. arbitrary rotations cause the axis aligned
  129. // box to not be aligned any more :-)
  130. float tmp,minx,miny,maxx,maxy;
  131. switch(rotation) {
  132. case ROTATE_NONE:
  133. break;
  134. case ROTATE_Z90:
  135. // rotate the center point and the move vector
  136. tmp = Box.Center.X; Box.Center.X = -Box.Center.Y; Box.Center.Y = tmp;
  137. tmp = Move.X; Move.X = -Move.Y; Move.Y = tmp;
  138. // swap x and y for the extent
  139. tmp = Box.Extent.X; Box.Extent.X = Box.Extent.Y; Box.Extent.Y = tmp;
  140. // update sweep bounding box
  141. minx = SweepMin.X; miny = SweepMin.Y; maxx = SweepMax.X; maxy = SweepMax.Y;
  142. SweepMin.X = -maxy;
  143. SweepMin.Y = minx;
  144. SweepMax.X = -miny;
  145. SweepMax.Y = maxx;
  146. break;
  147. case ROTATE_Z180:
  148. // rotate center and move vector
  149. Box.Center.X = -Box.Center.X;
  150. Box.Center.Y = -Box.Center.Y;
  151. Move.X = -Move.X;
  152. Move.Y = -Move.Y;
  153. // update min/max boxes
  154. minx = SweepMin.X; miny = SweepMin.Y; maxx = SweepMax.X; maxy = SweepMax.Y;
  155. SweepMin.X = -maxx;
  156. SweepMin.Y = -maxy;
  157. SweepMax.X = -minx;
  158. SweepMax.Y = -miny;
  159. break;
  160. case ROTATE_Z270:
  161. // rotate center and move.
  162. tmp = Box.Center.X; Box.Center.X = Box.Center.Y; Box.Center.Y = -tmp;
  163. tmp = Move.X; Move.X = Move.Y; Move.Y = -tmp;
  164. // update extent (x and y axis swap)
  165. tmp = Box.Extent.X; Box.Extent.X = Box.Extent.Y; Box.Extent.Y = tmp;
  166. // update min/max boxes
  167. minx = SweepMin.X; miny = SweepMin.Y; maxx = SweepMax.X; maxy = SweepMax.Y;
  168. SweepMin.X = miny;
  169. SweepMin.Y = -maxx;
  170. SweepMax.X = maxy;
  171. SweepMax.Y = -minx;
  172. break;
  173. }
  174. #ifndef NDEBUG
  175. assert((Box.Center - realcenter).Length() < 0.001f);
  176. assert((SweepMin - realmin).Length() < 0.001f);
  177. assert((SweepMax - realmax).Length() < 0.001f);
  178. #endif
  179. }
  180. void AABoxCollisionTestClass::Transform(const Matrix3D & tm)
  181. {
  182. // NOTE: this function will expand the box to enclose the rotated
  183. // form of the original box. In practice, this function was only
  184. // implemented to double-check the results of the Translate and Rotate
  185. // functions.
  186. int i;
  187. Vector3 tmpcenter = Box.Center;
  188. Vector3 tmpextent = Box.Extent;
  189. tm.Transform_Center_Extent_AABox(tmpcenter,tmpextent,&Box.Center,&Box.Extent);
  190. Move = tm.Rotate_Vector(Move);
  191. Vector3 pts[8];
  192. Vector3 & min = SweepMin;
  193. Vector3 & max = SweepMax;
  194. pts[0].Set(min.X,min.Y,min.Z);
  195. pts[1].Set(min.X,max.Y,min.Z);
  196. pts[2].Set(max.X,max.Y,min.Z);
  197. pts[3].Set(max.X,min.Y,min.Z);
  198. pts[4].Set(min.X,min.Y,max.Z);
  199. pts[5].Set(min.X,max.Y,max.Z);
  200. pts[6].Set(max.X,max.Y,max.Z);
  201. pts[7].Set(max.X,min.Y,max.Z);
  202. for (i=0; i<8; i++) {
  203. pts[i] = tm * pts[i];
  204. }
  205. Vector3 realmin = pts[0];
  206. Vector3 realmax = pts[0];
  207. for (i=1; i<8; i++) {
  208. if (realmin.X >= pts[i].X) realmin.X = pts[i].X;
  209. if (realmin.Y >= pts[i].Y) realmin.Y = pts[i].Y;
  210. if (realmin.Z >= pts[i].Z) realmin.Z = pts[i].Z;
  211. if (realmax.X <= pts[i].X) realmax.X = pts[i].X;
  212. if (realmax.Y <= pts[i].Y) realmax.Y = pts[i].Y;
  213. if (realmax.Z <= pts[i].Z) realmax.Z = pts[i].Z;
  214. }
  215. SweepMin = realmin;
  216. SweepMax = realmax;
  217. }
  218. OBBoxCollisionTestClass::OBBoxCollisionTestClass
  219. (
  220. const OBBoxClass & obbox,
  221. const Vector3 & move,
  222. CastResultStruct * res,
  223. int type
  224. ) :
  225. CollisionTestClass(res,type),
  226. Box(obbox),
  227. Move(move)
  228. {
  229. Vector3 max_extent;
  230. max_extent.X = WWMath::Fabs(Box.Basis[0][0] * Box.Extent.X) +
  231. WWMath::Fabs(Box.Basis[0][1] * Box.Extent.Y) +
  232. WWMath::Fabs(Box.Basis[0][2] * Box.Extent.Z) + 0.01f;
  233. max_extent.Y = WWMath::Fabs(Box.Basis[1][0] * Box.Extent.X) +
  234. WWMath::Fabs(Box.Basis[1][1] * Box.Extent.Y) +
  235. WWMath::Fabs(Box.Basis[1][2] * Box.Extent.Z) + 0.01f;
  236. max_extent.Z = WWMath::Fabs(Box.Basis[2][0] * Box.Extent.X) +
  237. WWMath::Fabs(Box.Basis[2][1] * Box.Extent.Y) +
  238. WWMath::Fabs(Box.Basis[2][2] * Box.Extent.Z) + 0.01f;
  239. SweepMin = Box.Center - max_extent;
  240. SweepMax = Box.Center + max_extent;
  241. Vector3 endmin = Box.Center + move - max_extent;
  242. Vector3 endmax = Box.Center + move + max_extent;
  243. if (endmax.X > SweepMax.X) SweepMax.X = endmax.X;
  244. if (endmax.Y > SweepMax.Y) SweepMax.Y = endmax.Y;
  245. if (endmax.Z > SweepMax.Z) SweepMax.Z = endmax.Z;
  246. if (endmin.X < SweepMin.X) SweepMin.X = endmin.X;
  247. if (endmin.Y < SweepMin.Y) SweepMin.Y = endmin.Y;
  248. if (endmin.Z < SweepMin.Z) SweepMin.Z = endmin.Z;
  249. }
  250. OBBoxCollisionTestClass::OBBoxCollisionTestClass(const OBBoxCollisionTestClass & that) :
  251. CollisionTestClass(that),
  252. Box(that.Box),
  253. Move(that.Move),
  254. SweepMin(that.SweepMin),
  255. SweepMax(that.SweepMax)
  256. {
  257. }
  258. OBBoxCollisionTestClass::OBBoxCollisionTestClass
  259. (
  260. const OBBoxCollisionTestClass & that,
  261. const Matrix3D & tm
  262. ) :
  263. CollisionTestClass(that)
  264. {
  265. tm.Transform_Min_Max_AABox(that.SweepMin,that.SweepMax,&SweepMin,&SweepMax);
  266. Matrix3D::Rotate_Vector(tm,that.Move,&Move);
  267. OBBoxClass::Transform(tm,that.Box,&Box);
  268. }
  269. OBBoxCollisionTestClass::OBBoxCollisionTestClass
  270. (
  271. const AABoxCollisionTestClass & that,
  272. const Matrix3D & tm
  273. ) :
  274. CollisionTestClass(that)
  275. {
  276. tm.Transform_Min_Max_AABox(that.SweepMin,that.SweepMax,&SweepMin,&SweepMax);
  277. Matrix3D::Rotate_Vector(tm,that.Move,&Move);
  278. Matrix3D::Transform_Vector(tm,that.Box.Center,&(Box.Center));
  279. Box.Extent = that.Box.Extent;
  280. Box.Basis = tm; // copies the 3x3 rotation portion of the transform
  281. }
  282. bool OBBoxCollisionTestClass::Cull(const AABoxClass & box)
  283. {
  284. Vector3 min_corner;
  285. Vector3 max_corner;
  286. Vector3::Subtract(box.Center,box.Extent,&min_corner);
  287. Vector3::Add(box.Center,box.Extent,&max_corner);
  288. if ((SweepMin.X > max_corner.X) || (SweepMax.X < min_corner.X)) {
  289. return true;
  290. }
  291. if ((SweepMin.Y > max_corner.Y) || (SweepMax.Y < min_corner.Y)) {
  292. return true;
  293. }
  294. if ((SweepMin.Z > max_corner.Z) || (SweepMax.Z < min_corner.Z)) {
  295. return true;
  296. }
  297. return false;
  298. }