colmathplane.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 : WWMath *
  23. * *
  24. * $Archive:: /Commando/Code/wwmath/colmathplane.h $*
  25. * *
  26. * Original Author:: Greg Hjelstrom *
  27. * *
  28. * $Author:: Greg_h $*
  29. * *
  30. * $Modtime:: 3/29/00 4:42p $*
  31. * *
  32. * $Revision:: 2 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * get_far_extent -- gets extents of a box projected onto an axis *
  37. * CollisionMath::Overlap_Test -- Tests overlap between a plane and a point *
  38. * CollisionMath::Overlap_Test -- Tests overlap between a plane and an AABox *
  39. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  40. #ifndef COLMATHPLANE_H
  41. #define COLMATHPLANE_H
  42. #include "always.h"
  43. #include "plane.h"
  44. #include "aabox.h"
  45. /*
  46. ** Inline collision functions dealing with planes
  47. ** This module is meant to be included only in .CPP files after you include colmath.h
  48. ** It is not automatically included in order to reduce file dependencies...
  49. */
  50. /***********************************************************************************************
  51. * get_far_extent -- gets extents of a box projected onto an axis *
  52. * *
  53. * INPUT: *
  54. * *
  55. * OUTPUT: *
  56. * *
  57. * WARNINGS: *
  58. * *
  59. * HISTORY: *
  60. * 3/29/2000 gth : Created. *
  61. *=============================================================================================*/
  62. inline void get_far_extent(const Vector3 & normal,const Vector3 & extent,Vector3 * posfarpt)
  63. {
  64. if (WWMath::Fast_Is_Float_Positive(normal.X)) {
  65. posfarpt->X = extent.X;
  66. } else {
  67. posfarpt->X = -extent.X;
  68. }
  69. if (WWMath::Fast_Is_Float_Positive(normal.Y)) {
  70. posfarpt->Y = extent.Y;
  71. } else {
  72. posfarpt->Y = -extent.Y;
  73. }
  74. if (WWMath::Fast_Is_Float_Positive(normal.Z)) {
  75. posfarpt->Z = extent.Z;
  76. } else {
  77. posfarpt->Z = -extent.Z;
  78. }
  79. }
  80. /***********************************************************************************************
  81. * CollisionMath::Overlap_Test -- Tests overlap between a plane and a point *
  82. * *
  83. * INPUT: *
  84. * *
  85. * OUTPUT: *
  86. * *
  87. * WARNINGS: *
  88. * *
  89. * HISTORY: *
  90. * 3/29/2000 gth : Created. *
  91. *=============================================================================================*/
  92. inline
  93. CollisionMath::OverlapType
  94. CollisionMath::Overlap_Test(const PlaneClass & plane,const Vector3 & point)
  95. {
  96. float delta = Vector3::Dot_Product(point,plane.N) - plane.D;
  97. if (delta > COINCIDENCE_EPSILON) {
  98. return POS;
  99. }
  100. if (delta < -COINCIDENCE_EPSILON) {
  101. return NEG;
  102. }
  103. return ON;
  104. }
  105. /***********************************************************************************************
  106. * CollisionMath::Overlap_Test -- Tests overlap between a plane and an AABox *
  107. * *
  108. * INPUT: *
  109. * *
  110. * OUTPUT: *
  111. * *
  112. * WARNINGS: *
  113. * *
  114. * HISTORY: *
  115. * 3/29/2000 gth : Created. *
  116. *=============================================================================================*/
  117. inline
  118. CollisionMath::OverlapType
  119. CollisionMath::Overlap_Test(const PlaneClass & plane,const AABoxClass & box)
  120. {
  121. // First, we determine the the near and far points of the box in the
  122. // direction of the plane normal
  123. Vector3 posfarpt;
  124. Vector3 negfarpt;
  125. get_far_extent(plane.N,box.Extent,&posfarpt);
  126. negfarpt = -posfarpt;
  127. posfarpt += box.Center;
  128. negfarpt += box.Center;
  129. if (Overlap_Test(plane,negfarpt) == POS) {
  130. return POS;
  131. }
  132. if (Overlap_Test(plane,posfarpt) == NEG) {
  133. return NEG;
  134. }
  135. return BOTH;
  136. }
  137. #endif