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