TaperedCapsuleShape.h 4.3 KB

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