Debian Codegraph @

thirdparty/jolt_physics/Jolt/Physics/Collision/Shape/PlaneShape.h

raw symbols SCIP
1 // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2 // SPDX-FileCopyrightText: 2024 Jorrit Rouwe
3 // SPDX-License-Identifier: MIT
4
5 #pragma once
6
7 #include <Jolt/Physics/Collision/Shape/Shape.h>
8 #include <Jolt/Physics/Collision/Shape/SubShapeID.h>
9 #include <Jolt/Physics/Collision/PhysicsMaterial.h>
10
11 JPH_NAMESPACE_BEGIN
12
13 class CollideShapeSettings;
14
15 /// Class that constructs a PlaneShape
16 class JPH_EXPORT PlaneShapeSettings final : public ShapeSettings
17 {
18 JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, PlaneShapeSettings)
19
20 public:
21 /// Default constructor for deserialization
22 PlaneShapeSettings() = default;
23
24 /// Create a plane shape.
25 PlaneShapeSettings(const Plane &inPlane, const PhysicsMaterial *inMaterial = nullptr, float inHalfExtent = cDefaultHalfExtent) : mPlane(inPlane), mMaterial(inMaterial), mHalfExtent(inHalfExtent) { }
26
27 // See: ShapeSettings
28 virtual ShapeResult Create() const override;
29
30 Plane mPlane; ///< Plane that describes the shape. The negative half space is considered solid.
31
32 RefConst<PhysicsMaterial> mMaterial; ///< Surface material of the plane
33
34 static constexpr float cDefaultHalfExtent = 1000.0f; ///< Default half-extent of the plane (total size along 1 axis will be 2 * half-extent)
35
36 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.
37 };
38
39 /// A plane shape. The negative half space is considered solid. Planes cannot be dynamic objects, only static or kinematic.
40 /// 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.
41 /// 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.
42 class JPH_EXPORT PlaneShape final : public Shape
43 {
44 public:
45 JPH_OVERRIDE_NEW_DELETE
46
47 /// Constructor
48 PlaneShape() : Shape(EShapeType::Plane, EShapeSubType::Plane) { }
49 PlaneShape(const Plane &inPlane, const PhysicsMaterial *inMaterial = nullptr, float inHalfExtent = PlaneShapeSettings::cDefaultHalfExtent) : Shape(EShapeType::Plane, EShapeSubType::Plane), mPlane(inPlane), mMaterial(inMaterial), mHalfExtent(inHalfExtent) { CalculateLocalBounds(); }
50 PlaneShape(const PlaneShapeSettings &inSettings, ShapeResult &outResult);
51
52 /// Get the plane
53 const Plane & GetPlane() const { return mPlane; }
54
55 /// Get the half-extent of the bounding box of the plane
56 float GetHalfExtent() const { return mHalfExtent; }
57
58 // See Shape::MustBeStatic
59 virtual bool MustBeStatic() const override { return true; }
60
61 // See Shape::GetLocalBounds
62 virtual AABox GetLocalBounds() const override { return mLocalBounds; }
63
64 // See Shape::GetSubShapeIDBitsRecursive
65 virtual uint GetSubShapeIDBitsRecursive() const override { return 0; }
66
67 // See Shape::GetInnerRadius
68 virtual float GetInnerRadius() const override { return 0.0f; }
69
70 // See Shape::GetMassProperties
71 virtual MassProperties GetMassProperties() const override;
72
73 // See Shape::GetMaterial
74 virtual const PhysicsMaterial * GetMaterial(const SubShapeID &inSubShapeID) const override { JPH_ASSERT(inSubShapeID.IsEmpty(), "Invalid subshape ID"); return GetMaterial(); }
75
76 // See Shape::GetSurfaceNormal
77 virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override { JPH_ASSERT(inSubShapeID.IsEmpty(), "Invalid subshape ID"); return mPlane.GetNormal(); }
78
79 // See Shape::GetSupportingFace
80 virtual void GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const override;
81
82 #ifdef JPH_DEBUG_RENDERER
83 // See Shape::Draw
84 virtual void Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override;
85 #endif // JPH_DEBUG_RENDERER
86
87 // See Shape::CastRay
88 virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;
89 virtual void CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
90
91 // See: Shape::CollidePoint
92 virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
93
94 // See: Shape::CollideSoftBodyVertices
95 virtual void CollideSoftBodyVertices(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const CollideSoftBodyVertexIterator &inVertices, uint inNumVertices, int inCollidingShapeIndex) const override;
96
97 // See Shape::GetTrianglesStart
98 virtual void GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const override;
99
100 // See Shape::GetTrianglesNext
101 virtual int GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials = nullptr) const override;
102
103 // See Shape::GetSubmergedVolume
104 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"); }
105
106 // See Shape
107 virtual void SaveBinaryState(StreamOut &inStream) const override;
108 virtual void SaveMaterialState(PhysicsMaterialList &outMaterials) const override;
109 virtual void RestoreMaterialState(const PhysicsMaterialRefC *inMaterials, uint inNumMaterials) override;
110
111 // See Shape::GetStats
112 virtual Stats GetStats() const override { return Stats(sizeof(*this), 0); }
113
114 // See Shape::GetVolume
115 virtual float GetVolume() const override { return 0; }
116
117 /// Material of the shape
118 void SetMaterial(const PhysicsMaterial *inMaterial) { mMaterial = inMaterial; }
119 const PhysicsMaterial * GetMaterial() const { return mMaterial != nullptr? mMaterial : PhysicsMaterial::sDefault; }
120
121 // Register shape functions with the registry
122 static void sRegister();
123
124 protected:
125 // See: Shape::RestoreBinaryState
126 virtual void RestoreBinaryState(StreamIn &inStream) override;
127
128 private:
129 struct PSGetTrianglesContext; ///< Context class for GetTrianglesStart/Next
130
131 // Get 4 vertices that form the plane
132 void GetVertices(Vec3 *outVertices) const;
133
134 // Cache the local bounds
135 void CalculateLocalBounds();
136
137 // Helper functions called by CollisionDispatch
138 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);
139 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);
140
141 Plane mPlane;
142 RefConst<PhysicsMaterial> mMaterial;
143 float mHalfExtent;
144 AABox mLocalBounds;
145 };
146
147 JPH_NAMESPACE_END