PlaneShape.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/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 PlaneShape
  11. class JPH_EXPORT PlaneShapeSettings final : public ShapeSettings
  12. {
  13. JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, PlaneShapeSettings)
  14. public:
  15. /// Default constructor for deserialization
  16. PlaneShapeSettings() = default;
  17. /// Create a plane shape.
  18. explicit PlaneShapeSettings(const Plane &inPlane, const PhysicsMaterial *inMaterial = nullptr, float inHalfExtent = cDefaultHalfExtent) : mPlane(inPlane), mMaterial(inMaterial), mHalfExtent(inHalfExtent) { }
  19. // See: ShapeSettings
  20. virtual ShapeResult Create() const override;
  21. Plane mPlane; ///< Plane that describes the shape. The negative half space is considered solid.
  22. RefConst<PhysicsMaterial> mMaterial; ///< Surface material of the plane
  23. static constexpr float cDefaultHalfExtent = 1000.0f; ///< Default half-extent of the plane (total size along 1 axis will be 2 * half-extent)
  24. float mHalfExtent = cDefaultHalfExtent; ///< The bounding box of this plane will run from [-half_extent, half_extent]. Keep this as low as possible for better broad phase performance.
  25. };
  26. /// A plane shape. The negative half space is considered solid. Planes cannot be dynamic objects, only static or kinematic.
  27. /// The plane is considered an infinite shape, but testing collision outside of its bounding box (defined by the half-extent parameter) will not return a collision result.
  28. /// At the edge of the bounding box collision with the plane will be inconsistent. If you need something of a well defined size, a box shape may be better.
  29. class JPH_EXPORT PlaneShape final : public Shape
  30. {
  31. public:
  32. JPH_OVERRIDE_NEW_DELETE
  33. /// Constructor
  34. PlaneShape() : Shape(EShapeType::Plane, EShapeSubType::Plane) { }
  35. explicit PlaneShape(const Plane &inPlane, const PhysicsMaterial *inMaterial = nullptr, float inHalfExtent = PlaneShapeSettings::cDefaultHalfExtent) : Shape(EShapeType::Plane, EShapeSubType::Plane), mPlane(inPlane), mMaterial(inMaterial), mHalfExtent(inHalfExtent) { CalculateLocalBounds(); }
  36. PlaneShape(const PlaneShapeSettings &inSettings, ShapeResult &outResult);
  37. /// Get the plane
  38. const Plane & GetPlane() const { return mPlane; }
  39. /// Get the half-extent of the bounding box of the plane
  40. float GetHalfExtent() const { return mHalfExtent; }
  41. // See Shape::MustBeStatic
  42. virtual bool MustBeStatic() const override { return true; }
  43. // See Shape::GetLocalBounds
  44. virtual AABox GetLocalBounds() const override { return mLocalBounds; }
  45. // See Shape::GetSubShapeIDBitsRecursive
  46. virtual uint GetSubShapeIDBitsRecursive() const override { return 0; }
  47. // See Shape::GetInnerRadius
  48. virtual float GetInnerRadius() const override { return 0.0f; }
  49. // See Shape::GetMassProperties
  50. virtual MassProperties GetMassProperties() const override;
  51. // See Shape::GetMaterial
  52. virtual const PhysicsMaterial * GetMaterial(const SubShapeID &inSubShapeID) const override { JPH_ASSERT(inSubShapeID.IsEmpty(), "Invalid subshape ID"); return GetMaterial(); }
  53. // See Shape::GetSurfaceNormal
  54. virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override { JPH_ASSERT(inSubShapeID.IsEmpty(), "Invalid subshape ID"); return mPlane.GetNormal(); }
  55. // See Shape::GetSupportingFace
  56. virtual void GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const override;
  57. #ifdef JPH_DEBUG_RENDERER
  58. // See Shape::Draw
  59. virtual void Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override;
  60. #endif // JPH_DEBUG_RENDERER
  61. // See Shape::CastRay
  62. virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;
  63. virtual void CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
  64. // See: Shape::CollidePoint
  65. virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
  66. // See: Shape::CollideSoftBodyVertices
  67. virtual void CollideSoftBodyVertices(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const CollideSoftBodyVertexIterator &inVertices, uint inNumVertices, int inCollidingShapeIndex) const override;
  68. // See Shape::GetTrianglesStart
  69. virtual void GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const override;
  70. // See Shape::GetTrianglesNext
  71. virtual int GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials = nullptr) const override;
  72. // See Shape::GetSubmergedVolume
  73. virtual void GetSubmergedVolume(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy JPH_IF_DEBUG_RENDERER(, RVec3Arg inBaseOffset)) const override { JPH_ASSERT(false, "Not supported"); }
  74. // See Shape
  75. virtual void SaveBinaryState(StreamOut &inStream) const override;
  76. virtual void SaveMaterialState(PhysicsMaterialList &outMaterials) const override;
  77. virtual void RestoreMaterialState(const PhysicsMaterialRefC *inMaterials, uint inNumMaterials) override;
  78. // See Shape::GetStats
  79. virtual Stats GetStats() const override { return Stats(sizeof(*this), 0); }
  80. // See Shape::GetVolume
  81. virtual float GetVolume() const override { return 0; }
  82. /// Material of the shape
  83. void SetMaterial(const PhysicsMaterial *inMaterial) { mMaterial = inMaterial; }
  84. const PhysicsMaterial * GetMaterial() const { return mMaterial != nullptr? mMaterial : PhysicsMaterial::sDefault; }
  85. // Register shape functions with the registry
  86. static void sRegister();
  87. protected:
  88. // See: Shape::RestoreBinaryState
  89. virtual void RestoreBinaryState(StreamIn &inStream) override;
  90. private:
  91. struct PSGetTrianglesContext; ///< Context class for GetTrianglesStart/Next
  92. // Get 4 vertices that form the plane
  93. void GetVertices(Vec3 *outVertices) const;
  94. // Cache the local bounds
  95. void CalculateLocalBounds();
  96. // Helper functions called by CollisionDispatch
  97. static void sCollideConvexVsPlane(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);
  98. static void sCastConvexVsPlane(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector);
  99. Plane mPlane;
  100. RefConst<PhysicsMaterial> mMaterial;
  101. float mHalfExtent;
  102. AABox mLocalBounds;
  103. };
  104. JPH_NAMESPACE_END