CapsuleShape.h 5.1 KB

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