ConvexShape.h 7.9 KB

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