OffsetCenterOfMassShape.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Physics/Collision/Shape/DecoratedShape.h>
  5. JPH_NAMESPACE_BEGIN
  6. class CollideShapeSettings;
  7. /// Class that constructs an OffsetCenterOfMassShape
  8. class OffsetCenterOfMassShapeSettings final : public DecoratedShapeSettings
  9. {
  10. public:
  11. JPH_DECLARE_SERIALIZABLE_VIRTUAL(OffsetCenterOfMassShapeSettings)
  12. /// Constructor
  13. OffsetCenterOfMassShapeSettings() = default;
  14. /// Construct with shape settings, can be serialized.
  15. OffsetCenterOfMassShapeSettings(Vec3Arg inOffset, const ShapeSettings *inShape) : DecoratedShapeSettings(inShape), mOffset(inOffset) { }
  16. /// Variant that uses a concrete shape, which means this object cannot be serialized.
  17. OffsetCenterOfMassShapeSettings(Vec3Arg inOffset, const Shape *inShape): DecoratedShapeSettings(inShape), mOffset(inOffset) { }
  18. // See: ShapeSettings
  19. virtual ShapeResult Create() const override;
  20. Vec3 mOffset; ///< Offset to be applied to the center of mass of the child shape
  21. };
  22. /// 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
  23. class OffsetCenterOfMassShape final : public DecoratedShape
  24. {
  25. public:
  26. /// Constructor
  27. OffsetCenterOfMassShape() : DecoratedShape(EShapeSubType::OffsetCenterOfMass) { }
  28. OffsetCenterOfMassShape(const OffsetCenterOfMassShapeSettings &inSettings, ShapeResult &outResult);
  29. /// Access the offset that is applied to the center of mass
  30. Vec3 GetOffset() const { return mOffset; }
  31. // See Shape::GetCenterOfMass
  32. virtual Vec3 GetCenterOfMass() const override { return mInnerShape->GetCenterOfMass() + mOffset; }
  33. // See Shape::GetLocalBounds
  34. virtual AABox GetLocalBounds() const override;
  35. // See Shape::GetWorldSpaceBounds
  36. virtual AABox GetWorldSpaceBounds(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale) const override;
  37. // See Shape::GetInnerRadius
  38. virtual float GetInnerRadius() const override { return mInnerShape->GetInnerRadius(); }
  39. // See Shape::GetMassProperties
  40. virtual MassProperties GetMassProperties() const override { return mInnerShape->GetMassProperties(); }
  41. // See Shape::GetSubShapeTransformedShape
  42. virtual TransformedShape GetSubShapeTransformedShape(const SubShapeID &inSubShapeID, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale, SubShapeID &outRemainder) const override;
  43. // See Shape::GetSurfaceNormal
  44. virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override;
  45. // See Shape::GetSubmergedVolume
  46. virtual void GetSubmergedVolume(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy) const override;
  47. #ifdef JPH_DEBUG_RENDERER
  48. // See Shape::Draw
  49. virtual void Draw(DebugRenderer *inRenderer, Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override;
  50. // See Shape::DrawGetSupportFunction
  51. virtual void DrawGetSupportFunction(DebugRenderer *inRenderer, Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inDrawSupportDirection) const override;
  52. // See Shape::DrawGetSupportingFace
  53. virtual void DrawGetSupportingFace(DebugRenderer *inRenderer, Mat44Arg inCenterOfMassTransform, Vec3Arg inScale) const override;
  54. #endif // JPH_DEBUG_RENDERER
  55. // See Shape::CastRay
  56. virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;
  57. virtual void CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector) const override;
  58. // See: Shape::CollidePoint
  59. virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector) const override;
  60. // See Shape::CollectTransformedShapes
  61. virtual void CollectTransformedShapes(const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale, const SubShapeIDCreator &inSubShapeIDCreator, TransformedShapeCollector &ioCollector) const override;
  62. // See Shape::TransformShape
  63. virtual void TransformShape(Mat44Arg inCenterOfMassTransform, TransformedShapeCollector &ioCollector) const override;
  64. // See Shape::GetTrianglesStart
  65. 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!"); }
  66. // See Shape::GetTrianglesNext
  67. 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; }
  68. // See Shape
  69. virtual void SaveBinaryState(StreamOut &inStream) const override;
  70. // See Shape::GetStats
  71. virtual Stats GetStats() const override { return Stats(sizeof(*this), 0); }
  72. // See Shape::GetVolume
  73. virtual float GetVolume() const override { return mInnerShape->GetVolume(); }
  74. // See Shape::IsValidScale
  75. virtual bool IsValidScale(Vec3Arg inScale) const override { return mInnerShape->IsValidScale(inScale); }
  76. // Register shape functions with the registry
  77. static void sRegister();
  78. protected:
  79. // See: Shape::RestoreBinaryState
  80. virtual void RestoreBinaryState(StreamIn &inStream) override;
  81. private:
  82. // Helper functions called by CollisionDispatch
  83. 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);
  84. 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);
  85. 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);
  86. 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);
  87. Vec3 mOffset; ///< Offset of the center of mass
  88. };
  89. JPH_NAMESPACE_END