TaperedCapsuleShape.h 4.1 KB

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