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