TaperedCapsuleShape.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/ConvexShape.h>
  6. #ifdef JPH_DEBUG_RENDERER
  7. #include <Jolt/Renderer/DebugRenderer.h>
  8. #endif // JPH_DEBUG_RENDERER
  9. JPH_NAMESPACE_BEGIN
  10. /// Class that constructs a TaperedCapsuleShape
  11. class JPH_EXPORT TaperedCapsuleShapeSettings final : public ConvexShapeSettings
  12. {
  13. JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, TaperedCapsuleShapeSettings)
  14. public:
  15. /// Default constructor for deserialization
  16. TaperedCapsuleShapeSettings() = default;
  17. /// Create a tapered capsule centered around the origin with one sphere cap at (0, -inHalfHeightOfTaperedCylinder, 0) with radius inBottomRadius and the other at (0, inHalfHeightOfTaperedCylinder, 0) with radius inTopRadius
  18. TaperedCapsuleShapeSettings(float inHalfHeightOfTaperedCylinder, float inTopRadius, float inBottomRadius, const PhysicsMaterial *inMaterial = nullptr);
  19. /// Check if the settings are valid
  20. bool IsValid() const { return mTopRadius > 0.0f && mBottomRadius > 0.0f && mHalfHeightOfTaperedCylinder >= 0.0f; }
  21. /// Checks if the settings of this tapered capsule make this shape a sphere
  22. bool IsSphere() const;
  23. // See: ShapeSettings
  24. virtual ShapeResult Create() const override;
  25. float mHalfHeightOfTaperedCylinder = 0.0f;
  26. float mTopRadius = 0.0f;
  27. float mBottomRadius = 0.0f;
  28. };
  29. /// A capsule with different top and bottom radii
  30. class JPH_EXPORT TaperedCapsuleShape final : public ConvexShape
  31. {
  32. public:
  33. JPH_OVERRIDE_NEW_DELETE
  34. /// Constructor
  35. TaperedCapsuleShape() : ConvexShape(EShapeSubType::TaperedCapsule) { }
  36. TaperedCapsuleShape(const TaperedCapsuleShapeSettings &inSettings, ShapeResult &outResult);
  37. /// Get top radius of the tapered capsule
  38. inline float GetTopRadius() const { return mTopRadius; }
  39. /// Get bottom radius of the tapered capsule
  40. inline float GetBottomRadius() const { return mBottomRadius; }
  41. /// Get half height between the top and bottom sphere center
  42. inline float GetHalfHeight() const { return 0.5f * (mTopCenter - mBottomCenter); }
  43. // See Shape::GetCenterOfMass
  44. virtual Vec3 GetCenterOfMass() const override { return mCenterOfMass; }
  45. // See Shape::GetLocalBounds
  46. virtual AABox GetLocalBounds() const override;
  47. // See Shape::GetWorldSpaceBounds
  48. virtual AABox GetWorldSpaceBounds(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale) const override;
  49. using Shape::GetWorldSpaceBounds;
  50. // See Shape::GetInnerRadius
  51. virtual float GetInnerRadius() const override { return min(mTopRadius, mBottomRadius); }
  52. // See Shape::GetMassProperties
  53. virtual MassProperties GetMassProperties() const override;
  54. // See Shape::GetSurfaceNormal
  55. virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override;
  56. // See Shape::GetSupportingFace
  57. virtual void GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const override;
  58. // See ConvexShape::GetSupportFunction
  59. virtual const Support * GetSupportFunction(ESupportMode inMode, SupportBuffer &inBuffer, Vec3Arg inScale) const override;
  60. // See: Shape::CollideSoftBodyVertices
  61. virtual void CollideSoftBodyVertices(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const CollideSoftBodyVertexIterator &inVertices, uint inNumVertices, int inCollidingShapeIndex) const override;
  62. #ifdef JPH_DEBUG_RENDERER
  63. // See Shape::Draw
  64. virtual void Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override;
  65. #endif // JPH_DEBUG_RENDERER
  66. // See Shape
  67. virtual void SaveBinaryState(StreamOut &inStream) const override;
  68. // See Shape::GetStats
  69. virtual Stats GetStats() const override { return Stats(sizeof(*this), 0); }
  70. // See Shape::GetVolume
  71. virtual float GetVolume() const override { return GetLocalBounds().GetVolume(); } // Volume is approximate!
  72. // See Shape::IsValidScale
  73. virtual bool IsValidScale(Vec3Arg inScale) const override;
  74. // See Shape::MakeScaleValid
  75. virtual Vec3 MakeScaleValid(Vec3Arg inScale) const override;
  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. // Class for GetSupportFunction
  83. class TaperedCapsule;
  84. /// Returns box that approximates the inertia
  85. AABox GetInertiaApproximation() const;
  86. Vec3 mCenterOfMass = Vec3::sZero();
  87. float mTopRadius = 0.0f;
  88. float mBottomRadius = 0.0f;
  89. float mTopCenter = 0.0f;
  90. float mBottomCenter = 0.0f;
  91. float mConvexRadius = 0.0f;
  92. float mSinAlpha = 0.0f;
  93. float mTanAlpha = 0.0f;
  94. #ifdef JPH_DEBUG_RENDERER
  95. mutable DebugRenderer::GeometryRef mGeometry;
  96. #endif // JPH_DEBUG_RENDERER
  97. };
  98. JPH_NAMESPACE_END