CapsuleShape.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. JPH_NAMESPACE_BEGIN
  7. /// Class that constructs a CapsuleShape
  8. class JPH_EXPORT CapsuleShapeSettings final : public ConvexShapeSettings
  9. {
  10. JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, CapsuleShapeSettings)
  11. public:
  12. /// Default constructor for deserialization
  13. CapsuleShapeSettings() = default;
  14. /// Create a capsule centered around the origin with one sphere cap at (0, -inHalfHeightOfCylinder, 0) and the other at (0, inHalfHeightOfCylinder, 0)
  15. CapsuleShapeSettings(float inHalfHeightOfCylinder, float inRadius, const PhysicsMaterial *inMaterial = nullptr) : ConvexShapeSettings(inMaterial), mRadius(inRadius), mHalfHeightOfCylinder(inHalfHeightOfCylinder) { }
  16. /// Check if this is a valid capsule shape
  17. bool IsValid() const { return mRadius > 0.0f && mHalfHeightOfCylinder >= 0.0f; }
  18. /// Checks if the settings of this capsule make this shape a sphere
  19. bool IsSphere() const { return mHalfHeightOfCylinder == 0.0f; }
  20. // See: ShapeSettings
  21. virtual ShapeResult Create() const override;
  22. float mRadius = 0.0f;
  23. float mHalfHeightOfCylinder = 0.0f;
  24. };
  25. /// A capsule, implemented as a line segment with convex radius
  26. class JPH_EXPORT CapsuleShape final : public ConvexShape
  27. {
  28. public:
  29. JPH_OVERRIDE_NEW_DELETE
  30. /// Constructor
  31. CapsuleShape() : ConvexShape(EShapeSubType::Capsule) { }
  32. CapsuleShape(const CapsuleShapeSettings &inSettings, ShapeResult &outResult);
  33. /// Create a capsule centered around the origin with one sphere cap at (0, -inHalfHeightOfCylinder, 0) and the other at (0, inHalfHeightOfCylinder, 0)
  34. CapsuleShape(float inHalfHeightOfCylinder, float inRadius, const PhysicsMaterial *inMaterial = nullptr) : ConvexShape(EShapeSubType::Capsule, inMaterial), mRadius(inRadius), mHalfHeightOfCylinder(inHalfHeightOfCylinder) { JPH_ASSERT(inHalfHeightOfCylinder > 0.0f); JPH_ASSERT(inRadius > 0.0f); }
  35. /// Radius of the cylinder
  36. float GetRadius() const { return mRadius; }
  37. /// Get half of the height of the cylinder
  38. float GetHalfHeightOfCylinder() const { return mHalfHeightOfCylinder; }
  39. // See Shape::GetLocalBounds
  40. virtual AABox GetLocalBounds() const override;
  41. // See Shape::GetWorldSpaceBounds
  42. virtual AABox GetWorldSpaceBounds(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale) const override;
  43. using Shape::GetWorldSpaceBounds;
  44. // See Shape::GetInnerRadius
  45. virtual float GetInnerRadius() const override { return mRadius; }
  46. // See Shape::GetMassProperties
  47. virtual MassProperties GetMassProperties() const override;
  48. // See Shape::GetSurfaceNormal
  49. virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override;
  50. // See Shape::GetSupportingFace
  51. virtual void GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const override;
  52. // See ConvexShape::GetSupportFunction
  53. virtual const Support * GetSupportFunction(ESupportMode inMode, SupportBuffer &inBuffer, Vec3Arg inScale) const override;
  54. #ifdef JPH_DEBUG_RENDERER
  55. // See Shape::Draw
  56. virtual void Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override;
  57. #endif // JPH_DEBUG_RENDERER
  58. // See Shape::CastRay
  59. using ConvexShape::CastRay;
  60. virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;
  61. // See: Shape::CollidePoint
  62. virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
  63. // See: Shape::CollideSoftBodyVertices
  64. virtual void CollideSoftBodyVertices(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const CollideSoftBodyVertexIterator &inVertices, uint inNumVertices, int inCollidingShapeIndex) const override;
  65. // See Shape::GetTrianglesStart
  66. virtual void GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const override;
  67. // See Shape::GetTrianglesNext
  68. virtual int GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials = nullptr) const override;
  69. // See Shape
  70. virtual void SaveBinaryState(StreamOut &inStream) const override;
  71. // See Shape::GetStats
  72. virtual Stats GetStats() const override { return Stats(sizeof(*this), 0); }
  73. // See Shape::GetVolume
  74. virtual float GetVolume() const override { return 4.0f / 3.0f * JPH_PI * Cubed(mRadius) + 2.0f * JPH_PI * mHalfHeightOfCylinder * Square(mRadius); }
  75. // See Shape::IsValidScale
  76. virtual bool IsValidScale(Vec3Arg inScale) const override;
  77. // See Shape::MakeScaleValid
  78. virtual Vec3 MakeScaleValid(Vec3Arg inScale) const override;
  79. // Register shape functions with the registry
  80. static void sRegister();
  81. protected:
  82. // See: Shape::RestoreBinaryState
  83. virtual void RestoreBinaryState(StreamIn &inStream) override;
  84. private:
  85. // Classes for GetSupportFunction
  86. class CapsuleNoConvex;
  87. class CapsuleWithConvex;
  88. float mRadius = 0.0f;
  89. float mHalfHeightOfCylinder = 0.0f;
  90. };
  91. JPH_NAMESPACE_END