BroadPhaseBruteForce.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt/Jolt.h>
  4. #include <Jolt/Physics/Collision/BroadPhase/BroadPhaseBruteForce.h>
  5. #include <Jolt/Physics/Collision/RayCast.h>
  6. #include <Jolt/Physics/Collision/AABoxCast.h>
  7. #include <Jolt/Physics/Collision/CastResult.h>
  8. #include <Jolt/Physics/Body/BodyManager.h>
  9. #include <Jolt/Physics/Body/BodyPair.h>
  10. #include <Jolt/Geometry/RayAABox.h>
  11. #include <Jolt/Geometry/OrientedBox.h>
  12. #include <Jolt/Core/QuickSort.h>
  13. JPH_NAMESPACE_BEGIN
  14. void BroadPhaseBruteForce::AddBodiesFinalize(BodyID *ioBodies, int inNumber, AddState inAddState)
  15. {
  16. lock_guard lock(mMutex);
  17. BodyVector &bodies = mBodyManager->GetBodies();
  18. // Allocate space
  19. uint32 idx = (uint32)mBodyIDs.size();
  20. mBodyIDs.resize(idx + inNumber);
  21. // Add bodies
  22. for (const BodyID *b = ioBodies, *b_end = ioBodies + inNumber; b < b_end; ++b)
  23. {
  24. Body &body = *bodies[b->GetIndex()];
  25. // Validate that body ID is consistent with array index
  26. JPH_ASSERT(body.GetID() == *b);
  27. JPH_ASSERT(!body.IsInBroadPhase());
  28. // Add it to the list
  29. mBodyIDs[idx] = body.GetID();
  30. ++idx;
  31. // Indicate body is in the broadphase
  32. body.SetInBroadPhaseInternal(true);
  33. }
  34. // Resort
  35. QuickSort(mBodyIDs.begin(), mBodyIDs.end());
  36. }
  37. void BroadPhaseBruteForce::RemoveBodies(BodyID *ioBodies, int inNumber)
  38. {
  39. lock_guard lock(mMutex);
  40. BodyVector &bodies = mBodyManager->GetBodies();
  41. JPH_ASSERT((int)mBodyIDs.size() >= inNumber);
  42. // Remove bodies
  43. for (const BodyID *b = ioBodies, *b_end = ioBodies + inNumber; b < b_end; ++b)
  44. {
  45. Body &body = *bodies[b->GetIndex()];
  46. // Validate that body ID is consistent with array index
  47. JPH_ASSERT(body.GetID() == *b);
  48. JPH_ASSERT(body.IsInBroadPhase());
  49. // Find body id
  50. Array<BodyID>::iterator it = lower_bound(mBodyIDs.begin(), mBodyIDs.end(), body.GetID());
  51. JPH_ASSERT(it != mBodyIDs.end());
  52. // Remove element
  53. mBodyIDs.erase(it);
  54. // Indicate body is no longer in the broadphase
  55. body.SetInBroadPhaseInternal(false);
  56. }
  57. }
  58. void BroadPhaseBruteForce::NotifyBodiesAABBChanged(BodyID *ioBodies, int inNumber, bool inTakeLock)
  59. {
  60. // Do nothing, we directly reference the body
  61. }
  62. void BroadPhaseBruteForce::NotifyBodiesLayerChanged(BodyID * ioBodies, int inNumber)
  63. {
  64. // Do nothing, we directly reference the body
  65. }
  66. void BroadPhaseBruteForce::CastRay(const RayCast &inRay, RayCastBodyCollector &ioCollector, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter) const
  67. {
  68. shared_lock lock(mMutex);
  69. // Load ray
  70. Vec3 origin(inRay.mOrigin);
  71. RayInvDirection inv_direction(inRay.mDirection);
  72. // For all bodies
  73. float early_out_fraction = ioCollector.GetEarlyOutFraction();
  74. for (BodyID b : mBodyIDs)
  75. {
  76. const Body &body = mBodyManager->GetBody(b);
  77. // Test layer
  78. if (inObjectLayerFilter.ShouldCollide(body.GetObjectLayer()))
  79. {
  80. // Test intersection with ray
  81. const AABox &bounds = body.GetWorldSpaceBounds();
  82. float fraction = RayAABox(origin, inv_direction, bounds.mMin, bounds.mMax);
  83. if (fraction < early_out_fraction)
  84. {
  85. // Store hit
  86. BroadPhaseCastResult result { b, fraction };
  87. ioCollector.AddHit(result);
  88. if (ioCollector.ShouldEarlyOut())
  89. break;
  90. early_out_fraction = ioCollector.GetEarlyOutFraction();
  91. }
  92. }
  93. }
  94. }
  95. void BroadPhaseBruteForce::CollideAABox(const AABox &inBox, CollideShapeBodyCollector &ioCollector, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter) const
  96. {
  97. shared_lock lock(mMutex);
  98. // For all bodies
  99. for (BodyID b : mBodyIDs)
  100. {
  101. const Body &body = mBodyManager->GetBody(b);
  102. // Test layer
  103. if (inObjectLayerFilter.ShouldCollide(body.GetObjectLayer()))
  104. {
  105. // Test intersection with box
  106. const AABox &bounds = body.GetWorldSpaceBounds();
  107. if (bounds.Overlaps(inBox))
  108. {
  109. // Store hit
  110. ioCollector.AddHit(b);
  111. if (ioCollector.ShouldEarlyOut())
  112. break;
  113. }
  114. }
  115. }
  116. }
  117. void BroadPhaseBruteForce::CollideSphere(Vec3Arg inCenter, float inRadius, CollideShapeBodyCollector &ioCollector, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter) const
  118. {
  119. shared_lock lock(mMutex);
  120. float radius_sq = Square(inRadius);
  121. // For all bodies
  122. for (BodyID b : mBodyIDs)
  123. {
  124. const Body &body = mBodyManager->GetBody(b);
  125. // Test layer
  126. if (inObjectLayerFilter.ShouldCollide(body.GetObjectLayer()))
  127. {
  128. // Test intersection with box
  129. const AABox &bounds = body.GetWorldSpaceBounds();
  130. if (bounds.GetSqDistanceTo(inCenter) <= radius_sq)
  131. {
  132. // Store hit
  133. ioCollector.AddHit(b);
  134. if (ioCollector.ShouldEarlyOut())
  135. break;
  136. }
  137. }
  138. }
  139. }
  140. void BroadPhaseBruteForce::CollidePoint(Vec3Arg inPoint, CollideShapeBodyCollector &ioCollector, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter) const
  141. {
  142. shared_lock lock(mMutex);
  143. // For all bodies
  144. for (BodyID b : mBodyIDs)
  145. {
  146. const Body &body = mBodyManager->GetBody(b);
  147. // Test layer
  148. if (inObjectLayerFilter.ShouldCollide(body.GetObjectLayer()))
  149. {
  150. // Test intersection with box
  151. const AABox &bounds = body.GetWorldSpaceBounds();
  152. if (bounds.Contains(inPoint))
  153. {
  154. // Store hit
  155. ioCollector.AddHit(b);
  156. if (ioCollector.ShouldEarlyOut())
  157. break;
  158. }
  159. }
  160. }
  161. }
  162. void BroadPhaseBruteForce::CollideOrientedBox(const OrientedBox &inBox, CollideShapeBodyCollector &ioCollector, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter) const
  163. {
  164. shared_lock lock(mMutex);
  165. // For all bodies
  166. for (BodyID b : mBodyIDs)
  167. {
  168. const Body &body = mBodyManager->GetBody(b);
  169. // Test layer
  170. if (inObjectLayerFilter.ShouldCollide(body.GetObjectLayer()))
  171. {
  172. // Test intersection with box
  173. const AABox &bounds = body.GetWorldSpaceBounds();
  174. if (inBox.Overlaps(bounds))
  175. {
  176. // Store hit
  177. ioCollector.AddHit(b);
  178. if (ioCollector.ShouldEarlyOut())
  179. break;
  180. }
  181. }
  182. }
  183. }
  184. void BroadPhaseBruteForce::CastAABoxNoLock(const AABoxCast &inBox, CastShapeBodyCollector &ioCollector, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter) const
  185. {
  186. shared_lock lock(mMutex);
  187. // Load box
  188. Vec3 origin(inBox.mBox.GetCenter());
  189. Vec3 extent(inBox.mBox.GetExtent());
  190. RayInvDirection inv_direction(inBox.mDirection);
  191. // For all bodies
  192. float early_out_fraction = ioCollector.GetEarlyOutFraction();
  193. for (BodyID b : mBodyIDs)
  194. {
  195. const Body &body = mBodyManager->GetBody(b);
  196. // Test layer
  197. if (inObjectLayerFilter.ShouldCollide(body.GetObjectLayer()))
  198. {
  199. // Test intersection with ray
  200. const AABox &bounds = body.GetWorldSpaceBounds();
  201. float fraction = RayAABox(origin, inv_direction, bounds.mMin - extent, bounds.mMax + extent);
  202. if (fraction < early_out_fraction)
  203. {
  204. // Store hit
  205. BroadPhaseCastResult result { b, fraction };
  206. ioCollector.AddHit(result);
  207. if (ioCollector.ShouldEarlyOut())
  208. break;
  209. early_out_fraction = ioCollector.GetEarlyOutFraction();
  210. }
  211. }
  212. }
  213. }
  214. void BroadPhaseBruteForce::CastAABox(const AABoxCast &inBox, CastShapeBodyCollector &ioCollector, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter) const
  215. {
  216. CastAABoxNoLock(inBox, ioCollector, inBroadPhaseLayerFilter, inObjectLayerFilter);
  217. }
  218. void BroadPhaseBruteForce::FindCollidingPairs(BodyID *ioActiveBodies, int inNumActiveBodies, float inSpeculativeContactDistance, ObjectVsBroadPhaseLayerFilter inObjectVsBroadPhaseLayerFilter, ObjectLayerPairFilter inObjectLayerPairFilter, BodyPairCollector &ioPairCollector) const
  219. {
  220. shared_lock lock(mMutex);
  221. // Loop through all active bodies
  222. size_t num_bodies = mBodyIDs.size();
  223. for (int b1 = 0; b1 < inNumActiveBodies; ++b1)
  224. {
  225. BodyID b1_id = ioActiveBodies[b1];
  226. const Body &body1 = mBodyManager->GetBody(b1_id);
  227. const ObjectLayer layer1 = body1.GetObjectLayer();
  228. // Expand the bounding box by the speculative contact distance
  229. AABox bounds1 = body1.GetWorldSpaceBounds();
  230. bounds1.ExpandBy(Vec3::sReplicate(inSpeculativeContactDistance));
  231. // For all other bodies
  232. for (size_t b2 = 0; b2 < num_bodies; ++b2)
  233. {
  234. // Check if bodies can collide
  235. BodyID b2_id = mBodyIDs[b2];
  236. const Body &body2 = mBodyManager->GetBody(b2_id);
  237. if (!Body::sFindCollidingPairsCanCollide(body1, body2))
  238. continue;
  239. // Check if layers can collide
  240. const ObjectLayer layer2 = body2.GetObjectLayer();
  241. if (!inObjectLayerPairFilter(layer1, layer2))
  242. continue;
  243. // Check if bounds overlap
  244. const AABox &bounds2 = body2.GetWorldSpaceBounds();
  245. if (!bounds1.Overlaps(bounds2))
  246. continue;
  247. // Store overlapping pair
  248. ioPairCollector.AddHit({ b1_id, b2_id });
  249. }
  250. }
  251. }
  252. JPH_NAMESPACE_END