ConvexShape.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/Core/StaticArray.h>
  6. #include <Jolt/Physics/Collision/Shape/Shape.h>
  7. #include <Jolt/Physics/Collision/Shape/SubShapeID.h>
  8. #include <Jolt/Physics/Collision/PhysicsMaterial.h>
  9. JPH_NAMESPACE_BEGIN
  10. class CollideShapeSettings;
  11. /// Class that constructs a ConvexShape (abstract)
  12. class JPH_EXPORT ConvexShapeSettings : public ShapeSettings
  13. {
  14. public:
  15. JPH_DECLARE_SERIALIZABLE_ABSTRACT(JPH_EXPORT, ConvexShapeSettings)
  16. /// Constructor
  17. ConvexShapeSettings() = default;
  18. explicit ConvexShapeSettings(const PhysicsMaterial *inMaterial) : mMaterial(inMaterial) { }
  19. /// Set the density of the object in kg / m^3
  20. void SetDensity(float inDensity) { mDensity = inDensity; }
  21. // Properties
  22. RefConst<PhysicsMaterial> mMaterial; ///< Material assigned to this shape
  23. float mDensity = 1000.0f; ///< Uniform density of the interior of the convex object (kg / m^3)
  24. };
  25. /// Base class for all convex shapes. Defines a virtual interface.
  26. class JPH_EXPORT ConvexShape : public Shape
  27. {
  28. public:
  29. JPH_OVERRIDE_NEW_DELETE
  30. /// Constructor
  31. explicit ConvexShape(EShapeSubType inSubType) : Shape(EShapeType::Convex, inSubType) { }
  32. ConvexShape(EShapeSubType inSubType, const ConvexShapeSettings &inSettings, ShapeResult &outResult) : Shape(EShapeType::Convex, inSubType, inSettings, outResult), mMaterial(inSettings.mMaterial), mDensity(inSettings.mDensity) { }
  33. ConvexShape(EShapeSubType inSubType, const PhysicsMaterial *inMaterial) : Shape(EShapeType::Convex, inSubType), mMaterial(inMaterial) { }
  34. // See Shape::GetSubShapeIDBitsRecursive
  35. virtual uint GetSubShapeIDBitsRecursive() const override { return 0; } // Convex shapes don't have sub shapes
  36. // See Shape::GetMaterial
  37. virtual const PhysicsMaterial * GetMaterial(const SubShapeID &inSubShapeID) const override { JPH_ASSERT(inSubShapeID.IsEmpty(), "Invalid subshape ID"); return GetMaterial(); }
  38. // See Shape::CastRay
  39. virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;
  40. virtual void CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
  41. // See: Shape::CollidePoint
  42. virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
  43. // See Shape::GetTrianglesStart
  44. virtual void GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const override;
  45. // See Shape::GetTrianglesNext
  46. virtual int GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials = nullptr) const override;
  47. // See Shape::GetSubmergedVolume
  48. virtual void GetSubmergedVolume(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy JPH_IF_DEBUG_RENDERER(, RVec3Arg inBaseOffset)) const override;
  49. /// Function that provides an interface for GJK
  50. class Support
  51. {
  52. public:
  53. /// Warning: Virtual destructor will not be called on this object!
  54. virtual ~Support() = default;
  55. /// Calculate the support vector for this convex shape (includes / excludes the convex radius depending on how this was obtained).
  56. /// Support vector is relative to the center of mass of the shape.
  57. virtual Vec3 GetSupport(Vec3Arg inDirection) const = 0;
  58. /// Convex radius of shape. Collision detection on penetrating shapes is much more expensive,
  59. /// so you can add a radius around objects to increase the shape. This makes it far less likely that they will actually penetrate.
  60. virtual float GetConvexRadius() const = 0;
  61. };
  62. /// Buffer to hold a Support object, used to avoid dynamic memory allocations
  63. class alignas(16) SupportBuffer
  64. {
  65. public:
  66. uint8 mData[4160];
  67. };
  68. /// How the GetSupport function should behave
  69. enum class ESupportMode
  70. {
  71. ExcludeConvexRadius, ///< Return the shape excluding the convex radius, Support::GetConvexRadius will return the convex radius if there is one, but adding this radius may not result in the most accurate/efficient representation of shapes with sharp edges
  72. IncludeConvexRadius, ///< Return the shape including the convex radius, Support::GetSupport includes the convex radius if there is one, Support::GetConvexRadius will return 0
  73. Default, ///< Use both Support::GetSupport add Support::GetConvexRadius to get a support point that matches the original shape as accurately/efficiently as possible
  74. };
  75. /// Returns an object that provides the GetSupport function for this shape.
  76. /// inMode determines if this support function includes or excludes the convex radius.
  77. /// of the values returned by the GetSupport function. This improves numerical accuracy of the results.
  78. /// inScale scales this shape in local space.
  79. virtual const Support * GetSupportFunction(ESupportMode inMode, SupportBuffer &inBuffer, Vec3Arg inScale) const = 0;
  80. /// Material of the shape
  81. void SetMaterial(const PhysicsMaterial *inMaterial) { mMaterial = inMaterial; }
  82. const PhysicsMaterial * GetMaterial() const { return mMaterial != nullptr? mMaterial : PhysicsMaterial::sDefault; }
  83. /// Set density of the shape (kg / m^3)
  84. void SetDensity(float inDensity) { mDensity = inDensity; }
  85. /// Get density of the shape (kg / m^3)
  86. float GetDensity() const { return mDensity; }
  87. #ifdef JPH_DEBUG_RENDERER
  88. // See Shape::DrawGetSupportFunction
  89. virtual void DrawGetSupportFunction(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inDrawSupportDirection) const override;
  90. // See Shape::DrawGetSupportingFace
  91. virtual void DrawGetSupportingFace(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale) const override;
  92. #endif // JPH_DEBUG_RENDERER
  93. // See Shape
  94. virtual void SaveBinaryState(StreamOut &inStream) const override;
  95. virtual void SaveMaterialState(PhysicsMaterialList &outMaterials) const override;
  96. virtual void RestoreMaterialState(const PhysicsMaterialRefC *inMaterials, uint inNumMaterials) override;
  97. // Register shape functions with the registry
  98. static void sRegister();
  99. protected:
  100. // See: Shape::RestoreBinaryState
  101. virtual void RestoreBinaryState(StreamIn &inStream) override;
  102. /// Vertex list that forms a unit sphere
  103. static const std::vector<Vec3> sUnitSphereTriangles;
  104. private:
  105. // Class for GetTrianglesStart/Next
  106. class CSGetTrianglesContext;
  107. // Helper functions called by CollisionDispatch
  108. static void sCollideConvexVsConvex(const Shape *inShape1, const Shape *inShape2, Vec3Arg inScale1, Vec3Arg inScale2, Mat44Arg inCenterOfMassTransform1, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, const CollideShapeSettings &inCollideShapeSettings, CollideShapeCollector &ioCollector, const ShapeFilter &inShapeFilter);
  109. static void sCastConvexVsConvex(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector);
  110. // Properties
  111. RefConst<PhysicsMaterial> mMaterial; ///< Material assigned to this shape
  112. float mDensity = 1000.0f; ///< Uniform density of the interior of the convex object (kg / m^3)
  113. };
  114. JPH_NAMESPACE_END