TaperedCapsuleShape.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. /// 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 JPH_EXPORT 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. // See: Shape::CollideSoftBodyVertices
  54. virtual void CollideSoftBodyVertices(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, SoftBodyVertex *ioVertices, uint inNumVertices, float inDeltaTime, Vec3Arg inDisplacementDueToGravity, int inCollidingShapeIndex) const override;
  55. #ifdef JPH_DEBUG_RENDERER
  56. // See Shape::Draw
  57. virtual void Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override;
  58. #endif // JPH_DEBUG_RENDERER
  59. // See Shape::TransformShape
  60. virtual void TransformShape(Mat44Arg inCenterOfMassTransform, TransformedShapeCollector &ioCollector) const override;
  61. // See Shape
  62. virtual void SaveBinaryState(StreamOut &inStream) const override;
  63. // See Shape::GetStats
  64. virtual Stats GetStats() const override { return Stats(sizeof(*this), 0); }
  65. // See Shape::GetVolume
  66. virtual float GetVolume() const override { return GetLocalBounds().GetVolume(); } // Volume is approximate!
  67. // See Shape::IsValidScale
  68. virtual bool IsValidScale(Vec3Arg inScale) const override;
  69. // Register shape functions with the registry
  70. static void sRegister();
  71. protected:
  72. // See: Shape::RestoreBinaryState
  73. virtual void RestoreBinaryState(StreamIn &inStream) override;
  74. private:
  75. // Class for GetSupportFunction
  76. class TaperedCapsule;
  77. /// Returns box that approximates the inertia
  78. AABox GetInertiaApproximation() const;
  79. Vec3 mCenterOfMass = Vec3::sZero();
  80. float mTopRadius = 0.0f;
  81. float mBottomRadius = 0.0f;
  82. float mTopCenter = 0.0f;
  83. float mBottomCenter = 0.0f;
  84. float mConvexRadius = 0.0f;
  85. float mSinAlpha = 0.0f;
  86. float mTanAlpha = 0.0f;
  87. #ifdef JPH_DEBUG_RENDERER
  88. mutable DebugRenderer::GeometryRef mGeometry;
  89. #endif // JPH_DEBUG_RENDERER
  90. };
  91. JPH_NAMESPACE_END