colmathobbox.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 : WWMath *
  23. * *
  24. * $Archive:: /Commando/Code/wwmath/colmathobbox.cpp $*
  25. * *
  26. * Org Author:: Greg Hjelstrom *
  27. * *
  28. * Author : Kenny Mitchell *
  29. * *
  30. * $Modtime:: 06/26/02 4:04p $*
  31. * *
  32. * $Revision:: 9 $*
  33. * *
  34. * 06/26/02 KM Matrix name change to avoid MAX conflicts *
  35. *---------------------------------------------------------------------------------------------*
  36. * Functions: *
  37. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  38. #include "colmath.h"
  39. #include "aaplane.h"
  40. #include "plane.h"
  41. #include "lineseg.h"
  42. #include "tri.h"
  43. #include "sphere.h"
  44. #include "aabox.h"
  45. #include "obbox.h"
  46. #include "wwdebug.h"
  47. // OBBox functions, where is operand B with respect to the OBBox
  48. CollisionMath::OverlapType
  49. CollisionMath::Overlap_Test(const OBBoxClass & box,const Vector3 & point)
  50. {
  51. // transform point into box coordinate system
  52. Vector3 localpoint;
  53. Matrix3x3::Transpose_Rotate_Vector(box.Basis,(point - box.Center),&localpoint);
  54. // if the point is outside any of the extents, it is outside the box
  55. if (WWMath::Fabs(localpoint.X) > box.Extent.X) {
  56. return OUTSIDE;
  57. }
  58. if (WWMath::Fabs(localpoint.Y) > box.Extent.Y) {
  59. return OUTSIDE;
  60. }
  61. if (WWMath::Fabs(localpoint.Z) > box.Extent.Z) {
  62. return OUTSIDE;
  63. }
  64. return INSIDE;
  65. }
  66. CollisionMath::OverlapType
  67. CollisionMath::Overlap_Test(const OBBoxClass & box,const LineSegClass & line)
  68. {
  69. CastResultStruct res;
  70. Collide(line,box,&res);
  71. return eval_overlap_collision(res);
  72. }
  73. CollisionMath::OverlapType
  74. CollisionMath::Overlap_Test(const OBBoxClass & box,const TriClass & tri)
  75. {
  76. CastResultStruct res;
  77. Collide(box,Vector3(0,0,0),tri,Vector3(0,0,0),&res);
  78. return eval_overlap_collision(res);
  79. }
  80. CollisionMath::OverlapType
  81. CollisionMath::Overlap_Test(const AABoxClass & aabox,const OBBoxClass & obbox)
  82. {
  83. if (CollisionMath::Intersection_Test(aabox,obbox)) {
  84. return BOTH; // inside or overlapping
  85. } else {
  86. return OUTSIDE;
  87. }
  88. }
  89. CollisionMath::OverlapType
  90. CollisionMath::Overlap_Test(const OBBoxClass & obbox,const AABoxClass & aabox)
  91. {
  92. if (CollisionMath::Intersection_Test(obbox,aabox)) {
  93. return BOTH; // inside or overlapping
  94. } else {
  95. return OUTSIDE;
  96. }
  97. }
  98. CollisionMath::OverlapType
  99. CollisionMath::Overlap_Test(const OBBoxClass & box,const OBBoxClass & box2)
  100. {
  101. CastResultStruct res;
  102. Collide(box,Vector3(0,0,0),box2,Vector3(0,0,0),&res);
  103. return eval_overlap_collision(res);
  104. }
  105. bool CollisionMath::Collide
  106. (
  107. const OBBoxClass & box,
  108. const Vector3 & move_vector,
  109. const PlaneClass & plane,
  110. CastResultStruct * result
  111. )
  112. {
  113. float frac;
  114. float extent = box.Project_To_Axis(plane.N);
  115. float dist = Vector3::Dot_Product(plane.N,box.Center) + plane.D;
  116. float move = Vector3::Dot_Product(plane.N,move_vector);
  117. if (dist > extent) {
  118. if (dist + move > extent) {
  119. // entire move ok!
  120. frac = 1.0f;
  121. } else {
  122. // partial move allowed
  123. frac = (extent - dist) / move;
  124. }
  125. } else if (dist < -extent) {
  126. if (dist + move < -extent) {
  127. // entire move ok!
  128. frac = 1.0f;
  129. } else {
  130. // partial move allowed
  131. frac = (-extent - dist) / move;
  132. }
  133. } else {
  134. result->StartBad = true;
  135. result->Normal = plane.N;
  136. return true;
  137. }
  138. if (frac < result->Fraction) {
  139. result->Fraction = frac;
  140. result->Normal = plane.N;
  141. if (result->ComputeContactPoint) {
  142. Vector3 move_dir(move_vector);
  143. move_dir.Normalize();
  144. float move_extent = Vector3::Dot_Product(move_dir,box.Extent);
  145. result->ContactPoint = box.Center + result->Fraction*move_vector + move_extent*move_dir;
  146. }
  147. return true;
  148. }
  149. return false;
  150. }