Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
ConvexHullShape.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
10#ifdef JPH_DEBUG_RENDERER
12#endif // JPH_DEBUG_RENDERER
13
15
18{
19public:
21
22
24
27 ConvexHullShapeSettings(const Vec3 *inPoints, int inNumPoints, float inMaxConvexRadius = cDefaultConvexRadius, const PhysicsMaterial *inMaterial = nullptr) : ConvexShapeSettings(inMaterial), mPoints(inPoints, inPoints + inNumPoints), mMaxConvexRadius(inMaxConvexRadius) { }
28 ConvexHullShapeSettings(const Array<Vec3> &inPoints, float inConvexRadius = cDefaultConvexRadius, const PhysicsMaterial *inMaterial = nullptr) : ConvexShapeSettings(inMaterial), mPoints(inPoints), mMaxConvexRadius(inConvexRadius) { }
29
30 // See: ShapeSettings
31 virtual ShapeResult Create() const override;
32
34 float mMaxConvexRadius = 0.0f;
35 float mMaxErrorConvexRadius = 0.05f;
36 float mHullTolerance = 1.0e-3f;
37};
38
40class ConvexHullShape final : public ConvexShape
41{
42public:
44
47 static constexpr int cMaxPointsInHull = 256;
48
51 ConvexHullShape(const ConvexHullShapeSettings &inSettings, ShapeResult &outResult);
52
53 // See Shape::GetCenterOfMass
54 virtual Vec3 GetCenterOfMass() const override { return mCenterOfMass; }
55
56 // See Shape::GetLocalBounds
57 virtual AABox GetLocalBounds() const override { return mLocalBounds; }
58
59 // See Shape::GetInnerRadius
60 virtual float GetInnerRadius() const override { return mInnerRadius; }
61
62 // See Shape::GetMassProperties
63 virtual MassProperties GetMassProperties() const override;
64
65 // See Shape::GetSurfaceNormal
66 virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override;
67
68 // See Shape::GetSupportingFace
69 virtual void GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const override;
70
71 // See ConvexShape::GetSupportFunction
72 virtual const Support * GetSupportFunction(ESupportMode inMode, SupportBuffer &inBuffer, Vec3Arg inScale) const override;
73
74 // See Shape::GetSubmergedVolume
75 virtual void GetSubmergedVolume(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy JPH_IF_DEBUG_RENDERER(, RVec3Arg inBaseOffset)) const override;
76
77#ifdef JPH_DEBUG_RENDERER
78 // See Shape::Draw
79 virtual void Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override;
80
82 void DrawShrunkShape(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale) const;
83#endif // JPH_DEBUG_RENDERER
84
85 // See Shape::CastRay
86 virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;
87 virtual void CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
88
89 // See: Shape::CollidePoint
90 virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
91
92 // See Shape::GetTrianglesStart
93 virtual void GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const override;
94
95 // See Shape::GetTrianglesNext
96 virtual int GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials = nullptr) const override;
97
98 // See Shape
99 virtual void SaveBinaryState(StreamOut &inStream) const override;
100
101 // See Shape::GetStats
102 virtual Stats GetStats() const override;
103
104 // See Shape::GetVolume
105 virtual float GetVolume() const override { return mVolume; }
106
108 float GetConvexRadius() const { return mConvexRadius; }
109
111 const Array<Plane> & GetPlanes() const { return mPlanes; }
112
114 inline uint GetNumPoints() const { return (uint)mPoints.size(); }
115
117 inline Vec3 GetPoint(uint inIndex) const { return mPoints[inIndex].mPosition; }
118
120 inline uint GetNumFaces() const { return (uint)mFaces.size(); }
121
123 inline uint GetNumVerticesInFace(uint inFaceIndex) const { return mFaces[inFaceIndex].mNumVertices; }
124
130 inline uint GetFaceVertices(uint inFaceIndex, uint inMaxVertices, uint *outVertices) const
131 {
132 const Face &face = mFaces[inFaceIndex];
133 const uint8 *first_vertex = mVertexIdx.data() + face.mFirstVertex;
134 uint num_vertices = min<uint>(face.mNumVertices, inMaxVertices);
135 for (uint i = 0; i < num_vertices; ++i)
136 outVertices[i] = first_vertex[i];
137 return face.mNumVertices;
138 }
139
140 // Register shape functions with the registry
141 static void sRegister();
142
143#ifdef JPH_DEBUG_RENDERER
145 inline static bool sDrawFaceOutlines = false;
146#endif // JPH_DEBUG_RENDERER
147
148protected:
149 // See: Shape::RestoreBinaryState
150 virtual void RestoreBinaryState(StreamIn &inStream) override;
151
152private:
154 bool CastRayHelper(const RayCast &inRay, float &outMinFraction, float &outMaxFraction) const;
155
158
160 class HullNoConvex;
161 class HullWithConvex;
163
164 struct Face
165 {
166 uint16 mFirstVertex;
167 uint16 mNumVertices = 0;
168 };
169
170 static_assert(sizeof(Face) == 4, "Unexpected size");
171 static_assert(alignof(Face) == 2, "Unexpected alignment");
172
173 struct Point
174 {
175 Vec3 mPosition;
176 int mNumFaces = 0;
177 int mFaces[3] = { -1, -1, -1 };
178 };
179
180 static_assert(sizeof(Point) == 32, "Unexpected size");
181 static_assert(alignof(Point) == JPH_VECTOR_ALIGNMENT, "Unexpected alignment");
182
183 Vec3 mCenterOfMass;
184 Mat44 mInertia;
185 AABox mLocalBounds;
186 Array<Point> mPoints;
187 Array<Face> mFaces;
188 Array<Plane> mPlanes;
189 Array<uint8> mVertexIdx;
190 float mConvexRadius = 0.0f;
191 float mVolume;
192 float mInnerRadius = FLT_MAX;
193
194#ifdef JPH_DEBUG_RENDERER
195 mutable DebugRenderer::GeometryRef mGeometry;
196#endif // JPH_DEBUG_RENDERER
197};
198
unsigned int uint
Definition: Core.h:309
#define JPH_NAMESPACE_END
Definition: Core.h:240
uint8_t uint8
Definition: Core.h:310
uint16_t uint16
Definition: Core.h:311
#define JPH_IF_DEBUG_RENDERER(...)
Definition: Core.h:378
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:234
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition: Memory.h:29
constexpr float cDefaultConvexRadius
How much padding to add around objects.
Definition: PhysicsSettings.h:16
std::vector< T, STLAllocator< T > > Array
Definition: STLAllocator.h:81
#define JPH_DECLARE_SERIALIZABLE_VIRTUAL(class_name)
Definition: SerializableObject.h:100
EShapeSubType
This enumerates all shape types, each shape can return its type through Shape::GetSubType.
Definition: Shape.h:71
Axis aligned box.
Definition: AABox.h:16
Virtual interface that allows collecting multiple collision results.
Definition: CollisionCollector.h:45
Class that holds an RGBA color with 8-bits per component.
Definition: Color.h:16
Definition: ConvexHullShape.cpp:1060
Definition: ConvexHullShape.cpp:397
Definition: ConvexHullShape.cpp:449
Definition: ConvexHullShape.cpp:488
A convex hull.
Definition: ConvexHullShape.h:41
virtual Vec3 GetCenterOfMass() const override
All shapes are centered around their center of mass. This function returns the center of mass positio...
Definition: ConvexHullShape.h:54
virtual float GetVolume() const override
Definition: ConvexHullShape.h:105
virtual void RestoreBinaryState(StreamIn &inStream) override
This function should not be called directly, it is used by sRestoreFromBinaryState.
Definition: ConvexHullShape.cpp:1162
static bool sDrawFaceOutlines
Draw the outlines of the faces of the convex hull when drawing the shape.
Definition: ConvexHullShape.h:145
uint GetFaceVertices(uint inFaceIndex, uint inMaxVertices, uint *outVertices) const
Definition: ConvexHullShape.h:130
static JPH_OVERRIDE_NEW_DELETE constexpr int cMaxPointsInHull
Definition: ConvexHullShape.h:47
virtual void SaveBinaryState(StreamOut &inStream) const override
Saves the contents of the shape in binary form to inStream.
Definition: ConvexHullShape.cpp:1145
virtual float GetInnerRadius() const override
Definition: ConvexHullShape.h:60
virtual AABox GetLocalBounds() const override
Get local bounding box including convex radius, this box is centered around the center of mass rather...
Definition: ConvexHullShape.h:57
virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override
Definition: ConvexHullShape.cpp:996
virtual void Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override
Draw the shape at a particular location with a particular color (debugging purposes)
Definition: ConvexHullShape.cpp:806
static void sRegister()
Definition: ConvexHullShape.cpp:1195
uint GetNumVerticesInFace(uint inFaceIndex) const
Get the number of vertices in a face.
Definition: ConvexHullShape.h:123
virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override
Definition: ConvexHullShape.cpp:372
Vec3 GetPoint(uint inIndex) const
Get a vertex of this convex hull relative to the center of mass.
Definition: ConvexHullShape.h:117
virtual void GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const override
Definition: ConvexHullShape.cpp:1069
virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter={ }) const override
Definition: ConvexHullShape.cpp:1044
virtual int GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials=nullptr) const override
Definition: ConvexHullShape.cpp:1077
const Array< Plane > & GetPlanes() const
Get the planes of this convex hull.
Definition: ConvexHullShape.h:111
uint GetNumFaces() const
Get the number of faces in this convex hull.
Definition: ConvexHullShape.h:120
virtual void GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const override
Definition: ConvexHullShape.cpp:673
virtual void GetSubmergedVolume(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy JPH_IF_DEBUG_RENDERER(, RVec3Arg inBaseOffset)) const override
Definition: ConvexHullShape.cpp:724
ConvexHullShape()
Constructor.
Definition: ConvexHullShape.h:50
virtual Stats GetStats() const override
Get stats of this shape. Use for logging / data collection purposes only. Does not add values from ch...
Definition: ConvexHullShape.cpp:1179
void DrawShrunkShape(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale) const
Debugging helper draw function that draws how all points are moved when a shape is shrunk by the conv...
Definition: ConvexHullShape.cpp:855
uint GetNumPoints() const
Get the number of vertices in this convex hull.
Definition: ConvexHullShape.h:114
float GetConvexRadius() const
Get the convex radius of this convex hull.
Definition: ConvexHullShape.h:108
virtual const Support * GetSupportFunction(ESupportMode inMode, SupportBuffer &inBuffer, Vec3Arg inScale) const override
Definition: ConvexHullShape.cpp:531
virtual MassProperties GetMassProperties() const override
Calculate the mass and inertia of this shape.
Definition: ConvexHullShape.cpp:356
Class that constructs a ConvexHullShape.
Definition: ConvexHullShape.h:18
ConvexHullShapeSettings(const Array< Vec3 > &inPoints, float inConvexRadius=cDefaultConvexRadius, const PhysicsMaterial *inMaterial=nullptr)
Definition: ConvexHullShape.h:28
Array< Vec3 > mPoints
Points to create the hull from.
Definition: ConvexHullShape.h:33
float mMaxConvexRadius
Convex radius as supplied by the constructor. Note that during hull creation the convex radius can be...
Definition: ConvexHullShape.h:34
float mHullTolerance
Points are allowed this far outside of the hull (increasing this yields a hull with less vertices)....
Definition: ConvexHullShape.h:36
virtual ShapeResult Create() const override
Create a shape according to the settings specified by this object.
Definition: ConvexHullShape.cpp:34
float mMaxErrorConvexRadius
Maximum distance between the shrunk hull + convex radius and the actual hull.
Definition: ConvexHullShape.h:35
Base class for all convex shapes. Defines a virtual interface.
Definition: ConvexShape.h:36
ESupportMode
How the GetSupport function should behave.
Definition: ConvexShape.h:92
Class that constructs a ConvexShape (abstract)
Definition: ConvexShape.h:18
Simple triangle renderer for debugging purposes.
Definition: DebugRenderer.h:25
Class that holds 3 floats. Used as a storage class. Convert to Vec3 for calculations.
Definition: Float3.h:13
Describes the mass and inertia properties of a body. Used during body construction only.
Definition: MassProperties.h:16
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition: Mat44.h:13
Definition: PhysicsMaterial.h:23
An infinite plane described by the formula X . Normal + Constant = 0.
Definition: Plane.h:11
Definition: Quat.h:33
Specialization of cast result against a shape.
Definition: CastResult.h:27
Settings to be passed with a ray cast.
Definition: RayCast.h:64
Filter class.
Definition: ShapeFilter.h:17
ShapeSettings::ShapeResult ShapeResult
Definition: Shape.h:174
StaticArray< Vec3, 32 > SupportingFace
Type definition for a supporting face.
Definition: Shape.h:235
Result< Ref< Shape > > ShapeResult
Definition: Shape.h:139
Simple binary input stream.
Definition: StreamIn.h:11
Simple binary output stream.
Definition: StreamOut.h:11
Definition: SubShapeID.h:108
A sub shape id contains a path to an element (usually a triangle or other primitive type) of a compou...
Definition: SubShapeID.h:23
Definition: Vec3.h:16
Definition: RayCast.h:41