| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <Jolt/Physics/Collision/Shape/Shape.h> |
| 8 |
#include <Jolt/Physics/Collision/Shape/ScaleHelpers.h> |
| 9 |
#include <Jolt/Physics/Collision/Shape/SubShapeID.h> |
| 10 |
|
| 11 |
JPH_NAMESPACE_BEGIN |
| 12 |
|
| 13 |
class CollideShapeSettings; |
| 14 |
class OrientedBox; |
| 15 |
|
| 16 |
|
| 17 |
class JPH_EXPORT CompoundShapeSettings : public ShapeSettings |
| 18 |
{ |
| 19 |
JPH_DECLARE_SERIALIZABLE_ABSTRACT(JPH_EXPORT, CompoundShapeSettings) |
| 20 |
|
| 21 |
public: |
| 22 |
|
| 23 |
CompoundShapeSettings() = default; |
| 24 |
|
| 25 |
|
| 26 |
void AddShape(Vec3Arg inPosition, QuatArg inRotation, const ShapeSettings *inShape, uint32 inUserData = 0); |
| 27 |
|
| 28 |
|
| 29 |
void AddShape(Vec3Arg inPosition, QuatArg inRotation, const Shape *inShape, uint32 inUserData = 0); |
| 30 |
|
| 31 |
struct SubShapeSettings |
| 32 |
{ |
| 33 |
JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, SubShapeSettings) |
| 34 |
|
| 35 |
RefConst<ShapeSettings> mShape; |
| 36 |
RefConst<Shape> mShapePtr; |
| 37 |
Vec3 mPosition; |
| 38 |
Quat mRotation; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
uint32 mUserData = 0; |
| 44 |
}; |
| 45 |
|
| 46 |
using SubShapes = Array<SubShapeSettings>; |
| 47 |
|
| 48 |
SubShapes mSubShapes; |
| 49 |
}; |
| 50 |
|
| 51 |
|
| 52 |
class JPH_EXPORT CompoundShape : public Shape |
| 53 |
{ |
| 54 |
public: |
| 55 |
JPH_OVERRIDE_NEW_DELETE |
| 56 |
|
| 57 |
|
| 58 |
explicit CompoundShape(EShapeSubType inSubType) : Shape(EShapeType::Compound, inSubType) { } |
| 59 |
CompoundShape(EShapeSubType inSubType, const ShapeSettings &inSettings, ShapeResult &outResult) : Shape(EShapeType::Compound, inSubType, inSettings, outResult) { } |
| 60 |
|
| 61 |
|
| 62 |
virtual Vec3 GetCenterOfMass() const override { return mCenterOfMass; } |
| 63 |
|
| 64 |
|
| 65 |
virtual bool MustBeStatic() const override; |
| 66 |
|
| 67 |
|
| 68 |
virtual AABox GetLocalBounds() const override { return mLocalBounds; } |
| 69 |
|
| 70 |
|
| 71 |
virtual uint GetSubShapeIDBitsRecursive() const override; |
| 72 |
|
| 73 |
|
| 74 |
virtual AABox GetWorldSpaceBounds(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale) const override; |
| 75 |
using Shape::GetWorldSpaceBounds; |
| 76 |
|
| 77 |
|
| 78 |
virtual float GetInnerRadius() const override { return mInnerRadius; } |
| 79 |
|
| 80 |
|
| 81 |
virtual MassProperties GetMassProperties() const override; |
| 82 |
|
| 83 |
|
| 84 |
virtual const PhysicsMaterial * GetMaterial(const SubShapeID &inSubShapeID) const override; |
| 85 |
|
| 86 |
|
| 87 |
virtual const Shape * GetLeafShape(const SubShapeID &inSubShapeID, SubShapeID &outRemainder) const override; |
| 88 |
|
| 89 |
|
| 90 |
virtual uint64 GetSubShapeUserData(const SubShapeID &inSubShapeID) const override; |
| 91 |
|
| 92 |
|
| 93 |
virtual TransformedShape GetSubShapeTransformedShape(const SubShapeID &inSubShapeID, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale, SubShapeID &outRemainder) const override; |
| 94 |
|
| 95 |
|
| 96 |
virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override; |
| 97 |
|
| 98 |
|
| 99 |
virtual void GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const override; |
| 100 |
|
| 101 |
|
| 102 |
virtual void GetSubmergedVolume(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy JPH_IF_DEBUG_RENDERER(, RVec3Arg inBaseOffset)) const override; |
| 103 |
|
| 104 |
#ifdef JPH_DEBUG_RENDERER |
| 105 |
|
| 106 |
virtual void Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override; |
| 107 |
|
| 108 |
|
| 109 |
virtual void DrawGetSupportFunction(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inDrawSupportDirection) const override; |
| 110 |
|
| 111 |
|
| 112 |
virtual void DrawGetSupportingFace(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale) const override; |
| 113 |
#endif |
| 114 |
|
| 115 |
|
| 116 |
virtual void CollideSoftBodyVertices(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const CollideSoftBodyVertexIterator &inVertices, uint inNumVertices, int inCollidingShapeIndex) const override; |
| 117 |
|
| 118 |
|
| 119 |
virtual void TransformShape(Mat44Arg inCenterOfMassTransform, TransformedShapeCollector &ioCollector) const override; |
| 120 |
|
| 121 |
|
| 122 |
virtual void GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const override { JPH_ASSERT(false, "Cannot call on non-leaf shapes, use CollectTransformedShapes to collect the leaves first!"); } |
| 123 |
|
| 124 |
|
| 125 |
virtual int GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials = nullptr) const override { JPH_ASSERT(false, "Cannot call on non-leaf shapes, use CollectTransformedShapes to collect the leaves first!"); return 0; } |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
virtual int GetIntersectingSubShapes(const AABox &inBox, uint *outSubShapeIndices, int inMaxSubShapeIndices) const = 0; |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
virtual int GetIntersectingSubShapes(const OrientedBox &inBox, uint *outSubShapeIndices, int inMaxSubShapeIndices) const = 0; |
| 140 |
|
| 141 |
struct SubShape |
| 142 |
{ |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
bool FromSettings(const CompoundShapeSettings::SubShapeSettings &inSettings, ShapeResult &outResult) |
| 148 |
{ |
| 149 |
if (inSettings.mShapePtr != nullptr) |
| 150 |
{ |
| 151 |
|
| 152 |
mShape = inSettings.mShapePtr; |
| 153 |
} |
| 154 |
else |
| 155 |
{ |
| 156 |
|
| 157 |
ShapeResult child_result = inSettings.mShape->Create(); |
| 158 |
if (!child_result.IsValid()) |
| 159 |
{ |
| 160 |
outResult = child_result; |
| 161 |
return false; |
| 162 |
} |
| 163 |
mShape = child_result.Get(); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
mUserData = inSettings.mUserData; |
| 168 |
|
| 169 |
SetTransform(inSettings.mPosition, inSettings.mRotation, Vec3::sZero() ); |
| 170 |
return true; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
JPH_INLINE void SetTransform(Vec3Arg inPosition, QuatArg inRotation, Vec3Arg inCenterOfMass) |
| 178 |
{ |
| 179 |
SetPositionCOM(inPosition - inCenterOfMass + inRotation * mShape->GetCenterOfMass()); |
| 180 |
|
| 181 |
mIsRotationIdentity = inRotation.IsClose(Quat::sIdentity()) || inRotation.IsClose(-Quat::sIdentity()); |
| 182 |
SetRotation(mIsRotationIdentity? Quat::sIdentity() : inRotation); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
JPH_INLINE Mat44 GetLocalTransformNoScale(Vec3Arg inScale) const |
| 189 |
{ |
| 190 |
JPH_ASSERT(IsValidScale(inScale)); |
| 191 |
return Mat44::sRotationTranslation(GetRotation(), inScale * GetPositionCOM()); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
inline bool IsValidScale(Vec3Arg inScale) const |
| 196 |
{ |
| 197 |
|
| 198 |
if (mIsRotationIdentity || ScaleHelpers::IsUniformScale(inScale)) |
| 199 |
return true; |
| 200 |
|
| 201 |
return ScaleHelpers::CanScaleBeRotated(GetRotation(), inScale); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
inline Vec3 TransformScale(Vec3Arg inScale) const |
| 206 |
{ |
| 207 |
|
| 208 |
if (mIsRotationIdentity || ScaleHelpers::IsUniformScale(inScale)) |
| 209 |
return inScale; |
| 210 |
|
| 211 |
return ScaleHelpers::RotateScale(GetRotation(), inScale); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
JPH_INLINE void SetPositionCOM(Vec3Arg inPositionCOM) |
| 216 |
{ |
| 217 |
inPositionCOM.StoreFloat3(&mPositionCOM); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
JPH_INLINE Vec3 GetPositionCOM() const |
| 222 |
{ |
| 223 |
return Vec3::sLoadFloat3Unsafe(mPositionCOM); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
JPH_INLINE void SetRotation(QuatArg inRotation) |
| 228 |
{ |
| 229 |
inRotation.StoreFloat3(&mRotation); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
JPH_INLINE Quat GetRotation() const |
| 234 |
{ |
| 235 |
return mIsRotationIdentity? Quat::sIdentity() : Quat::sLoadFloat3Unsafe(mRotation); |
| 236 |
} |
| 237 |
|
| 238 |
RefConst<Shape> mShape; |
| 239 |
Float3 mPositionCOM; |
| 240 |
Float3 mRotation; |
| 241 |
uint32 mUserData; |
| 242 |
bool mIsRotationIdentity; |
| 243 |
|
| 244 |
}; |
| 245 |
|
| 246 |
static_assert(sizeof(SubShape) == (JPH_CPU_ADDRESS_BITS == 64? 40 : 36), "Compiler added unexpected padding"); |
| 247 |
|
| 248 |
using SubShapes = Array<SubShape>; |
| 249 |
|
| 250 |
|
| 251 |
const SubShapes & GetSubShapes() const { return mSubShapes; } |
| 252 |
|
| 253 |
|
| 254 |
uint GetNumSubShapes() const { return uint(mSubShapes.size()); } |
| 255 |
|
| 256 |
|
| 257 |
const SubShape & GetSubShape(uint inIdx) const { return mSubShapes[inIdx]; } |
| 258 |
|
| 259 |
|
| 260 |
uint32 GetCompoundUserData(uint inIdx) const { return mSubShapes[inIdx].mUserData; } |
| 261 |
|
| 262 |
|
| 263 |
void SetCompoundUserData(uint inIdx, uint32 inUserData) { mSubShapes[inIdx].mUserData = inUserData; } |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
inline bool IsSubShapeIDValid(SubShapeID inSubShapeID) const |
| 269 |
{ |
| 270 |
SubShapeID remainder; |
| 271 |
return inSubShapeID.PopID(GetSubShapeIDBits(), remainder) < mSubShapes.size(); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
inline uint32 GetSubShapeIndexFromID(SubShapeID inSubShapeID, SubShapeID &outRemainder) const |
| 279 |
{ |
| 280 |
uint32 idx = inSubShapeID.PopID(GetSubShapeIDBits(), outRemainder); |
| 281 |
JPH_ASSERT(idx < mSubShapes.size(), "Invalid SubShapeID"); |
| 282 |
return idx; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
inline SubShapeIDCreator GetSubShapeIDFromIndex(int inIdx, const SubShapeIDCreator &inParentSubShapeID) const |
| 290 |
{ |
| 291 |
return inParentSubShapeID.PushID(inIdx, GetSubShapeIDBits()); |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
virtual void SaveBinaryState(StreamOut &inStream) const override; |
| 296 |
virtual void SaveSubShapeState(ShapeList &outSubShapes) const override; |
| 297 |
virtual void RestoreSubShapeState(const ShapeRefC *inSubShapes, uint inNumShapes) override; |
| 298 |
|
| 299 |
|
| 300 |
virtual Stats GetStatsRecursive(VisitedShapes &ioVisitedShapes) const override; |
| 301 |
|
| 302 |
|
| 303 |
virtual float GetVolume() const override; |
| 304 |
|
| 305 |
|
| 306 |
virtual bool IsValidScale(Vec3Arg inScale) const override; |
| 307 |
|
| 308 |
|
| 309 |
virtual Vec3 MakeScaleValid(Vec3Arg inScale) const override; |
| 310 |
|
| 311 |
|
| 312 |
static void sRegister(); |
| 313 |
|
| 314 |
protected: |
| 315 |
|
| 316 |
virtual void RestoreBinaryState(StreamIn &inStream) override; |
| 317 |
|
| 318 |
|
| 319 |
struct CastRayVisitor; |
| 320 |
struct CastRayVisitorCollector; |
| 321 |
struct CollidePointVisitor; |
| 322 |
struct CastShapeVisitor; |
| 323 |
struct CollectTransformedShapesVisitor; |
| 324 |
struct CollideCompoundVsShapeVisitor; |
| 325 |
struct CollideShapeVsCompoundVisitor; |
| 326 |
template <class BoxType> struct GetIntersectingSubShapesVisitor; |
| 327 |
|
| 328 |
|
| 329 |
inline uint GetSubShapeIDBits() const |
| 330 |
{ |
| 331 |
|
| 332 |
uint32 n = uint32(mSubShapes.size()) - 1; |
| 333 |
return 32 - CountLeadingZeros(n); |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
inline void CalculateInnerRadius() |
| 338 |
{ |
| 339 |
mInnerRadius = FLT_MAX; |
| 340 |
for (const SubShape &s : mSubShapes) |
| 341 |
mInnerRadius = min(mInnerRadius, s.mShape->GetInnerRadius()); |
| 342 |
} |
| 343 |
|
| 344 |
Vec3 mCenterOfMass { Vec3::sZero() }; |
| 345 |
AABox mLocalBounds { Vec3::sZero(), Vec3::sZero() }; |
| 346 |
SubShapes mSubShapes; |
| 347 |
float mInnerRadius = FLT_MAX; |
| 348 |
|
| 349 |
private: |
| 350 |
|
| 351 |
static void sCastCompoundVsShape(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector); |
| 352 |
}; |
| 353 |
|
| 354 |
JPH_NAMESPACE_END |