CylinderShape.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Physics/Collision/Shape/ConvexShape.h>
  5. #include <Physics/PhysicsSettings.h>
  6. namespace JPH {
  7. /// Class that constructs a CylinderShape
  8. class CylinderShapeSettings final : public ConvexShapeSettings
  9. {
  10. public:
  11. JPH_DECLARE_SERIALIZABLE_VIRTUAL(CylinderShapeSettings)
  12. /// Default constructor for deserialization
  13. CylinderShapeSettings() = default;
  14. /// Create a shape centered around the origin with one top at (0, -inHalfHeight, 0) and the other at (0, inHalfHeight, 0) and radius inRadius.
  15. /// (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).
  16. CylinderShapeSettings(float inHalfHeight, float inRadius, float inConvexRadius = cDefaultConvexRadius, const PhysicsMaterial *inMaterial = nullptr) : ConvexShapeSettings(inMaterial), mHalfHeight(inHalfHeight), mRadius(inRadius), mConvexRadius(inConvexRadius) { }
  17. // See: ShapeSettings
  18. virtual ShapeResult Create() const override;
  19. float mHalfHeight = 0.0f;
  20. float mRadius = 0.0f;
  21. float mConvexRadius = 0.0f;
  22. };
  23. /// A cylinder
  24. class CylinderShape final : public ConvexShape
  25. {
  26. public:
  27. /// Constructor
  28. CylinderShape() : ConvexShape(EShapeSubType::Cylinder) { }
  29. CylinderShape(const CylinderShapeSettings &inSettings, ShapeResult &outResult);
  30. /// Create a shape centered around the origin with one top at (0, -inHalfHeight, 0) and the other at (0, inHalfHeight, 0) and radius inRadius.
  31. /// (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).
  32. CylinderShape(float inHalfHeight, float inRadius, float inConvexRadius = cDefaultConvexRadius, const PhysicsMaterial *inMaterial = nullptr);
  33. /// Get half height of cylinder
  34. float GetHalfHeight() const { return mHalfHeight; }
  35. /// Get radius of cylinder
  36. float GetRadius() const { return mRadius; }
  37. // See Shape::GetLocalBounds
  38. virtual AABox GetLocalBounds() const override;
  39. // See Shape::GetInnerRadius
  40. virtual float GetInnerRadius() const override { return min(mHalfHeight, mRadius); }
  41. // See Shape::GetMassProperties
  42. virtual MassProperties GetMassProperties() const override;
  43. // See Shape::GetSurfaceNormal
  44. virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override;
  45. // See ConvexShape::GetSupportFunction
  46. virtual const Support * GetSupportFunction(ESupportMode inMode, SupportBuffer &inBuffer, Vec3Arg inScale) const override;
  47. // See ConvexShape::GetSupportingFace
  48. virtual void GetSupportingFace(Vec3Arg inDirection, Vec3Arg inScale, SupportingFace &outVertices) const override;
  49. #ifdef JPH_DEBUG_RENDERER
  50. // See Shape::Draw
  51. virtual void Draw(DebugRenderer *inRenderer, Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override;
  52. #endif // JPH_DEBUG_RENDERER
  53. // See Shape::CastRay
  54. virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;
  55. // See: Shape::CollidePoint
  56. virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector) const override;
  57. // See Shape::TransformShape
  58. virtual void TransformShape(Mat44Arg inCenterOfMassTransform, TransformedShapeCollector &ioCollector) const override;
  59. // See Shape::GetTrianglesStart
  60. virtual void GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const override;
  61. // See Shape::GetTrianglesNext
  62. virtual int GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials = nullptr) const override;
  63. // See Shape
  64. virtual void SaveBinaryState(StreamOut &inStream) const override;
  65. // See Shape::GetStats
  66. virtual Stats GetStats() const override { return Stats(sizeof(*this), 0); }
  67. // See Shape::GetVolume
  68. virtual float GetVolume() const override { return 2.0f * JPH_PI * mHalfHeight * Square(mRadius); }
  69. // See Shape::IsValidScale
  70. virtual bool IsValidScale(Vec3Arg inScale) const override;
  71. // Register shape functions with the registry
  72. static void sRegister();
  73. protected:
  74. // See: Shape::RestoreBinaryState
  75. virtual void RestoreBinaryState(StreamIn &inStream) override;
  76. private:
  77. // Class for GetSupportFunction
  78. class Cylinder;
  79. float mHalfHeight = 0.0f;
  80. float mRadius = 0.0f;
  81. float mConvexRadius = 0.0f;
  82. };
  83. } // JPH