| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <Jolt/Physics/Collision/CollideShape.h> |
| 8 |
#include <Jolt/Physics/Collision/CollisionDispatch.h> |
| 9 |
#include <Jolt/Core/STLLocalAllocator.h> |
| 10 |
|
| 11 |
JPH_NAMESPACE_BEGIN |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
template <class LeafCollector> |
| 29 |
void CollideShapeVsShapePerLeaf(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 = { }) |
| 30 |
{ |
| 31 |
|
| 32 |
struct LeafShape |
| 33 |
{ |
| 34 |
LeafShape() = default; |
| 35 |
|
| 36 |
LeafShape(const AABox &inBounds, Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Shape *inShape, const SubShapeIDCreator &inSubShapeIDCreator) : |
| 37 |
mBounds(inBounds), |
| 38 |
mCenterOfMassTransform(inCenterOfMassTransform), |
| 39 |
mScale(inScale), |
| 40 |
mShape(inShape), |
| 41 |
mSubShapeIDCreator(inSubShapeIDCreator) |
| 42 |
{ |
| 43 |
} |
| 44 |
|
| 45 |
AABox mBounds; |
| 46 |
Mat44 mCenterOfMassTransform; |
| 47 |
Vec3 mScale; |
| 48 |
const Shape * mShape; |
| 49 |
SubShapeIDCreator mSubShapeIDCreator; |
| 50 |
}; |
| 51 |
|
| 52 |
constexpr uint cMaxLocalLeafShapes = 32; |
| 53 |
|
| 54 |
|
| 55 |
class MyCollector : public TransformedShapeCollector |
| 56 |
{ |
| 57 |
public: |
| 58 |
MyCollector() |
| 59 |
{ |
| 60 |
mHits.reserve(cMaxLocalLeafShapes); |
| 61 |
} |
| 62 |
|
| 63 |
void AddHit(const TransformedShape &inShape) override |
| 64 |
{ |
| 65 |
mHits.emplace_back(inShape.GetWorldSpaceBounds(), inShape.GetCenterOfMassTransform().ToMat44(), inShape.GetShapeScale(), inShape.mShape, inShape.mSubShapeIDCreator); |
| 66 |
} |
| 67 |
|
| 68 |
Array<LeafShape, STLLocalAllocator<LeafShape, cMaxLocalLeafShapes>> mHits; |
| 69 |
}; |
| 70 |
|
| 71 |
|
| 72 |
AABox bounds1 = inShape1->GetWorldSpaceBounds(inCenterOfMassTransform1, inScale1); |
| 73 |
AABox bounds2 = inShape2->GetWorldSpaceBounds(inCenterOfMassTransform2, inScale2); |
| 74 |
|
| 75 |
|
| 76 |
MyCollector leaf_shapes1, leaf_shapes2; |
| 77 |
inShape1->CollectTransformedShapes(bounds2, inCenterOfMassTransform1.GetTranslation(), inCenterOfMassTransform1.GetQuaternion(), inScale1, inSubShapeIDCreator1, leaf_shapes1, inShapeFilter); |
| 78 |
inShape2->CollectTransformedShapes(bounds1, inCenterOfMassTransform2.GetTranslation(), inCenterOfMassTransform2.GetQuaternion(), inScale2, inSubShapeIDCreator2, leaf_shapes2, inShapeFilter); |
| 79 |
|
| 80 |
|
| 81 |
for (const LeafShape &leaf1 : leaf_shapes1.mHits) |
| 82 |
for (const LeafShape &leaf2 : leaf_shapes2.mHits) |
| 83 |
if (leaf1.mBounds.Overlaps(leaf2.mBounds)) |
| 84 |
{ |
| 85 |
|
| 86 |
LeafCollector collector; |
| 87 |
CollisionDispatch::sCollideShapeVsShape(leaf1.mShape, leaf2.mShape, leaf1.mScale, leaf2.mScale, leaf1.mCenterOfMassTransform, leaf2.mCenterOfMassTransform, leaf1.mSubShapeIDCreator, leaf2.mSubShapeIDCreator, inCollideShapeSettings, collector, inShapeFilter); |
| 88 |
if (collector.HadHit()) |
| 89 |
ioCollector.AddHit(collector.mHit); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
JPH_NAMESPACE_END |