OffsetCenterOfMassShape.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Physics/Collision/Shape/DecoratedShape.h>
  6. JPH_NAMESPACE_BEGIN
  7. class CollideShapeSettings;
  8. /// Class that constructs an OffsetCenterOfMassShape
  9. class JPH_EXPORT OffsetCenterOfMassShapeSettings final : public DecoratedShapeSettings
  10. {
  11. public:
  12. JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, OffsetCenterOfMassShapeSettings)
  13. /// Constructor
  14. OffsetCenterOfMassShapeSettings() = default;
  15. /// Construct with shape settings, can be serialized.
  16. OffsetCenterOfMassShapeSettings(Vec3Arg inOffset, const ShapeSettings *inShape) : DecoratedShapeSettings(inShape), mOffset(inOffset) { }
  17. /// Variant that uses a concrete shape, which means this object cannot be serialized.
  18. OffsetCenterOfMassShapeSettings(Vec3Arg inOffset, const Shape *inShape): DecoratedShapeSettings(inShape), mOffset(inOffset) { }
  19. // See: ShapeSettings
  20. virtual ShapeResult Create() const override;
  21. Vec3 mOffset; ///< Offset to be applied to the center of mass of the child shape
  22. };
  23. /// This shape will shift the center of mass of a child shape, it can e.g. be used to lower the center of mass of an unstable object like a boat to make it stable
  24. class JPH_EXPORT OffsetCenterOfMassShape final : public DecoratedShape
  25. {
  26. public:
  27. JPH_OVERRIDE_NEW_DELETE
  28. /// Constructor
  29. OffsetCenterOfMassShape() : DecoratedShape(EShapeSubType::OffsetCenterOfMass) { }
  30. OffsetCenterOfMassShape(const OffsetCenterOfMassShapeSettings &inSettings, ShapeResult &outResult);
  31. OffsetCenterOfMassShape(const Shape *inShape, Vec3Arg inOffset) : DecoratedShape(EShapeSubType::OffsetCenterOfMass, inShape), mOffset(inOffset) { }
  32. /// Access the offset that is applied to the center of mass
  33. Vec3 GetOffset() const { return mOffset; }
  34. // See Shape::GetCenterOfMass
  35. virtual Vec3 GetCenterOfMass() const override { return mInnerShape->GetCenterOfMass() + mOffset; }
  36. // See Shape::GetLocalBounds
  37. virtual AABox GetLocalBounds() const override;
  38. // See Shape::GetWorldSpaceBounds
  39. virtual AABox GetWorldSpaceBounds(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale) const override;
  40. using Shape::GetWorldSpaceBounds;
  41. // See Shape::GetInnerRadius
  42. virtual float GetInnerRadius() const override { return mInnerShape->GetInnerRadius(); }
  43. // See Shape::GetMassProperties
  44. virtual MassProperties GetMassProperties() const override
  45. {
  46. MassProperties mp = mInnerShape->GetMassProperties();
  47. mp.Translate(mOffset);
  48. return mp;
  49. }
  50. // See Shape::GetSubShapeTransformedShape
  51. virtual TransformedShape GetSubShapeTransformedShape(const SubShapeID &inSubShapeID, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale, SubShapeID &outRemainder) const override;
  52. // See Shape::GetSurfaceNormal
  53. virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override;
  54. // See Shape::GetSupportingFace
  55. virtual void GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const override;
  56. // See Shape::GetSubmergedVolume
  57. virtual void GetSubmergedVolume(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy JPH_IF_DEBUG_RENDERER(, RVec3Arg inBaseOffset)) const override;
  58. #ifdef JPH_DEBUG_RENDERER
  59. // See Shape::Draw
  60. virtual void Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override;
  61. // See Shape::DrawGetSupportFunction
  62. virtual void DrawGetSupportFunction(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inDrawSupportDirection) const override;
  63. // See Shape::DrawGetSupportingFace
  64. virtual void DrawGetSupportingFace(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale) const override;
  65. #endif // JPH_DEBUG_RENDERER
  66. // See Shape::CastRay
  67. virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;
  68. virtual void CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
  69. // See: Shape::CollidePoint
  70. virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
  71. // See: Shape::CollideSoftBodyVertices
  72. virtual void CollideSoftBodyVertices(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, SoftBodyVertex *ioVertices, uint inNumVertices, float inDeltaTime, Vec3Arg inDisplacementDueToGravity, int inCollidingShapeIndex) const override;
  73. // See Shape::CollectTransformedShapes
  74. virtual void CollectTransformedShapes(const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale, const SubShapeIDCreator &inSubShapeIDCreator, TransformedShapeCollector &ioCollector, const ShapeFilter &inShapeFilter) const override;
  75. // See Shape::TransformShape
  76. virtual void TransformShape(Mat44Arg inCenterOfMassTransform, TransformedShapeCollector &ioCollector) const override;
  77. // See Shape::GetTrianglesStart
  78. 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!"); }
  79. // See Shape::GetTrianglesNext
  80. 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; }
  81. // See Shape
  82. virtual void SaveBinaryState(StreamOut &inStream) const override;
  83. // See Shape::GetStats
  84. virtual Stats GetStats() const override { return Stats(sizeof(*this), 0); }
  85. // See Shape::GetVolume
  86. virtual float GetVolume() const override { return mInnerShape->GetVolume(); }
  87. // See Shape::IsValidScale
  88. virtual bool IsValidScale(Vec3Arg inScale) const override { return mInnerShape->IsValidScale(inScale); }
  89. // Register shape functions with the registry
  90. static void sRegister();
  91. protected:
  92. // See: Shape::RestoreBinaryState
  93. virtual void RestoreBinaryState(StreamIn &inStream) override;
  94. private:
  95. // Helper functions called by CollisionDispatch
  96. static void sCollideOffsetCenterOfMassVsShape(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);
  97. static void sCollideShapeVsOffsetCenterOfMass(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);
  98. static void sCastOffsetCenterOfMassVsShape(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector);
  99. static void sCastShapeVsOffsetCenterOfMass(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector);
  100. Vec3 mOffset; ///< Offset of the center of mass
  101. };
  102. JPH_NAMESPACE_END