ScaledShape.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #include <Jolt/Physics/Collision/Shape/ScaleHelpers.h>
  7. JPH_NAMESPACE_BEGIN
  8. class SubShapeIDCreator;
  9. class CollideShapeSettings;
  10. /// Class that constructs a ScaledShape
  11. class JPH_EXPORT ScaledShapeSettings final : public DecoratedShapeSettings
  12. {
  13. JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, ScaledShapeSettings)
  14. public:
  15. /// Default constructor for deserialization
  16. ScaledShapeSettings() = default;
  17. /// Constructor that decorates another shape with a scale
  18. ScaledShapeSettings(const ShapeSettings *inShape, Vec3Arg inScale) : DecoratedShapeSettings(inShape), mScale(inScale) { }
  19. /// Variant that uses a concrete shape, which means this object cannot be serialized.
  20. ScaledShapeSettings(const Shape *inShape, Vec3Arg inScale) : DecoratedShapeSettings(inShape), mScale(inScale) { }
  21. // See: ShapeSettings
  22. virtual ShapeResult Create() const override;
  23. Vec3 mScale = Vec3(1, 1, 1);
  24. };
  25. /// A shape that scales a child shape in local space of that shape. The scale can be non-uniform and can even turn it inside out when one or three components of the scale are negative.
  26. class JPH_EXPORT ScaledShape final : public DecoratedShape
  27. {
  28. public:
  29. JPH_OVERRIDE_NEW_DELETE
  30. /// Constructor
  31. ScaledShape() : DecoratedShape(EShapeSubType::Scaled) { }
  32. ScaledShape(const ScaledShapeSettings &inSettings, ShapeResult &outResult);
  33. /// Constructor that decorates another shape with a scale
  34. ScaledShape(const Shape *inShape, Vec3Arg inScale) : DecoratedShape(EShapeSubType::Scaled, inShape), mScale(inScale) { JPH_ASSERT(!ScaleHelpers::IsZeroScale(mScale)); }
  35. /// Get the scale
  36. Vec3 GetScale() const { return mScale; }
  37. // See Shape::GetCenterOfMass
  38. virtual Vec3 GetCenterOfMass() const override { return mScale * mInnerShape->GetCenterOfMass(); }
  39. // See Shape::GetLocalBounds
  40. virtual AABox GetLocalBounds() const override;
  41. // See Shape::GetWorldSpaceBounds
  42. virtual AABox GetWorldSpaceBounds(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale) const override;
  43. using Shape::GetWorldSpaceBounds;
  44. // See Shape::GetInnerRadius
  45. virtual float GetInnerRadius() const override { return mScale.ReduceMin() * mInnerShape->GetInnerRadius(); }
  46. // See Shape::GetMassProperties
  47. virtual MassProperties GetMassProperties() const override;
  48. // See Shape::GetSubShapeTransformedShape
  49. virtual TransformedShape GetSubShapeTransformedShape(const SubShapeID &inSubShapeID, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale, SubShapeID &outRemainder) const override;
  50. // See Shape::GetSurfaceNormal
  51. virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override;
  52. // See Shape::GetSupportingFace
  53. virtual void GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const override;
  54. // See Shape::GetSubmergedVolume
  55. virtual void GetSubmergedVolume(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy JPH_IF_DEBUG_RENDERER(, RVec3Arg inBaseOffset)) const override;
  56. #ifdef JPH_DEBUG_RENDERER
  57. // See Shape::Draw
  58. virtual void Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override;
  59. // See Shape::DrawGetSupportFunction
  60. virtual void DrawGetSupportFunction(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inDrawSupportDirection) const override;
  61. // See Shape::DrawGetSupportingFace
  62. virtual void DrawGetSupportingFace(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale) const override;
  63. #endif // JPH_DEBUG_RENDERER
  64. // See Shape::CastRay
  65. virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;
  66. virtual void CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
  67. // See: Shape::CollidePoint
  68. virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
  69. // See: Shape::CollideSoftBodyVertices
  70. virtual void CollideSoftBodyVertices(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const CollideSoftBodyVertexIterator &inVertices, uint inNumVertices, int inCollidingShapeIndex) const override;
  71. // See Shape::CollectTransformedShapes
  72. virtual void CollectTransformedShapes(const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale, const SubShapeIDCreator &inSubShapeIDCreator, TransformedShapeCollector &ioCollector, const ShapeFilter &inShapeFilter) const override;
  73. // See Shape::TransformShape
  74. virtual void TransformShape(Mat44Arg inCenterOfMassTransform, TransformedShapeCollector &ioCollector) const override;
  75. // See Shape::GetTrianglesStart
  76. 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!"); }
  77. // See Shape::GetTrianglesNext
  78. 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; }
  79. // See Shape
  80. virtual void SaveBinaryState(StreamOut &inStream) const override;
  81. // See Shape::GetStats
  82. virtual Stats GetStats() const override { return Stats(sizeof(*this), 0); }
  83. // See Shape::GetVolume
  84. virtual float GetVolume() const override;
  85. // See Shape::IsValidScale
  86. virtual bool IsValidScale(Vec3Arg inScale) const override;
  87. // See Shape::MakeScaleValid
  88. virtual Vec3 MakeScaleValid(Vec3Arg inScale) const override;
  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 sCollideScaledVsShape(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 sCollideShapeVsScaled(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 sCastScaledVsShape(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 sCastShapeVsScaled(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 mScale = Vec3(1, 1, 1);
  101. };
  102. JPH_NAMESPACE_END