colmathobbox.cpp 5.3 KB

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