MutableCompoundShape.cpp 20 KB

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