CapsuleShape.h 5.2 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. JPH_NAMESPACE_BEGIN
  7. /// Class that constructs a CapsuleShape
  8. class CapsuleShapeSettings final : public ConvexShapeSettings
  9. {
  10. JPH_DECLARE_SERIALIZABLE_VIRTUAL(CapsuleShapeSettings)
  11. /// Default constructor for deserialization
  12. CapsuleShapeSettings() = default;
  13. /// Create a capsule centered around the origin with one sphere cap at (0, -inHalfHeightOfCylinder, 0) and the other at (0, inHalfHeightOfCylinder, 0)
  14. CapsuleShapeSettings(float inHalfHeightOfCylinder, float inRadius, const PhysicsMaterial *inMaterial = nullptr) : ConvexShapeSettings(inMaterial), mRadius(inRadius), mHalfHeightOfCylinder(inHalfHeightOfCylinder) { }
  15. /// Check if this is a valid capsule shape
  16. bool IsValid() const { return mRadius > 0.0f && mHalfHeightOfCylinder >= 0.0f; }
  17. /// Checks if the settings of this capsule make this shape a sphere
  18. bool IsSphere() const { return mHalfHeightOfCylinder == 0.0f; }
  19. // See: ShapeSettings
  20. virtual ShapeResult Create() const override;
  21. float mRadius = 0.0f;
  22. float mHalfHeightOfCylinder = 0.0f;
  23. };
  24. /// A capsule, implemented as a line segment with convex radius
  25. class CapsuleShape final : public ConvexShape
  26. {
  27. public:
  28. JPH_OVERRIDE_NEW_DELETE
  29. /// Constructor
  30. CapsuleShape() : ConvexShape(EShapeSubType::Capsule) { }
  31. CapsuleShape(const CapsuleShapeSettings &inSettings, ShapeResult &outResult);
  32. /// Create a capsule centered around the origin with one sphere cap at (0, -inHalfHeightOfCylinder, 0) and the other at (0, inHalfHeightOfCylinder, 0)
  33. 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); }
  34. /// Radius of the cylinder
  35. float GetRadius() const { return mRadius; }
  36. /// Get half of the height of the cylinder
  37. float GetHalfHeightOfCylinder() const { return mHalfHeightOfCylinder; }
  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 mRadius; }
  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::CastRay
  58. using ConvexShape::CastRay;
  59. virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;
  60. // See: Shape::CollidePoint
  61. virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
  62. // See Shape::TransformShape
  63. virtual void TransformShape(Mat44Arg inCenterOfMassTransform, TransformedShapeCollector &ioCollector) const override;
  64. // See Shape::GetTrianglesStart
  65. virtual void GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const override;
  66. // See Shape::GetTrianglesNext
  67. virtual int GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials = nullptr) const override;
  68. // See Shape
  69. virtual void SaveBinaryState(StreamOut &inStream) const override;
  70. // See Shape::GetStats
  71. virtual Stats GetStats() const override { return Stats(sizeof(*this), 0); }
  72. // See Shape::GetVolume
  73. virtual float GetVolume() const override { return 4.0f / 3.0f * JPH_PI * Cubed(mRadius) + 2.0f * JPH_PI * mHalfHeightOfCylinder * Square(mRadius); }
  74. // See Shape::IsValidScale
  75. virtual bool IsValidScale(Vec3Arg inScale) const override;
  76. // Register shape functions with the registry
  77. static void sRegister();
  78. protected:
  79. // See: Shape::RestoreBinaryState
  80. virtual void RestoreBinaryState(StreamIn &inStream) override;
  81. private:
  82. // Classes for GetSupportFunction
  83. class CapsuleNoConvex;
  84. class CapsuleWithConvex;
  85. float mRadius = 0.0f;
  86. float mHalfHeightOfCylinder = 0.0f;
  87. };
  88. JPH_NAMESPACE_END