ConvexShape.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 ConvexShapeSettings : public ShapeSettings
  13. {
  14. public:
  15. JPH_DECLARE_SERIALIZABLE_ABSTRACT(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 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
  72. IncludeConvexRadius, ///< Return the shape including the convex radius
  73. };
  74. /// Returns an object that provides the GetSupport function for this shape.
  75. /// inMode determines if this support function includes or excludes the convex radius.
  76. /// of the values returned by the GetSupport function. This improves numerical accuracy of the results.
  77. /// inScale scales this shape in local space.
  78. virtual const Support * GetSupportFunction(ESupportMode inMode, SupportBuffer &inBuffer, Vec3Arg inScale) const = 0;
  79. /// Material of the shape
  80. void SetMaterial(const PhysicsMaterial *inMaterial) { mMaterial = inMaterial; }
  81. const PhysicsMaterial * GetMaterial() const { return mMaterial != nullptr? mMaterial : PhysicsMaterial::sDefault; }
  82. /// Set density of the shape (kg / m^3)
  83. void SetDensity(float inDensity) { mDensity = inDensity; }
  84. /// Get density of the shape (kg / m^3)
  85. float GetDensity() const { return mDensity; }
  86. #ifdef JPH_DEBUG_RENDERER
  87. // See Shape::DrawGetSupportFunction
  88. virtual void DrawGetSupportFunction(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inDrawSupportDirection) const override;
  89. // See Shape::DrawGetSupportingFace
  90. virtual void DrawGetSupportingFace(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale) const override;
  91. #endif // JPH_DEBUG_RENDERER
  92. // See Shape
  93. virtual void SaveBinaryState(StreamOut &inStream) const override;
  94. virtual void SaveMaterialState(PhysicsMaterialList &outMaterials) const override;
  95. virtual void RestoreMaterialState(const PhysicsMaterialRefC *inMaterials, uint inNumMaterials) override;
  96. // Register shape functions with the registry
  97. static void sRegister();
  98. protected:
  99. // See: Shape::RestoreBinaryState
  100. virtual void RestoreBinaryState(StreamIn &inStream) override;
  101. /// Vertex list that forms a unit sphere
  102. static const std::vector<Vec3> sUnitSphereTriangles;
  103. private:
  104. // Class for GetTrianglesStart/Next
  105. class CSGetTrianglesContext;
  106. // Helper functions called by CollisionDispatch
  107. 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);
  108. 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);
  109. // Properties
  110. RefConst<PhysicsMaterial> mMaterial; ///< Material assigned to this shape
  111. float mDensity = 1000.0f; ///< Uniform density of the interior of the convex object (kg / m^3)
  112. };
  113. JPH_NAMESPACE_END