TaperedCapsuleShape.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 TaperedCapsuleShapeSettings final : public ConvexShapeSettings
  12. {
  13. JPH_DECLARE_SERIALIZABLE_VIRTUAL(TaperedCapsuleShapeSettings)
  14. /// Default constructor for deserialization
  15. TaperedCapsuleShapeSettings() = default;
  16. /// 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
  17. TaperedCapsuleShapeSettings(float inHalfHeightOfTaperedCylinder, float inTopRadius, float inBottomRadius, const PhysicsMaterial *inMaterial = nullptr);
  18. /// Check if the settings are valid
  19. bool IsValid() const { return mTopRadius > 0.0f && mBottomRadius > 0.0f && mHalfHeightOfTaperedCylinder >= 0.0f; }
  20. /// Checks if the settings of this tapered capsule make this shape a sphere
  21. bool IsSphere() const;
  22. // See: ShapeSettings
  23. virtual ShapeResult Create() const override;
  24. float mHalfHeightOfTaperedCylinder = 0.0f;
  25. float mTopRadius = 0.0f;
  26. float mBottomRadius = 0.0f;
  27. };
  28. /// A capsule with different top and bottom radii
  29. class TaperedCapsuleShape final : public ConvexShape
  30. {
  31. public:
  32. JPH_OVERRIDE_NEW_DELETE
  33. /// Constructor
  34. TaperedCapsuleShape() : ConvexShape(EShapeSubType::TaperedCapsule) { }
  35. TaperedCapsuleShape(const TaperedCapsuleShapeSettings &inSettings, ShapeResult &outResult);
  36. // See Shape::GetCenterOfMass
  37. virtual Vec3 GetCenterOfMass() const override { return mCenterOfMass; }
  38. // See Shape::GetLocalBounds
  39. virtual AABox GetLocalBounds() const override;
  40. // See Shape::GetWorldSpaceBounds
  41. virtual AABox GetWorldSpaceBounds(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale) const override;
  42. using Shape::GetWorldSpaceBounds;
  43. // See Shape::GetInnerRadius
  44. virtual float GetInnerRadius() const override { return min(mTopRadius, mBottomRadius); }
  45. // See Shape::GetMassProperties
  46. virtual MassProperties GetMassProperties() const override;
  47. // See Shape::GetSurfaceNormal
  48. virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override;
  49. // See Shape::GetSupportingFace
  50. virtual void GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const override;
  51. // See ConvexShape::GetSupportFunction
  52. virtual const Support * GetSupportFunction(ESupportMode inMode, SupportBuffer &inBuffer, Vec3Arg inScale) const override;
  53. #ifdef JPH_DEBUG_RENDERER
  54. // See Shape::Draw
  55. virtual void Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override;
  56. #endif // JPH_DEBUG_RENDERER
  57. // See Shape::TransformShape
  58. virtual void TransformShape(Mat44Arg inCenterOfMassTransform, TransformedShapeCollector &ioCollector) const override;
  59. // See Shape
  60. virtual void SaveBinaryState(StreamOut &inStream) const override;
  61. // See Shape::GetStats
  62. virtual Stats GetStats() const override { return Stats(sizeof(*this), 0); }
  63. // See Shape::GetVolume
  64. virtual float GetVolume() const override { return GetLocalBounds().GetVolume(); } // Volume is approximate!
  65. // See Shape::IsValidScale
  66. virtual bool IsValidScale(Vec3Arg inScale) const override;
  67. // Register shape functions with the registry
  68. static void sRegister();
  69. protected:
  70. // See: Shape::RestoreBinaryState
  71. virtual void RestoreBinaryState(StreamIn &inStream) override;
  72. private:
  73. // Class for GetSupportFunction
  74. class TaperedCapsule;
  75. /// Returns box that approximates the inertia
  76. AABox GetInertiaApproximation() const;
  77. Vec3 mCenterOfMass = Vec3::sZero();
  78. float mTopRadius = 0.0f;
  79. float mBottomRadius = 0.0f;
  80. float mTopCenter = 0.0f;
  81. float mBottomCenter = 0.0f;
  82. float mConvexRadius = 0.0f;
  83. float mSinAlpha = 0.0f;
  84. float mTanAlpha = 0.0f;
  85. #ifdef JPH_DEBUG_RENDERER
  86. mutable DebugRenderer::GeometryRef mGeometry;
  87. #endif // JPH_DEBUG_RENDERER
  88. };
  89. JPH_NAMESPACE_END