| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <Jolt/Geometry/AABox.h> |
| 8 |
#include <Jolt/Physics/Collision/CollideShape.h> |
| 9 |
#include <Jolt/Physics/Collision/Shape/Shape.h> |
| 10 |
|
| 11 |
JPH_NAMESPACE_BEGIN |
| 12 |
|
| 13 |
|
| 14 |
template <class Vec, class Mat, class ShapeCastType> |
| 15 |
struct ShapeCastT |
| 16 |
{ |
| 17 |
JPH_OVERRIDE_NEW_DELETE |
| 18 |
|
| 19 |
|
| 20 |
ShapeCastT(const Shape *inShape, Vec3Arg inScale, typename Mat::ArgType inCenterOfMassStart, Vec3Arg inDirection, const AABox &inWorldSpaceBounds) : |
| 21 |
mShape(inShape), |
| 22 |
mScale(inScale), |
| 23 |
mCenterOfMassStart(inCenterOfMassStart), |
| 24 |
mDirection(inDirection), |
| 25 |
mShapeWorldBounds(inWorldSpaceBounds) |
| 26 |
{ |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
ShapeCastT(const Shape *inShape, Vec3Arg inScale, typename Mat::ArgType inCenterOfMassStart, Vec3Arg inDirection) : |
| 31 |
ShapeCastT<Vec, Mat, ShapeCastType>(inShape, inScale, inCenterOfMassStart, inDirection, inShape->GetWorldSpaceBounds(inCenterOfMassStart, inScale)) |
| 32 |
{ |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
static inline ShapeCastType sFromWorldTransform(const Shape *inShape, Vec3Arg inScale, typename Mat::ArgType inWorldTransform, Vec3Arg inDirection) |
| 37 |
{ |
| 38 |
return ShapeCastType(inShape, inScale, inWorldTransform.PreTranslated(inShape->GetCenterOfMass()), inDirection); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
ShapeCastType PostTransformed(typename Mat::ArgType inTransform) const |
| 43 |
{ |
| 44 |
Mat44 start = inTransform * mCenterOfMassStart; |
| 45 |
Vec3 direction = inTransform.Multiply3x3(mDirection); |
| 46 |
return { mShape, mScale, start, direction }; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
ShapeCastType PostTranslated(typename Vec::ArgType inTranslation) const |
| 51 |
{ |
| 52 |
return { mShape, mScale, mCenterOfMassStart.PostTranslated(inTranslation), mDirection }; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
inline Vec GetPointOnRay(float inFraction) const |
| 57 |
{ |
| 58 |
return mCenterOfMassStart.GetTranslation() + inFraction * mDirection; |
| 59 |
} |
| 60 |
|
| 61 |
const Shape * mShape; |
| 62 |
const Vec3 mScale; |
| 63 |
const Mat mCenterOfMassStart; |
| 64 |
const Vec3 mDirection; |
| 65 |
const AABox mShapeWorldBounds; |
| 66 |
}; |
| 67 |
|
| 68 |
struct ShapeCast : public ShapeCastT<Vec3, Mat44, ShapeCast> |
| 69 |
{ |
| 70 |
using ShapeCastT<Vec3, Mat44, ShapeCast>::ShapeCastT; |
| 71 |
}; |
| 72 |
|
| 73 |
struct RShapeCast : public ShapeCastT<RVec3, RMat44, RShapeCast> |
| 74 |
{ |
| 75 |
using ShapeCastT<RVec3, RMat44, RShapeCast>::ShapeCastT; |
| 76 |
|
| 77 |
|
| 78 |
explicit RShapeCast(const ShapeCast &inCast) : |
| 79 |
RShapeCast(inCast.mShape, inCast.mScale, RMat44(inCast.mCenterOfMassStart), inCast.mDirection, inCast.mShapeWorldBounds) |
| 80 |
{ |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
explicit operator ShapeCast() const |
| 85 |
{ |
| 86 |
return ShapeCast(mShape, mScale, mCenterOfMassStart.ToMat44(), mDirection, mShapeWorldBounds); |
| 87 |
} |
| 88 |
}; |
| 89 |
|
| 90 |
|
| 91 |
class ShapeCastSettings : public CollideSettingsBase |
| 92 |
{ |
| 93 |
public: |
| 94 |
JPH_OVERRIDE_NEW_DELETE |
| 95 |
|
| 96 |
|
| 97 |
void SetBackFaceMode(EBackFaceMode inMode) { mBackFaceModeTriangles = mBackFaceModeConvex = inMode; } |
| 98 |
|
| 99 |
|
| 100 |
EBackFaceMode mBackFaceModeTriangles = EBackFaceMode::IgnoreBackFaces; |
| 101 |
|
| 102 |
|
| 103 |
EBackFaceMode mBackFaceModeConvex = EBackFaceMode::IgnoreBackFaces; |
| 104 |
|
| 105 |
|
| 106 |
bool mUseShrunkenShapeAndConvexRadius = false; |
| 107 |
|
| 108 |
|
| 109 |
bool mReturnDeepestPoint = false; |
| 110 |
}; |
| 111 |
|
| 112 |
|
| 113 |
class ShapeCastResult : public CollideShapeResult |
| 114 |
{ |
| 115 |
public: |
| 116 |
JPH_OVERRIDE_NEW_DELETE |
| 117 |
|
| 118 |
|
| 119 |
ShapeCastResult() = default; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
ShapeCastResult(float inFraction, Vec3Arg inContactPoint1, Vec3Arg inContactPoint2, Vec3Arg inContactNormalOrPenetrationDepth, bool inBackFaceHit, const SubShapeID &inSubShapeID1, const SubShapeID &inSubShapeID2, const BodyID &inBodyID2) : |
| 131 |
CollideShapeResult(inContactPoint1, inContactPoint2, inContactNormalOrPenetrationDepth, (inContactPoint2 - inContactPoint1).Length(), inSubShapeID1, inSubShapeID2, inBodyID2), |
| 132 |
mFraction(inFraction), |
| 133 |
mIsBackFaceHit(inBackFaceHit) |
| 134 |
{ |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
inline float GetEarlyOutFraction() const { return mFraction > 0.0f? mFraction : -mPenetrationDepth; } |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
ShapeCastResult Reversed(Vec3Arg inWorldSpaceCastDirection) const |
| 143 |
{ |
| 144 |
|
| 145 |
Vec3 delta = mFraction * inWorldSpaceCastDirection; |
| 146 |
|
| 147 |
ShapeCastResult result; |
| 148 |
result.mContactPointOn2 = mContactPointOn1 - delta; |
| 149 |
result.mContactPointOn1 = mContactPointOn2 - delta; |
| 150 |
result.mPenetrationAxis = -mPenetrationAxis; |
| 151 |
result.mPenetrationDepth = mPenetrationDepth; |
| 152 |
result.mSubShapeID2 = mSubShapeID1; |
| 153 |
result.mSubShapeID1 = mSubShapeID2; |
| 154 |
result.mBodyID2 = mBodyID2; |
| 155 |
result.mFraction = mFraction; |
| 156 |
result.mIsBackFaceHit = mIsBackFaceHit; |
| 157 |
|
| 158 |
result.mShape2Face.resize(mShape1Face.size()); |
| 159 |
for (Face::size_type i = 0; i < mShape1Face.size(); ++i) |
| 160 |
result.mShape2Face[i] = mShape1Face[i] - delta; |
| 161 |
|
| 162 |
result.mShape1Face.resize(mShape2Face.size()); |
| 163 |
for (Face::size_type i = 0; i < mShape2Face.size(); ++i) |
| 164 |
result.mShape1Face[i] = mShape2Face[i] - delta; |
| 165 |
|
| 166 |
return result; |
| 167 |
} |
| 168 |
|
| 169 |
float mFraction; |
| 170 |
bool mIsBackFaceHit; |
| 171 |
}; |
| 172 |
|
| 173 |
JPH_NAMESPACE_END |