MutableCompoundShape.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <Jolt/Jolt.h>
  5. #include <Jolt/Physics/Collision/Shape/MutableCompoundShape.h>
  6. #include <Jolt/Physics/Collision/Shape/CompoundShapeVisitors.h>
  7. #include <Jolt/Core/Profiler.h>
  8. #include <Jolt/Core/StreamIn.h>
  9. #include <Jolt/Core/StreamOut.h>
  10. #include <Jolt/ObjectStream/TypeDeclarations.h>
  11. JPH_NAMESPACE_BEGIN
  12. JPH_IMPLEMENT_SERIALIZABLE_VIRTUAL(MutableCompoundShapeSettings)
  13. {
  14. JPH_ADD_BASE_CLASS(MutableCompoundShapeSettings, CompoundShapeSettings)
  15. }
  16. ShapeSettings::ShapeResult MutableCompoundShapeSettings::Create() const
  17. {
  18. // Build a mutable compound shape
  19. if (mCachedResult.IsEmpty())
  20. Ref<Shape> shape = new MutableCompoundShape(*this, mCachedResult);
  21. return mCachedResult;
  22. }
  23. MutableCompoundShape::MutableCompoundShape(const MutableCompoundShapeSettings &inSettings, ShapeResult &outResult) :
  24. CompoundShape(EShapeSubType::MutableCompound, inSettings, outResult)
  25. {
  26. mSubShapes.reserve(inSettings.mSubShapes.size());
  27. for (const CompoundShapeSettings::SubShapeSettings &shape : inSettings.mSubShapes)
  28. {
  29. // Start constructing the runtime sub shape
  30. SubShape out_shape;
  31. if (!out_shape.FromSettings(shape, outResult))
  32. return;
  33. mSubShapes.push_back(out_shape);
  34. }
  35. AdjustCenterOfMass();
  36. CalculateSubShapeBounds(0, (uint)mSubShapes.size());
  37. // Check if we're not exceeding the amount of sub shape id bits
  38. if (GetSubShapeIDBitsRecursive() > SubShapeID::MaxBits)
  39. {
  40. outResult.SetError("Compound hierarchy is too deep and exceeds the amount of available sub shape ID bits");
  41. return;
  42. }
  43. outResult.Set(this);
  44. }
  45. void MutableCompoundShape::AdjustCenterOfMass()
  46. {
  47. // First calculate the delta of the center of mass
  48. float mass = 0.0f;
  49. Vec3 center_of_mass = Vec3::sZero();
  50. for (const CompoundShape::SubShape &sub_shape : mSubShapes)
  51. {
  52. MassProperties child = sub_shape.mShape->GetMassProperties();
  53. mass += child.mMass;
  54. center_of_mass += sub_shape.GetPositionCOM() * child.mMass;
  55. }
  56. if (mass > 0.0f)
  57. center_of_mass /= mass;
  58. // Now adjust all shapes to recenter around center of mass
  59. for (CompoundShape::SubShape &sub_shape : mSubShapes)
  60. sub_shape.SetPositionCOM(sub_shape.GetPositionCOM() - center_of_mass);
  61. // And adjust the center of mass for this shape in the opposite direction
  62. mCenterOfMass += center_of_mass;
  63. }
  64. void MutableCompoundShape::CalculateLocalBounds()
  65. {
  66. uint num_blocks = GetNumBlocks();
  67. if (num_blocks > 0)
  68. {
  69. // Initialize min/max for first block
  70. const Bounds *bounds = mSubShapeBounds.data();
  71. Vec4 min_x = bounds->mMinX;
  72. Vec4 min_y = bounds->mMinY;
  73. Vec4 min_z = bounds->mMinZ;
  74. Vec4 max_x = bounds->mMaxX;
  75. Vec4 max_y = bounds->mMaxY;
  76. Vec4 max_z = bounds->mMaxZ;
  77. // Accumulate other blocks
  78. const Bounds *bounds_end = bounds + num_blocks;
  79. for (++bounds; bounds < bounds_end; ++bounds)
  80. {
  81. min_x = Vec4::sMin(min_x, bounds->mMinX);
  82. min_y = Vec4::sMin(min_y, bounds->mMinY);
  83. min_z = Vec4::sMin(min_z, bounds->mMinZ);
  84. max_x = Vec4::sMax(max_x, bounds->mMaxX);
  85. max_y = Vec4::sMax(max_y, bounds->mMaxY);
  86. max_z = Vec4::sMax(max_z, bounds->mMaxZ);
  87. }
  88. // Calculate resulting bounding box
  89. mLocalBounds.mMin.SetX(min_x.ReduceMin());
  90. mLocalBounds.mMin.SetY(min_y.ReduceMin());
  91. mLocalBounds.mMin.SetZ(min_z.ReduceMin());
  92. mLocalBounds.mMax.SetX(max_x.ReduceMax());
  93. mLocalBounds.mMax.SetY(max_y.ReduceMax());
  94. mLocalBounds.mMax.SetZ(max_z.ReduceMax());
  95. }
  96. else
  97. {
  98. // There are no subshapes, set the bounding box to invalid
  99. mLocalBounds.SetEmpty();
  100. }
  101. // Cache the inner radius as it can take a while to recursively iterate over all sub shapes
  102. CalculateInnerRadius();
  103. }
  104. void MutableCompoundShape::EnsureSubShapeBoundsCapacity()
  105. {
  106. // Check if we have enough space
  107. uint new_capacity = ((uint)mSubShapes.size() + 3) >> 2;
  108. if (mSubShapeBounds.size() < new_capacity)
  109. mSubShapeBounds.resize(new_capacity);
  110. }
  111. void MutableCompoundShape::CalculateSubShapeBounds(uint inStartIdx, uint inNumber)
  112. {
  113. // Ensure that we have allocated the required space for mSubShapeBounds
  114. EnsureSubShapeBoundsCapacity();
  115. // Loop over blocks of 4 sub shapes
  116. for (uint sub_shape_idx_start = inStartIdx & ~uint(3), sub_shape_idx_end = inStartIdx + inNumber; sub_shape_idx_start < sub_shape_idx_end; sub_shape_idx_start += 4)
  117. {
  118. Mat44 bounds_min;
  119. Mat44 bounds_max;
  120. AABox sub_shape_bounds;
  121. for (uint col = 0; col < 4; ++col)
  122. {
  123. uint sub_shape_idx = sub_shape_idx_start + col;
  124. if (sub_shape_idx < mSubShapes.size()) // else reuse sub_shape_bounds from previous iteration
  125. {
  126. const SubShape &sub_shape = mSubShapes[sub_shape_idx];
  127. // Tranform the shape's bounds into our local space
  128. Mat44 transform = Mat44::sRotationTranslation(sub_shape.GetRotation(), sub_shape.GetPositionCOM());
  129. // Get the bounding box
  130. sub_shape_bounds = sub_shape.mShape->GetWorldSpaceBounds(transform, Vec3::sReplicate(1.0f));
  131. }
  132. // Put the bounds as columns in a matrix
  133. bounds_min.SetColumn3(col, sub_shape_bounds.mMin);
  134. bounds_max.SetColumn3(col, sub_shape_bounds.mMax);
  135. }
  136. // Transpose to go to strucucture of arrays format
  137. Mat44 bounds_min_t = bounds_min.Transposed();
  138. Mat44 bounds_max_t = bounds_max.Transposed();
  139. // Store in our bounds array
  140. Bounds &bounds = mSubShapeBounds[sub_shape_idx_start >> 2];
  141. bounds.mMinX = bounds_min_t.GetColumn4(0);
  142. bounds.mMinY = bounds_min_t.GetColumn4(1);
  143. bounds.mMinZ = bounds_min_t.GetColumn4(2);
  144. bounds.mMaxX = bounds_max_t.GetColumn4(0);
  145. bounds.mMaxY = bounds_max_t.GetColumn4(1);
  146. bounds.mMaxZ = bounds_max_t.GetColumn4(2);
  147. }
  148. CalculateLocalBounds();
  149. }
  150. uint MutableCompoundShape::AddShape(Vec3Arg inPosition, QuatArg inRotation, const Shape *inShape, uint32 inUserData)
  151. {
  152. SubShape sub_shape;
  153. sub_shape.mShape = inShape;
  154. sub_shape.mUserData = inUserData;
  155. sub_shape.SetTransform(inPosition, inRotation, mCenterOfMass);
  156. mSubShapes.push_back(sub_shape);
  157. uint shape_idx = (uint)mSubShapes.size() - 1;
  158. CalculateSubShapeBounds(shape_idx, 1);
  159. return shape_idx;
  160. }
  161. void MutableCompoundShape::RemoveShape(uint inIndex)
  162. {
  163. mSubShapes.erase(mSubShapes.begin() + inIndex);
  164. uint num_bounds = (uint)mSubShapes.size() - inIndex;
  165. if (num_bounds > 0)
  166. CalculateSubShapeBounds(inIndex, num_bounds);
  167. else
  168. CalculateLocalBounds();
  169. }
  170. void MutableCompoundShape::ModifyShape(uint inIndex, Vec3Arg inPosition, QuatArg inRotation)
  171. {
  172. SubShape &sub_shape = mSubShapes[inIndex];
  173. sub_shape.SetTransform(inPosition, inRotation, mCenterOfMass);
  174. CalculateSubShapeBounds(inIndex, 1);
  175. }
  176. void MutableCompoundShape::ModifyShape(uint inIndex, Vec3Arg inPosition, QuatArg inRotation, const Shape *inShape)
  177. {
  178. SubShape &sub_shape = mSubShapes[inIndex];
  179. sub_shape.mShape = inShape;
  180. sub_shape.SetTransform(inPosition, inRotation, mCenterOfMass);
  181. CalculateSubShapeBounds(inIndex, 1);
  182. }
  183. void MutableCompoundShape::ModifyShapes(uint inStartIndex, uint inNumber, const Vec3 *inPositions, const Quat *inRotations, uint inPositionStride, uint inRotationStride)
  184. {
  185. JPH_ASSERT(inStartIndex + inNumber <= mSubShapes.size());
  186. const Vec3 *pos = inPositions;
  187. const Quat *rot = inRotations;
  188. for (SubShape *dest = &mSubShapes[inStartIndex], *dest_end = dest + inNumber; dest < dest_end; ++dest)
  189. {
  190. // Update transform
  191. dest->SetTransform(*pos, *rot, mCenterOfMass);
  192. // Advance pointer in position / rotation buffer
  193. pos = reinterpret_cast<const Vec3 *>(reinterpret_cast<const uint8 *>(pos) + inPositionStride);
  194. rot = reinterpret_cast<const Quat *>(reinterpret_cast<const uint8 *>(rot) + inRotationStride);
  195. }
  196. CalculateSubShapeBounds(inStartIndex, inNumber);
  197. }
  198. template <class Visitor>
  199. inline void MutableCompoundShape::WalkSubShapes(Visitor &ioVisitor) const
  200. {
  201. // Loop over all blocks of 4 bounding boxes
  202. for (uint block = 0, num_blocks = GetNumBlocks(); block < num_blocks; ++block)
  203. {
  204. // Test the bounding boxes
  205. const Bounds &bounds = mSubShapeBounds[block];
  206. typename Visitor::Result result = ioVisitor.TestBlock(bounds.mMinX, bounds.mMinY, bounds.mMinZ, bounds.mMaxX, bounds.mMaxY, bounds.mMaxZ);
  207. // Check if any of the bounding boxes collided
  208. if (ioVisitor.ShouldVisitBlock(result))
  209. {
  210. // Go through the individual boxes
  211. uint sub_shape_start_idx = block << 2;
  212. for (uint col = 0, max_col = min<uint>(4, (uint)mSubShapes.size() - sub_shape_start_idx); col < max_col; ++col) // Don't read beyond the end of the subshapes array
  213. if (ioVisitor.ShouldVisitSubShape(result, col)) // Because the early out fraction can change, we need to retest every shape
  214. {
  215. // Test sub shape
  216. uint sub_shape_idx = sub_shape_start_idx + col;
  217. const SubShape &sub_shape = mSubShapes[sub_shape_idx];
  218. ioVisitor.VisitShape(sub_shape, sub_shape_idx);
  219. // If no better collision is available abort
  220. if (ioVisitor.ShouldAbort())
  221. break;
  222. }
  223. }
  224. }
  225. }
  226. bool MutableCompoundShape::CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const
  227. {
  228. JPH_PROFILE_FUNCTION();
  229. struct Visitor : public CastRayVisitor
  230. {
  231. using CastRayVisitor::CastRayVisitor;
  232. using Result = Vec4;
  233. JPH_INLINE Result TestBlock(Vec4Arg inBoundsMinX, Vec4Arg inBoundsMinY, Vec4Arg inBoundsMinZ, Vec4Arg inBoundsMaxX, Vec4Arg inBoundsMaxY, Vec4Arg inBoundsMaxZ) const
  234. {
  235. return TestBounds(inBoundsMinX, inBoundsMinY, inBoundsMinZ, inBoundsMaxX, inBoundsMaxY, inBoundsMaxZ);
  236. }
  237. JPH_INLINE bool ShouldVisitBlock(Vec4Arg inResult) const
  238. {
  239. UVec4 closer = Vec4::sLess(inResult, Vec4::sReplicate(mHit.mFraction));
  240. return closer.TestAnyTrue();
  241. }
  242. JPH_INLINE bool ShouldVisitSubShape(Vec4Arg inResult, uint inIndexInBlock) const
  243. {
  244. return inResult[inIndexInBlock] < mHit.mFraction;
  245. }
  246. };
  247. Visitor visitor(inRay, this, inSubShapeIDCreator, ioHit);
  248. WalkSubShapes(visitor);
  249. return visitor.mReturnValue;
  250. }
  251. void MutableCompoundShape::CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter) const
  252. {
  253. JPH_PROFILE_FUNCTION();
  254. // Test shape filter
  255. if (!inShapeFilter.ShouldCollide(this, inSubShapeIDCreator.GetID()))
  256. return;
  257. struct Visitor : public CastRayVisitorCollector
  258. {
  259. using CastRayVisitorCollector::CastRayVisitorCollector;
  260. using Result = Vec4;
  261. JPH_INLINE Result TestBlock(Vec4Arg inBoundsMinX, Vec4Arg inBoundsMinY, Vec4Arg inBoundsMinZ, Vec4Arg inBoundsMaxX, Vec4Arg inBoundsMaxY, Vec4Arg inBoundsMaxZ) const
  262. {
  263. return TestBounds(inBoundsMinX, inBoundsMinY, inBoundsMinZ, inBoundsMaxX, inBoundsMaxY, inBoundsMaxZ);
  264. }
  265. JPH_INLINE bool ShouldVisitBlock(Vec4Arg inResult) const
  266. {
  267. UVec4 closer = Vec4::sLess(inResult, Vec4::sReplicate(mCollector.GetEarlyOutFraction()));
  268. return closer.TestAnyTrue();
  269. }
  270. JPH_INLINE bool ShouldVisitSubShape(Vec4Arg inResult, uint inIndexInBlock) const
  271. {
  272. return inResult[inIndexInBlock] < mCollector.GetEarlyOutFraction();
  273. }
  274. };
  275. Visitor visitor(inRay, inRayCastSettings, this, inSubShapeIDCreator, ioCollector, inShapeFilter);
  276. WalkSubShapes(visitor);
  277. }
  278. void MutableCompoundShape::CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter) const
  279. {
  280. JPH_PROFILE_FUNCTION();
  281. // Test shape filter
  282. if (!inShapeFilter.ShouldCollide(this, inSubShapeIDCreator.GetID()))
  283. return;
  284. struct Visitor : public CollidePointVisitor
  285. {
  286. using CollidePointVisitor::CollidePointVisitor;
  287. using Result = UVec4;
  288. JPH_INLINE Result TestBlock(Vec4Arg inBoundsMinX, Vec4Arg inBoundsMinY, Vec4Arg inBoundsMinZ, Vec4Arg inBoundsMaxX, Vec4Arg inBoundsMaxY, Vec4Arg inBoundsMaxZ) const
  289. {
  290. return TestBounds(inBoundsMinX, inBoundsMinY, inBoundsMinZ, inBoundsMaxX, inBoundsMaxY, inBoundsMaxZ);
  291. }
  292. JPH_INLINE bool ShouldVisitBlock(UVec4Arg inResult) const
  293. {
  294. return inResult.TestAnyTrue();
  295. }
  296. JPH_INLINE bool ShouldVisitSubShape(UVec4Arg inResult, uint inIndexInBlock) const
  297. {
  298. return inResult[inIndexInBlock] != 0;
  299. }
  300. };
  301. Visitor visitor(inPoint, this, inSubShapeIDCreator, ioCollector, inShapeFilter);
  302. WalkSubShapes(visitor);
  303. }
  304. void MutableCompoundShape::sCastShapeVsCompound(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector)
  305. {
  306. JPH_PROFILE_FUNCTION();
  307. struct Visitor : public CastShapeVisitor
  308. {
  309. using CastShapeVisitor::CastShapeVisitor;
  310. using Result = Vec4;
  311. JPH_INLINE Result TestBlock(Vec4Arg inBoundsMinX, Vec4Arg inBoundsMinY, Vec4Arg inBoundsMinZ, Vec4Arg inBoundsMaxX, Vec4Arg inBoundsMaxY, Vec4Arg inBoundsMaxZ) const
  312. {
  313. return TestBounds(inBoundsMinX, inBoundsMinY, inBoundsMinZ, inBoundsMaxX, inBoundsMaxY, inBoundsMaxZ);
  314. }
  315. JPH_INLINE bool ShouldVisitBlock(Vec4Arg inResult) const
  316. {
  317. UVec4 closer = Vec4::sLess(inResult, Vec4::sReplicate(mCollector.GetEarlyOutFraction()));
  318. return closer.TestAnyTrue();
  319. }
  320. JPH_INLINE bool ShouldVisitSubShape(Vec4Arg inResult, uint inIndexInBlock) const
  321. {
  322. return inResult[inIndexInBlock] < mCollector.GetEarlyOutFraction();
  323. }
  324. };
  325. JPH_ASSERT(inShape->GetSubType() == EShapeSubType::MutableCompound);
  326. const MutableCompoundShape *shape = static_cast<const MutableCompoundShape *>(inShape);
  327. Visitor visitor(inShapeCast, inShapeCastSettings, shape, inScale, inShapeFilter, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, ioCollector);
  328. shape->WalkSubShapes(visitor);
  329. }
  330. void MutableCompoundShape::CollectTransformedShapes(const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale, const SubShapeIDCreator &inSubShapeIDCreator, TransformedShapeCollector &ioCollector, const ShapeFilter &inShapeFilter) const
  331. {
  332. JPH_PROFILE_FUNCTION();
  333. // Test shape filter
  334. if (!inShapeFilter.ShouldCollide(this, inSubShapeIDCreator.GetID()))
  335. return;
  336. struct Visitor : public CollectTransformedShapesVisitor
  337. {
  338. using CollectTransformedShapesVisitor::CollectTransformedShapesVisitor;
  339. using Result = UVec4;
  340. JPH_INLINE Result TestBlock(Vec4Arg inBoundsMinX, Vec4Arg inBoundsMinY, Vec4Arg inBoundsMinZ, Vec4Arg inBoundsMaxX, Vec4Arg inBoundsMaxY, Vec4Arg inBoundsMaxZ) const
  341. {
  342. return TestBounds(inBoundsMinX, inBoundsMinY, inBoundsMinZ, inBoundsMaxX, inBoundsMaxY, inBoundsMaxZ);
  343. }
  344. JPH_INLINE bool ShouldVisitBlock(UVec4Arg inResult) const
  345. {
  346. return inResult.TestAnyTrue();
  347. }
  348. JPH_INLINE bool ShouldVisitSubShape(UVec4Arg inResult, uint inIndexInBlock) const
  349. {
  350. return inResult[inIndexInBlock] != 0;
  351. }
  352. };
  353. Visitor visitor(inBox, this, inPositionCOM, inRotation, inScale, inSubShapeIDCreator, ioCollector, inShapeFilter);
  354. WalkSubShapes(visitor);
  355. }
  356. int MutableCompoundShape::GetIntersectingSubShapes(const AABox &inBox, uint *outSubShapeIndices, int inMaxSubShapeIndices) const
  357. {
  358. JPH_PROFILE_FUNCTION();
  359. GetIntersectingSubShapesVisitorMC<AABox> visitor(inBox, outSubShapeIndices, inMaxSubShapeIndices);
  360. WalkSubShapes(visitor);
  361. return visitor.GetNumResults();
  362. }
  363. int MutableCompoundShape::GetIntersectingSubShapes(const OrientedBox &inBox, uint *outSubShapeIndices, int inMaxSubShapeIndices) const
  364. {
  365. JPH_PROFILE_FUNCTION();
  366. GetIntersectingSubShapesVisitorMC<OrientedBox> visitor(inBox, outSubShapeIndices, inMaxSubShapeIndices);
  367. WalkSubShapes(visitor);
  368. return visitor.GetNumResults();
  369. }
  370. void MutableCompoundShape::sCollideCompoundVsShape(const Shape *inShape1, const Shape *inShape2, Vec3Arg inScale1, Vec3Arg inScale2, Mat44Arg inCenterOfMassTransform1, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, const CollideShapeSettings &inCollideShapeSettings, CollideShapeCollector &ioCollector, const ShapeFilter &inShapeFilter)
  371. {
  372. JPH_PROFILE_FUNCTION();
  373. JPH_ASSERT(inShape1->GetSubType() == EShapeSubType::MutableCompound);
  374. const MutableCompoundShape *shape1 = static_cast<const MutableCompoundShape *>(inShape1);
  375. struct Visitor : public CollideCompoundVsShapeVisitor
  376. {
  377. using CollideCompoundVsShapeVisitor::CollideCompoundVsShapeVisitor;
  378. using Result = UVec4;
  379. JPH_INLINE Result TestBlock(Vec4Arg inBoundsMinX, Vec4Arg inBoundsMinY, Vec4Arg inBoundsMinZ, Vec4Arg inBoundsMaxX, Vec4Arg inBoundsMaxY, Vec4Arg inBoundsMaxZ) const
  380. {
  381. return TestBounds(inBoundsMinX, inBoundsMinY, inBoundsMinZ, inBoundsMaxX, inBoundsMaxY, inBoundsMaxZ);
  382. }
  383. JPH_INLINE bool ShouldVisitBlock(UVec4Arg inResult) const
  384. {
  385. return inResult.TestAnyTrue();
  386. }
  387. JPH_INLINE bool ShouldVisitSubShape(UVec4Arg inResult, uint inIndexInBlock) const
  388. {
  389. return inResult[inIndexInBlock] != 0;
  390. }
  391. };
  392. Visitor visitor(shape1, inShape2, inScale1, inScale2, inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, inCollideShapeSettings, ioCollector, inShapeFilter);
  393. shape1->WalkSubShapes(visitor);
  394. }
  395. void MutableCompoundShape::sCollideShapeVsCompound(const Shape *inShape1, const Shape *inShape2, Vec3Arg inScale1, Vec3Arg inScale2, Mat44Arg inCenterOfMassTransform1, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, const CollideShapeSettings &inCollideShapeSettings, CollideShapeCollector &ioCollector, const ShapeFilter &inShapeFilter)
  396. {
  397. JPH_PROFILE_FUNCTION();
  398. JPH_ASSERT(inShape2->GetSubType() == EShapeSubType::MutableCompound);
  399. const MutableCompoundShape *shape2 = static_cast<const MutableCompoundShape *>(inShape2);
  400. struct Visitor : public CollideShapeVsCompoundVisitor
  401. {
  402. using CollideShapeVsCompoundVisitor::CollideShapeVsCompoundVisitor;
  403. using Result = UVec4;
  404. JPH_INLINE Result TestBlock(Vec4Arg inBoundsMinX, Vec4Arg inBoundsMinY, Vec4Arg inBoundsMinZ, Vec4Arg inBoundsMaxX, Vec4Arg inBoundsMaxY, Vec4Arg inBoundsMaxZ) const
  405. {
  406. return TestBounds(inBoundsMinX, inBoundsMinY, inBoundsMinZ, inBoundsMaxX, inBoundsMaxY, inBoundsMaxZ);
  407. }
  408. JPH_INLINE bool ShouldVisitBlock(UVec4Arg inResult) const
  409. {
  410. return inResult.TestAnyTrue();
  411. }
  412. JPH_INLINE bool ShouldVisitSubShape(UVec4Arg inResult, uint inIndexInBlock) const
  413. {
  414. return inResult[inIndexInBlock] != 0;
  415. }
  416. };
  417. Visitor visitor(inShape1, shape2, inScale1, inScale2, inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, inCollideShapeSettings, ioCollector, inShapeFilter);
  418. shape2->WalkSubShapes(visitor);
  419. }
  420. void MutableCompoundShape::SaveBinaryState(StreamOut &inStream) const
  421. {
  422. CompoundShape::SaveBinaryState(inStream);
  423. // Write bounds
  424. uint bounds_size = (((uint)mSubShapes.size() + 3) >> 2) * sizeof(Bounds);
  425. inStream.WriteBytes(mSubShapeBounds.data(), bounds_size);
  426. }
  427. void MutableCompoundShape::RestoreBinaryState(StreamIn &inStream)
  428. {
  429. CompoundShape::RestoreBinaryState(inStream);
  430. // Ensure that we have allocated the required space for mSubShapeBounds
  431. EnsureSubShapeBoundsCapacity();
  432. // Read bounds
  433. uint bounds_size = (((uint)mSubShapes.size() + 3) >> 2) * sizeof(Bounds);
  434. inStream.ReadBytes(mSubShapeBounds.data(), bounds_size);
  435. }
  436. void MutableCompoundShape::sRegister()
  437. {
  438. ShapeFunctions &f = ShapeFunctions::sGet(EShapeSubType::MutableCompound);
  439. f.mConstruct = []() -> Shape * { return new MutableCompoundShape; };
  440. f.mColor = Color::sDarkOrange;
  441. for (EShapeSubType s : sAllSubShapeTypes)
  442. {
  443. CollisionDispatch::sRegisterCollideShape(EShapeSubType::MutableCompound, s, sCollideCompoundVsShape);
  444. CollisionDispatch::sRegisterCollideShape(s, EShapeSubType::MutableCompound, sCollideShapeVsCompound);
  445. CollisionDispatch::sRegisterCastShape(s, EShapeSubType::MutableCompound, sCastShapeVsCompound);
  446. }
  447. }
  448. JPH_NAMESPACE_END