TaperedCylinderShape.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2024 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 TaperedCylinderShape
  9. class JPH_EXPORT TaperedCylinderShapeSettings final : public ConvexShapeSettings
  10. {
  11. JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, TaperedCylinderShapeSettings)
  12. public:
  13. /// Default constructor for deserialization
  14. TaperedCylinderShapeSettings() = default;
  15. /// Create a tapered cylinder centered around the origin with bottom at (0, -inHalfHeightOfTaperedCylinder, 0) with radius inBottomRadius and top at (0, inHalfHeightOfTaperedCylinder, 0) with radius inTopRadius
  16. TaperedCylinderShapeSettings(float inHalfHeightOfTaperedCylinder, float inTopRadius, float inBottomRadius, float inConvexRadius = cDefaultConvexRadius, const PhysicsMaterial *inMaterial = nullptr);
  17. // See: ShapeSettings
  18. virtual ShapeResult Create() const override;
  19. float mHalfHeight = 0.0f;
  20. float mTopRadius = 0.0f;
  21. float mBottomRadius = 0.0f;
  22. float mConvexRadius = 0.0f;
  23. };
  24. /// A cylinder with different top and bottom radii
  25. class JPH_EXPORT TaperedCylinderShape final : public ConvexShape
  26. {
  27. public:
  28. JPH_OVERRIDE_NEW_DELETE
  29. /// Constructor
  30. TaperedCylinderShape() : ConvexShape(EShapeSubType::TaperedCylinder) { }
  31. TaperedCylinderShape(const TaperedCylinderShapeSettings &inSettings, ShapeResult &outResult);
  32. /// Get top radius of the tapered cylinder
  33. inline float GetTopRadius() const { return mTopRadius; }
  34. /// Get bottom radius of the tapered cylinder
  35. inline float GetBottomRadius() const { return mBottomRadius; }
  36. /// Get convex radius of the tapered cylinder
  37. inline float GetConvexRadius() const { return mConvexRadius; }
  38. /// Get half height of the tapered cylinder
  39. inline float GetHalfHeight() const { return 0.5f * (mTop - mBottom); }
  40. // See Shape::GetCenterOfMass
  41. virtual Vec3 GetCenterOfMass() const override { return Vec3(0, -0.5f * (mTop + mBottom), 0); }
  42. // See Shape::GetLocalBounds
  43. virtual AABox GetLocalBounds() const override;
  44. // See Shape::GetInnerRadius
  45. virtual float GetInnerRadius() const override { return min(mTopRadius, mBottomRadius); }
  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. // See: Shape::CollidePoint
  55. virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
  56. // See: Shape::CollideSoftBodyVertices
  57. virtual void CollideSoftBodyVertices(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const CollideSoftBodyVertexIterator &inVertices, uint inNumVertices, int inCollidingShapeIndex) const override;
  58. // See Shape::GetTrianglesStart
  59. virtual void GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const override;
  60. // See Shape::GetTrianglesNext
  61. virtual int GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials = nullptr) const override;
  62. #ifdef JPH_DEBUG_RENDERER
  63. // See Shape::Draw
  64. virtual void Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override;
  65. #endif // JPH_DEBUG_RENDERER
  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;
  72. // See Shape::IsValidScale
  73. virtual bool IsValidScale(Vec3Arg inScale) const override;
  74. // See Shape::MakeScaleValid
  75. virtual Vec3 MakeScaleValid(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 TaperedCylinder;
  84. // Class for GetTrianglesTart
  85. class TCSGetTrianglesContext;
  86. // Scale the cylinder
  87. JPH_INLINE void GetScaled(Vec3Arg inScale, float &outTop, float &outBottom, float &outTopRadius, float &outBottomRadius, float &outConvexRadius) const;
  88. float mTop = 0.0f;
  89. float mBottom = 0.0f;
  90. float mTopRadius = 0.0f;
  91. float mBottomRadius = 0.0f;
  92. float mConvexRadius = 0.0f;
  93. };
  94. JPH_NAMESPACE_END