NarrowPhaseQuery.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt/Jolt.h>
  4. #include <Jolt/Physics/Collision/NarrowPhaseQuery.h>
  5. #include <Jolt/Physics/Collision/CollisionDispatch.h>
  6. #include <Jolt/Physics/Collision/RayCast.h>
  7. #include <Jolt/Physics/Collision/AABoxCast.h>
  8. #include <Jolt/Physics/Collision/ShapeCast.h>
  9. #include <Jolt/Physics/Collision/CollideShape.h>
  10. #include <Jolt/Physics/Collision/CollisionCollectorImpl.h>
  11. #include <Jolt/Physics/Collision/CastResult.h>
  12. JPH_NAMESPACE_BEGIN
  13. bool NarrowPhaseQuery::CastRay(const RayCast &inRay, RayCastResult &ioHit, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter) const
  14. {
  15. JPH_PROFILE_FUNCTION();
  16. class MyCollector : public RayCastBodyCollector
  17. {
  18. public:
  19. MyCollector(const RayCast &inRay, RayCastResult &ioHit, const BodyLockInterface &inBodyLockInterface, const BodyFilter &inBodyFilter) :
  20. mRay(inRay),
  21. mHit(ioHit),
  22. mBodyLockInterface(inBodyLockInterface),
  23. mBodyFilter(inBodyFilter)
  24. {
  25. UpdateEarlyOutFraction(ioHit.mFraction);
  26. }
  27. virtual void AddHit(const ResultType &inResult) override
  28. {
  29. JPH_ASSERT(inResult.mFraction < mHit.mFraction, "This hit should not have been passed on to the collector");
  30. // Only test shape if it passes the body filter
  31. if (mBodyFilter.ShouldCollide(inResult.mBodyID))
  32. {
  33. // Lock the body
  34. BodyLockRead lock(mBodyLockInterface, inResult.mBodyID);
  35. if (lock.Succeeded())
  36. {
  37. const Body &body = lock.GetBody();
  38. // Check body filter again now that we've locked the body
  39. if (mBodyFilter.ShouldCollideLocked(body))
  40. {
  41. // Collect the transformed shape
  42. TransformedShape ts = body.GetTransformedShape();
  43. // Release the lock now, we have all the info we need in the transformed shape
  44. lock.ReleaseLock();
  45. // Do narrow phase collision check
  46. if (ts.CastRay(mRay, mHit))
  47. {
  48. // Test that we didn't find a further hit by accident
  49. JPH_ASSERT(mHit.mFraction >= 0.0f && mHit.mFraction < GetEarlyOutFraction());
  50. // Update early out fraction based on narrow phase collector
  51. UpdateEarlyOutFraction(mHit.mFraction);
  52. }
  53. }
  54. }
  55. }
  56. }
  57. RayCast mRay;
  58. RayCastResult & mHit;
  59. const BodyLockInterface & mBodyLockInterface;
  60. const BodyFilter & mBodyFilter;
  61. };
  62. // Do broadphase test
  63. MyCollector collector(inRay, ioHit, *mBodyLockInterface, inBodyFilter);
  64. mBroadPhase->CastRay(inRay, collector, inBroadPhaseLayerFilter, inObjectLayerFilter);
  65. return ioHit.mFraction <= 1.0f;
  66. }
  67. void NarrowPhaseQuery::CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, CastRayCollector &ioCollector, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter) const
  68. {
  69. JPH_PROFILE_FUNCTION();
  70. class MyCollector : public RayCastBodyCollector
  71. {
  72. public:
  73. MyCollector(const RayCast &inRay, const RayCastSettings &inRayCastSettings, CastRayCollector &ioCollector, const BodyLockInterface &inBodyLockInterface, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter) :
  74. mRay(inRay),
  75. mRayCastSettings(inRayCastSettings),
  76. mCollector(ioCollector),
  77. mBodyLockInterface(inBodyLockInterface),
  78. mBodyFilter(inBodyFilter),
  79. mShapeFilter(inShapeFilter)
  80. {
  81. UpdateEarlyOutFraction(ioCollector.GetEarlyOutFraction());
  82. }
  83. virtual void AddHit(const ResultType &inResult) override
  84. {
  85. JPH_ASSERT(inResult.mFraction < mCollector.GetEarlyOutFraction(), "This hit should not have been passed on to the collector");
  86. // Only test shape if it passes the body filter
  87. if (mBodyFilter.ShouldCollide(inResult.mBodyID))
  88. {
  89. // Lock the body
  90. BodyLockRead lock(mBodyLockInterface, inResult.mBodyID);
  91. if (lock.Succeeded())
  92. {
  93. const Body &body = lock.GetBody();
  94. // Check body filter again now that we've locked the body
  95. if (mBodyFilter.ShouldCollideLocked(body))
  96. {
  97. // Collect the transformed shape
  98. TransformedShape ts = body.GetTransformedShape();
  99. // Notify collector of new body
  100. mCollector.OnBody(body);
  101. // Release the lock now, we have all the info we need in the transformed shape
  102. lock.ReleaseLock();
  103. // Do narrow phase collision check
  104. ts.CastRay(mRay, mRayCastSettings, mCollector, mShapeFilter);
  105. // Update early out fraction based on narrow phase collector
  106. UpdateEarlyOutFraction(mCollector.GetEarlyOutFraction());
  107. }
  108. }
  109. }
  110. }
  111. RayCast mRay;
  112. RayCastSettings mRayCastSettings;
  113. CastRayCollector & mCollector;
  114. const BodyLockInterface & mBodyLockInterface;
  115. const BodyFilter & mBodyFilter;
  116. const ShapeFilter & mShapeFilter;
  117. };
  118. // Do broadphase test
  119. MyCollector collector(inRay, inRayCastSettings, ioCollector, *mBodyLockInterface, inBodyFilter, inShapeFilter);
  120. mBroadPhase->CastRay(inRay, collector, inBroadPhaseLayerFilter, inObjectLayerFilter);
  121. }
  122. void NarrowPhaseQuery::CollidePoint(Vec3Arg inPoint, CollidePointCollector &ioCollector, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter) const
  123. {
  124. JPH_PROFILE_FUNCTION();
  125. class MyCollector : public CollideShapeBodyCollector
  126. {
  127. public:
  128. MyCollector(Vec3Arg inPoint, CollidePointCollector &ioCollector, const BodyLockInterface &inBodyLockInterface, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter) :
  129. mPoint(inPoint),
  130. mCollector(ioCollector),
  131. mBodyLockInterface(inBodyLockInterface),
  132. mBodyFilter(inBodyFilter),
  133. mShapeFilter(inShapeFilter)
  134. {
  135. }
  136. virtual void AddHit(const ResultType &inResult) override
  137. {
  138. // Only test shape if it passes the body filter
  139. if (mBodyFilter.ShouldCollide(inResult))
  140. {
  141. // Lock the body
  142. BodyLockRead lock(mBodyLockInterface, inResult);
  143. if (lock.Succeeded())
  144. {
  145. const Body &body = lock.GetBody();
  146. // Check body filter again now that we've locked the body
  147. if (mBodyFilter.ShouldCollideLocked(body))
  148. {
  149. // Collect the transformed shape
  150. TransformedShape ts = body.GetTransformedShape();
  151. // Notify collector of new body
  152. mCollector.OnBody(body);
  153. // Release the lock now, we have all the info we need in the transformed shape
  154. lock.ReleaseLock();
  155. // Do narrow phase collision check
  156. ts.CollidePoint(mPoint, mCollector, mShapeFilter);
  157. // Update early out fraction based on narrow phase collector
  158. UpdateEarlyOutFraction(mCollector.GetEarlyOutFraction());
  159. }
  160. }
  161. }
  162. }
  163. Vec3 mPoint;
  164. CollidePointCollector & mCollector;
  165. const BodyLockInterface & mBodyLockInterface;
  166. const BodyFilter & mBodyFilter;
  167. const ShapeFilter & mShapeFilter;
  168. };
  169. // Do broadphase test
  170. MyCollector collector(inPoint, ioCollector, *mBodyLockInterface, inBodyFilter, inShapeFilter);
  171. mBroadPhase->CollidePoint(inPoint, collector, inBroadPhaseLayerFilter, inObjectLayerFilter);
  172. }
  173. void NarrowPhaseQuery::CollideShape(const Shape *inShape, Vec3Arg inShapeScale, Mat44Arg inCenterOfMassTransform, const CollideShapeSettings &inCollideShapeSettings, CollideShapeCollector &ioCollector, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter) const
  174. {
  175. JPH_PROFILE_FUNCTION();
  176. class MyCollector : public CollideShapeBodyCollector
  177. {
  178. public:
  179. MyCollector(const Shape *inShape, Vec3Arg inShapeScale, Mat44Arg inCenterOfMassTransform, const CollideShapeSettings &inCollideShapeSettings, CollideShapeCollector &ioCollector, const BodyLockInterface &inBodyLockInterface, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter) :
  180. mShape(inShape),
  181. mShapeScale(inShapeScale),
  182. mCenterOfMassTransform(inCenterOfMassTransform),
  183. mCollideShapeSettings(inCollideShapeSettings),
  184. mCollector(ioCollector),
  185. mBodyLockInterface(inBodyLockInterface),
  186. mBodyFilter(inBodyFilter),
  187. mShapeFilter(inShapeFilter)
  188. {
  189. }
  190. virtual void AddHit(const ResultType &inResult) override
  191. {
  192. // Only test shape if it passes the body filter
  193. if (mBodyFilter.ShouldCollide(inResult))
  194. {
  195. // Lock the body
  196. BodyLockRead lock(mBodyLockInterface, inResult);
  197. if (lock.Succeeded())
  198. {
  199. const Body &body = lock.GetBody();
  200. // Check body filter again now that we've locked the body
  201. if (mBodyFilter.ShouldCollideLocked(body))
  202. {
  203. // Collect the transformed shape
  204. TransformedShape ts = body.GetTransformedShape();
  205. // Notify collector of new body
  206. mCollector.OnBody(body);
  207. // Release the lock now, we have all the info we need in the transformed shape
  208. lock.ReleaseLock();
  209. // Do narrow phase collision check
  210. ts.CollideShape(mShape, mShapeScale, mCenterOfMassTransform, mCollideShapeSettings, mCollector, mShapeFilter);
  211. // Update early out fraction based on narrow phase collector
  212. UpdateEarlyOutFraction(mCollector.GetEarlyOutFraction());
  213. }
  214. }
  215. }
  216. }
  217. const Shape * mShape;
  218. Vec3 mShapeScale;
  219. Mat44 mCenterOfMassTransform;
  220. const CollideShapeSettings & mCollideShapeSettings;
  221. CollideShapeCollector & mCollector;
  222. const BodyLockInterface & mBodyLockInterface;
  223. const BodyFilter & mBodyFilter;
  224. const ShapeFilter & mShapeFilter;
  225. };
  226. // Calculate bounds for shape and expand by max separation distance
  227. AABox bounds = inShape->GetWorldSpaceBounds(inCenterOfMassTransform, inShapeScale);
  228. bounds.ExpandBy(Vec3::sReplicate(inCollideShapeSettings.mMaxSeparationDistance));
  229. // Do broadphase test
  230. MyCollector collector(inShape, inShapeScale, inCenterOfMassTransform, inCollideShapeSettings, ioCollector, *mBodyLockInterface, inBodyFilter, inShapeFilter);
  231. mBroadPhase->CollideAABox(bounds, collector, inBroadPhaseLayerFilter, inObjectLayerFilter);
  232. }
  233. void NarrowPhaseQuery::CastShape(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, CastShapeCollector &ioCollector, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter) const
  234. {
  235. JPH_PROFILE_FUNCTION();
  236. class MyCollector : public CastShapeBodyCollector
  237. {
  238. private:
  239. /// Update early out fraction based on narrow phase collector
  240. inline void PropagateEarlyOutFraction()
  241. {
  242. // The CastShapeCollector uses negative values for penetration depth so we want to clamp to the smallest positive number to keep receiving deeper hits
  243. if (mCollector.ShouldEarlyOut())
  244. ForceEarlyOut();
  245. else
  246. UpdateEarlyOutFraction(max(FLT_MIN, mCollector.GetEarlyOutFraction()));
  247. }
  248. public:
  249. MyCollector(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, CastShapeCollector &ioCollector, const BodyLockInterface &inBodyLockInterface, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter) :
  250. mShapeCast(inShapeCast),
  251. mShapeCastSettings(inShapeCastSettings),
  252. mCollector(ioCollector),
  253. mBodyLockInterface(inBodyLockInterface),
  254. mBodyFilter(inBodyFilter),
  255. mShapeFilter(inShapeFilter)
  256. {
  257. PropagateEarlyOutFraction();
  258. }
  259. virtual void AddHit(const ResultType &inResult) override
  260. {
  261. JPH_ASSERT(inResult.mFraction <= max(0.0f, mCollector.GetEarlyOutFraction()), "This hit should not have been passed on to the collector");
  262. // Only test shape if it passes the body filter
  263. if (mBodyFilter.ShouldCollide(inResult.mBodyID))
  264. {
  265. // Lock the body
  266. BodyLockRead lock(mBodyLockInterface, inResult.mBodyID);
  267. if (lock.Succeeded())
  268. {
  269. const Body &body = lock.GetBody();
  270. // Check body filter again now that we've locked the body
  271. if (mBodyFilter.ShouldCollideLocked(body))
  272. {
  273. // Collect the transformed shape
  274. TransformedShape ts = body.GetTransformedShape();
  275. // Notify collector of new body
  276. mCollector.OnBody(body);
  277. // Release the lock now, we have all the info we need in the transformed shape
  278. lock.ReleaseLock();
  279. // Do narrow phase collision check
  280. ts.CastShape(mShapeCast, mShapeCastSettings, mCollector, mShapeFilter);
  281. // Update early out fraction based on narrow phase collector
  282. PropagateEarlyOutFraction();
  283. }
  284. }
  285. }
  286. }
  287. ShapeCast mShapeCast;
  288. const ShapeCastSettings & mShapeCastSettings;
  289. CastShapeCollector & mCollector;
  290. const BodyLockInterface & mBodyLockInterface;
  291. const BodyFilter & mBodyFilter;
  292. const ShapeFilter & mShapeFilter;
  293. };
  294. // Do broadphase test
  295. MyCollector collector(inShapeCast, inShapeCastSettings, ioCollector, *mBodyLockInterface, inBodyFilter, inShapeFilter);
  296. mBroadPhase->CastAABox({ inShapeCast.mShapeWorldBounds, inShapeCast.mDirection }, collector, inBroadPhaseLayerFilter, inObjectLayerFilter);
  297. }
  298. void NarrowPhaseQuery::CollectTransformedShapes(const AABox &inBox, TransformedShapeCollector &ioCollector, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter) const
  299. {
  300. class MyCollector : public CollideShapeBodyCollector
  301. {
  302. public:
  303. MyCollector(const AABox &inBox, TransformedShapeCollector &ioCollector, const BodyLockInterface &inBodyLockInterface, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter) :
  304. mBox(inBox),
  305. mCollector(ioCollector),
  306. mBodyLockInterface(inBodyLockInterface),
  307. mBodyFilter(inBodyFilter),
  308. mShapeFilter(inShapeFilter)
  309. {
  310. }
  311. virtual void AddHit(const ResultType &inResult) override
  312. {
  313. // Only test shape if it passes the body filter
  314. if (mBodyFilter.ShouldCollide(inResult))
  315. {
  316. // Lock the body
  317. BodyLockRead lock(mBodyLockInterface, inResult);
  318. if (lock.Succeeded())
  319. {
  320. const Body &body = lock.GetBody();
  321. // Check body filter again now that we've locked the body
  322. if (mBodyFilter.ShouldCollideLocked(body))
  323. {
  324. // Collect the transformed shape
  325. TransformedShape ts = body.GetTransformedShape();
  326. // Notify collector of new body
  327. mCollector.OnBody(body);
  328. // Release the lock now, we have all the info we need in the transformed shape
  329. lock.ReleaseLock();
  330. // Do narrow phase collision check
  331. ts.CollectTransformedShapes(mBox, mCollector, mShapeFilter);
  332. // Update early out fraction based on narrow phase collector
  333. UpdateEarlyOutFraction(mCollector.GetEarlyOutFraction());
  334. }
  335. }
  336. }
  337. }
  338. const AABox & mBox;
  339. TransformedShapeCollector & mCollector;
  340. const BodyLockInterface & mBodyLockInterface;
  341. const BodyFilter & mBodyFilter;
  342. const ShapeFilter & mShapeFilter;
  343. };
  344. // Do broadphase test
  345. MyCollector collector(inBox, ioCollector, *mBodyLockInterface, inBodyFilter, inShapeFilter);
  346. mBroadPhase->CollideAABox(inBox, collector, inBroadPhaseLayerFilter, inObjectLayerFilter);
  347. }
  348. JPH_NAMESPACE_END