Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Collision/Shape/CylinderShape.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/Physics/Collision/Shape/ConvexShape.h>
8
#include <Jolt/Physics/PhysicsSettings.h>
9
10
JPH_NAMESPACE_BEGIN
11
12
/// Class that constructs a CylinderShape
13
class JPH_EXPORT CylinderShapeSettings final : public ConvexShapeSettings
14
{
15
JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, CylinderShapeSettings)
16
17
public:
18
/// Default constructor for deserialization
19
CylinderShapeSettings() = default;
20
21
/// Create a shape centered around the origin with one top at (0, -inHalfHeight, 0) and the other at (0, inHalfHeight, 0) and radius inRadius.
22
/// (internally the convex radius will be subtracted from the cylinder the total cylinder will not grow with the convex radius, but the edges of the cylinder will be rounded a bit).
23
CylinderShapeSettings(float inHalfHeight, float inRadius, float inConvexRadius = cDefaultConvexRadius, const PhysicsMaterial *inMaterial = nullptr) : ConvexShapeSettings(inMaterial), mHalfHeight(inHalfHeight), mRadius(inRadius), mConvexRadius(inConvexRadius) { }
24
25
// See: ShapeSettings
26
virtual ShapeResult Create() const override;
27
28
float mHalfHeight = 0.0f;
29
float mRadius = 0.0f;
30
float mConvexRadius = 0.0f;
31
};
32
33
/// A cylinder
34
class JPH_EXPORT CylinderShape final : public ConvexShape
35
{
36
public:
37
JPH_OVERRIDE_NEW_DELETE
38
39
/// Constructor
40
CylinderShape() : ConvexShape(EShapeSubType::Cylinder) { }
41
CylinderShape(const CylinderShapeSettings &inSettings, ShapeResult &outResult);
42
43
/// Create a shape centered around the origin with one top at (0, -inHalfHeight, 0) and the other at (0, inHalfHeight, 0) and radius inRadius.
44
/// (internally the convex radius will be subtracted from the cylinder the total cylinder will not grow with the convex radius, but the edges of the cylinder will be rounded a bit).
45
CylinderShape(float inHalfHeight, float inRadius, float inConvexRadius = cDefaultConvexRadius, const PhysicsMaterial *inMaterial = nullptr);
46
47
/// Get half height of cylinder
48
float GetHalfHeight() const { return mHalfHeight; }
49
50
/// Get radius of cylinder
51
float GetRadius() const { return mRadius; }
52
53
// See Shape::GetLocalBounds
54
virtual AABox GetLocalBounds() const override;
55
56
// See Shape::GetInnerRadius
57
virtual float GetInnerRadius() const override { return min(mHalfHeight, mRadius); }
58
59
// See Shape::GetMassProperties
60
virtual MassProperties GetMassProperties() const override;
61
62
// See Shape::GetSurfaceNormal
63
virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override;
64
65
// See Shape::GetSupportingFace
66
virtual void GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const override;
67
68
// See ConvexShape::GetSupportFunction
69
virtual const Support * GetSupportFunction(ESupportMode inMode, SupportBuffer &inBuffer, Vec3Arg inScale) const override;
70
71
#ifdef JPH_DEBUG_RENDERER
72
// See Shape::Draw
73
virtual void Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override;
74
#endif // JPH_DEBUG_RENDERER
75
76
// See Shape::CastRay
77
using ConvexShape::CastRay;
78
virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;
79
80
// See: Shape::CollidePoint
81
virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
82
83
// See: Shape::CollideSoftBodyVertices
84
virtual void CollideSoftBodyVertices(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const CollideSoftBodyVertexIterator &inVertices, uint inNumVertices, int inCollidingShapeIndex) const override;
85
86
// See Shape::GetTrianglesStart
87
virtual void GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const override;
88
89
// See Shape::GetTrianglesNext
90
virtual int GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials = nullptr) const override;
91
92
// See Shape
93
virtual void SaveBinaryState(StreamOut &inStream) const override;
94
95
// See Shape::GetStats
96
virtual Stats GetStats() const override { return Stats(sizeof(*this), 0); }
97
98
// See Shape::GetVolume
99
virtual float GetVolume() const override { return 2.0f * JPH_PI * mHalfHeight * Square(mRadius); }
100
101
/// Get the convex radius of this cylinder
102
float GetConvexRadius() const { return mConvexRadius; }
103
104
// See Shape::IsValidScale
105
virtual bool IsValidScale(Vec3Arg inScale) const override;
106
107
// See Shape::MakeScaleValid
108
virtual Vec3 MakeScaleValid(Vec3Arg inScale) const override;
109
110
// Register shape functions with the registry
111
static void sRegister();
112
113
protected:
114
// See: Shape::RestoreBinaryState
115
virtual void RestoreBinaryState(StreamIn &inStream) override;
116
117
private:
118
// Class for GetSupportFunction
119
class Cylinder;
120
121
float mHalfHeight = 0.0f;
122
float mRadius = 0.0f;
123
float mConvexRadius = 0.0f;
124
};
125
126
JPH_NAMESPACE_END
127
128