Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Character/CharacterBase.h
9912 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/Reference.h>
8
#include <Jolt/Core/NonCopyable.h>
9
#include <Jolt/Physics/Body/BodyID.h>
10
#include <Jolt/Physics/Collision/Shape/Shape.h>
11
#include <Jolt/Physics/Collision/Shape/SubShapeID.h>
12
#include <Jolt/Physics/Collision/PhysicsMaterial.h>
13
14
JPH_NAMESPACE_BEGIN
15
16
class PhysicsSystem;
17
class StateRecorder;
18
19
/// Base class for configuration of a character
20
class JPH_EXPORT CharacterBaseSettings : public RefTarget<CharacterBaseSettings>
21
{
22
public:
23
JPH_OVERRIDE_NEW_DELETE
24
25
/// Constructor
26
CharacterBaseSettings() = default;
27
CharacterBaseSettings(const CharacterBaseSettings &inSettings) = default;
28
CharacterBaseSettings & operator = (const CharacterBaseSettings &inSettings) = default;
29
30
/// Virtual destructor
31
virtual ~CharacterBaseSettings() = default;
32
33
/// Vector indicating the up direction of the character
34
Vec3 mUp = Vec3::sAxisY();
35
36
/// Plane, defined in local space relative to the character. Every contact behind this plane can support the
37
/// character, every contact in front of this plane is treated as only colliding with the player.
38
/// Default: Accept any contact.
39
Plane mSupportingVolume { Vec3::sAxisY(), -1.0e10f };
40
41
/// Maximum angle of slope that character can still walk on (radians).
42
float mMaxSlopeAngle = DegreesToRadians(50.0f);
43
44
/// Set to indicate that extra effort should be made to try to remove ghost contacts (collisions with internal edges of a mesh). This is more expensive but makes bodies move smoother over a mesh with convex edges.
45
bool mEnhancedInternalEdgeRemoval = false;
46
47
/// Initial shape that represents the character's volume.
48
/// Usually this is a capsule, make sure the shape is made so that the bottom of the shape is at (0, 0, 0).
49
RefConst<Shape> mShape;
50
};
51
52
/// Base class for character class
53
class JPH_EXPORT CharacterBase : public RefTarget<CharacterBase>, public NonCopyable
54
{
55
public:
56
JPH_OVERRIDE_NEW_DELETE
57
58
/// Constructor
59
CharacterBase(const CharacterBaseSettings *inSettings, PhysicsSystem *inSystem);
60
61
/// Destructor
62
virtual ~CharacterBase() = default;
63
64
/// Set the maximum angle of slope that character can still walk on (radians)
65
void SetMaxSlopeAngle(float inMaxSlopeAngle) { mCosMaxSlopeAngle = Cos(inMaxSlopeAngle); }
66
float GetCosMaxSlopeAngle() const { return mCosMaxSlopeAngle; }
67
68
/// Set the up vector for the character
69
void SetUp(Vec3Arg inUp) { mUp = inUp; }
70
Vec3 GetUp() const { return mUp; }
71
72
/// Check if the normal of the ground surface is too steep to walk on
73
bool IsSlopeTooSteep(Vec3Arg inNormal) const
74
{
75
// If cos max slope angle is close to one the system is turned off,
76
// otherwise check the angle between the up and normal vector
77
return mCosMaxSlopeAngle < cNoMaxSlopeAngle && inNormal.Dot(mUp) < mCosMaxSlopeAngle;
78
}
79
80
/// Get the current shape that the character is using.
81
const Shape * GetShape() const { return mShape; }
82
83
enum class EGroundState
84
{
85
OnGround, ///< Character is on the ground and can move freely.
86
OnSteepGround, ///< Character is on a slope that is too steep and can't climb up any further. The caller should start applying downward velocity if sliding from the slope is desired.
87
NotSupported, ///< Character is touching an object, but is not supported by it and should fall. The GetGroundXXX functions will return information about the touched object.
88
InAir, ///< Character is in the air and is not touching anything.
89
};
90
91
/// Debug function to convert enum values to string
92
static const char * sToString(EGroundState inState);
93
94
///@name Properties of the ground this character is standing on
95
96
/// Current ground state
97
EGroundState GetGroundState() const { return mGroundState; }
98
99
/// Returns true if the player is supported by normal or steep ground
100
bool IsSupported() const { return mGroundState == EGroundState::OnGround || mGroundState == EGroundState::OnSteepGround; }
101
102
/// Get the contact point with the ground
103
RVec3 GetGroundPosition() const { return mGroundPosition; }
104
105
/// Get the contact normal with the ground
106
Vec3 GetGroundNormal() const { return mGroundNormal; }
107
108
/// Velocity in world space of ground
109
Vec3 GetGroundVelocity() const { return mGroundVelocity; }
110
111
/// Material that the character is standing on
112
const PhysicsMaterial * GetGroundMaterial() const { return mGroundMaterial; }
113
114
/// BodyID of the object the character is standing on. Note may have been removed!
115
BodyID GetGroundBodyID() const { return mGroundBodyID; }
116
117
/// Sub part of the body that we're standing on.
118
SubShapeID GetGroundSubShapeID() const { return mGroundBodySubShapeID; }
119
120
/// User data value of the body that we're standing on
121
uint64 GetGroundUserData() const { return mGroundUserData; }
122
123
// Saving / restoring state for replay
124
virtual void SaveState(StateRecorder &inStream) const;
125
virtual void RestoreState(StateRecorder &inStream);
126
127
protected:
128
// Cached physics system
129
PhysicsSystem * mSystem;
130
131
// The shape that the body currently has
132
RefConst<Shape> mShape;
133
134
// The character's world space up axis
135
Vec3 mUp;
136
137
// Every contact behind this plane can support the character
138
Plane mSupportingVolume;
139
140
// Beyond this value there is no max slope
141
static constexpr float cNoMaxSlopeAngle = 0.9999f;
142
143
// Cosine of the maximum angle of slope that character can still walk on
144
float mCosMaxSlopeAngle;
145
146
// Ground properties
147
EGroundState mGroundState = EGroundState::InAir;
148
BodyID mGroundBodyID;
149
SubShapeID mGroundBodySubShapeID;
150
RVec3 mGroundPosition = RVec3::sZero();
151
Vec3 mGroundNormal = Vec3::sZero();
152
Vec3 mGroundVelocity = Vec3::sZero();
153
RefConst<PhysicsMaterial> mGroundMaterial = PhysicsMaterial::sDefault;
154
uint64 mGroundUserData = 0;
155
};
156
157
JPH_NAMESPACE_END
158
159