CylinderShape.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include <Jolt/Physics/PhysicsSettings.h>
  7. JPH_NAMESPACE_BEGIN
  8. /// Class that constructs a CylinderShape
  9. class CylinderShapeSettings final : public ConvexShapeSettings
  10. {
  11. public:
  12. JPH_DECLARE_SERIALIZABLE_VIRTUAL(CylinderShapeSettings)
  13. /// Default constructor for deserialization
  14. CylinderShapeSettings() = default;
  15. /// Create a shape centered around the origin with one top at (0, -inHalfHeight, 0) and the other at (0, inHalfHeight, 0) and radius inRadius.
  16. /// (internally the convex radius will be subtracted from the cylinder the total cylinder will not grow with the convex radius, but the edges of the cylinder will be rounded a bit).
  17. CylinderShapeSettings(float inHalfHeight, float inRadius, float inConvexRadius = cDefaultConvexRadius, const PhysicsMaterial *inMaterial = nullptr) : ConvexShapeSettings(inMaterial), mHalfHeight(inHalfHeight), mRadius(inRadius), mConvexRadius(inConvexRadius) { }
  18. // See: ShapeSettings
  19. virtual ShapeResult Create() const override;
  20. float mHalfHeight = 0.0f;
  21. float mRadius = 0.0f;
  22. float mConvexRadius = 0.0f;
  23. };
  24. /// A cylinder
  25. class CylinderShape final : public ConvexShape
  26. {
  27. public:
  28. JPH_OVERRIDE_NEW_DELETE
  29. /// Constructor
  30. CylinderShape() : ConvexShape(EShapeSubType::Cylinder) { }
  31. CylinderShape(const CylinderShapeSettings &inSettings, ShapeResult &outResult);
  32. /// Create a shape centered around the origin with one top at (0, -inHalfHeight, 0) and the other at (0, inHalfHeight, 0) and radius inRadius.
  33. /// (internally the convex radius will be subtracted from the cylinder the total cylinder will not grow with the convex radius, but the edges of the cylinder will be rounded a bit).
  34. CylinderShape(float inHalfHeight, float inRadius, float inConvexRadius = cDefaultConvexRadius, const PhysicsMaterial *inMaterial = nullptr);
  35. /// Get half height of cylinder
  36. float GetHalfHeight() const { return mHalfHeight; }
  37. /// Get radius of cylinder
  38. float GetRadius() const { return mRadius; }
  39. // See Shape::GetLocalBounds
  40. virtual AABox GetLocalBounds() const override;
  41. // See Shape::GetInnerRadius
  42. virtual float GetInnerRadius() const override { return min(mHalfHeight, 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, RMat44Arg 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 2.0f * JPH_PI * mHalfHeight * Square(mRadius); }
  72. /// Get the convex radius of this cylinder
  73. float GetConvexRadius() const { return mConvexRadius; }
  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. // Class for GetSupportFunction
  83. class Cylinder;
  84. float mHalfHeight = 0.0f;
  85. float mRadius = 0.0f;
  86. float mConvexRadius = 0.0f;
  87. };
  88. JPH_NAMESPACE_END