NarrowPhaseQuery.cpp 14 KB

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