Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Collision/Shape/TaperedCapsuleShape.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
#ifdef JPH_DEBUG_RENDERER
9
#include <Jolt/Renderer/DebugRenderer.h>
10
#endif // JPH_DEBUG_RENDERER
11
12
JPH_NAMESPACE_BEGIN
13
14
/// Class that constructs a TaperedCapsuleShape
15
class JPH_EXPORT TaperedCapsuleShapeSettings final : public ConvexShapeSettings
16
{
17
JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, TaperedCapsuleShapeSettings)
18
19
public:
20
/// Default constructor for deserialization
21
TaperedCapsuleShapeSettings() = default;
22
23
/// Create a tapered capsule centered around the origin with one sphere cap at (0, -inHalfHeightOfTaperedCylinder, 0) with radius inBottomRadius and the other at (0, inHalfHeightOfTaperedCylinder, 0) with radius inTopRadius
24
TaperedCapsuleShapeSettings(float inHalfHeightOfTaperedCylinder, float inTopRadius, float inBottomRadius, const PhysicsMaterial *inMaterial = nullptr);
25
26
/// Check if the settings are valid
27
bool IsValid() const { return mTopRadius > 0.0f && mBottomRadius > 0.0f && mHalfHeightOfTaperedCylinder >= 0.0f; }
28
29
/// Checks if the settings of this tapered capsule make this shape a sphere
30
bool IsSphere() const;
31
32
// See: ShapeSettings
33
virtual ShapeResult Create() const override;
34
35
float mHalfHeightOfTaperedCylinder = 0.0f;
36
float mTopRadius = 0.0f;
37
float mBottomRadius = 0.0f;
38
};
39
40
/// A capsule with different top and bottom radii
41
class JPH_EXPORT TaperedCapsuleShape final : public ConvexShape
42
{
43
public:
44
JPH_OVERRIDE_NEW_DELETE
45
46
/// Constructor
47
TaperedCapsuleShape() : ConvexShape(EShapeSubType::TaperedCapsule) { }
48
TaperedCapsuleShape(const TaperedCapsuleShapeSettings &inSettings, ShapeResult &outResult);
49
50
/// Get top radius of the tapered capsule
51
inline float GetTopRadius() const { return mTopRadius; }
52
53
/// Get bottom radius of the tapered capsule
54
inline float GetBottomRadius() const { return mBottomRadius; }
55
56
/// Get half height between the top and bottom sphere center
57
inline float GetHalfHeight() const { return 0.5f * (mTopCenter - mBottomCenter); }
58
59
// See Shape::GetCenterOfMass
60
virtual Vec3 GetCenterOfMass() const override { return mCenterOfMass; }
61
62
// See Shape::GetLocalBounds
63
virtual AABox GetLocalBounds() const override;
64
65
// See Shape::GetWorldSpaceBounds
66
virtual AABox GetWorldSpaceBounds(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale) const override;
67
using Shape::GetWorldSpaceBounds;
68
69
// See Shape::GetInnerRadius
70
virtual float GetInnerRadius() const override { return min(mTopRadius, mBottomRadius); }
71
72
// See Shape::GetMassProperties
73
virtual MassProperties GetMassProperties() const override;
74
75
// See Shape::GetSurfaceNormal
76
virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override;
77
78
// See Shape::GetSupportingFace
79
virtual void GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const override;
80
81
// See ConvexShape::GetSupportFunction
82
virtual const Support * GetSupportFunction(ESupportMode inMode, SupportBuffer &inBuffer, Vec3Arg inScale) const override;
83
84
// See: Shape::CollideSoftBodyVertices
85
virtual void CollideSoftBodyVertices(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const CollideSoftBodyVertexIterator &inVertices, uint inNumVertices, int inCollidingShapeIndex) const override;
86
87
#ifdef JPH_DEBUG_RENDERER
88
// See Shape::Draw
89
virtual void Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override;
90
#endif // JPH_DEBUG_RENDERER
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 GetLocalBounds().GetVolume(); } // Volume is approximate!
100
101
// See Shape::IsValidScale
102
virtual bool IsValidScale(Vec3Arg inScale) const override;
103
104
// See Shape::MakeScaleValid
105
virtual Vec3 MakeScaleValid(Vec3Arg inScale) const override;
106
107
// Register shape functions with the registry
108
static void sRegister();
109
110
protected:
111
// See: Shape::RestoreBinaryState
112
virtual void RestoreBinaryState(StreamIn &inStream) override;
113
114
private:
115
// Class for GetSupportFunction
116
class TaperedCapsule;
117
118
/// Returns box that approximates the inertia
119
AABox GetInertiaApproximation() const;
120
121
Vec3 mCenterOfMass = Vec3::sZero();
122
float mTopRadius = 0.0f;
123
float mBottomRadius = 0.0f;
124
float mTopCenter = 0.0f;
125
float mBottomCenter = 0.0f;
126
float mConvexRadius = 0.0f;
127
float mSinAlpha = 0.0f;
128
float mTanAlpha = 0.0f;
129
130
#ifdef JPH_DEBUG_RENDERER
131
mutable DebugRenderer::GeometryRef mGeometry;
132
#endif // JPH_DEBUG_RENDERER
133
};
134
135
JPH_NAMESPACE_END
136
137