ConvexShape.h 7.3 KB

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