Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
CharacterBase.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
13
15
16class PhysicsSystem;
17class StateRecorder;
18
20class JPH_EXPORT CharacterBaseSettings : public RefTarget<CharacterBaseSettings>
21{
22public:
24
27 CharacterBaseSettings(const CharacterBaseSettings &inSettings) = default;
29
31 virtual ~CharacterBaseSettings() = default;
32
35
39 Plane mSupportingVolume { Vec3::sAxisY(), -1.0e10f };
40
42 float mMaxSlopeAngle = DegreesToRadians(50.0f);
43
47};
48
50class JPH_EXPORT CharacterBase : public RefTarget<CharacterBase>, public NonCopyable
51{
52public:
54
56 CharacterBase(const CharacterBaseSettings *inSettings, PhysicsSystem *inSystem);
57
59 virtual ~CharacterBase() = default;
60
62 void SetMaxSlopeAngle(float inMaxSlopeAngle) { mCosMaxSlopeAngle = Cos(inMaxSlopeAngle); }
63 float GetCosMaxSlopeAngle() const { return mCosMaxSlopeAngle; }
64
66 void SetUp(Vec3Arg inUp) { mUp = inUp; }
67 Vec3 GetUp() const { return mUp; }
68
70 bool IsSlopeTooSteep(Vec3Arg inNormal) const
71 {
72 // If cos max slope angle is close to one the system is turned off,
73 // otherwise check the angle between the up and normal vector
74 return mCosMaxSlopeAngle < cNoMaxSlopeAngle && inNormal.Dot(mUp) < mCosMaxSlopeAngle;
75 }
76
78 const Shape * GetShape() const { return mShape; }
79
80 enum class EGroundState
81 {
82 OnGround,
83 OnSteepGround,
84 NotSupported,
85 InAir,
86 };
87
89 static const char * sToString(EGroundState inState);
90
92
94 EGroundState GetGroundState() const { return mGroundState; }
95
97 bool IsSupported() const { return mGroundState == EGroundState::OnGround || mGroundState == EGroundState::OnSteepGround; }
98
100 RVec3 GetGroundPosition() const { return mGroundPosition; }
101
103 Vec3 GetGroundNormal() const { return mGroundNormal; }
104
106 Vec3 GetGroundVelocity() const { return mGroundVelocity; }
107
109 const PhysicsMaterial * GetGroundMaterial() const { return mGroundMaterial; }
110
112 BodyID GetGroundBodyID() const { return mGroundBodyID; }
113
115 SubShapeID GetGroundSubShapeID() const { return mGroundBodySubShapeID; }
116
118 uint64 GetGroundUserData() const { return mGroundUserData; }
119
120 // Saving / restoring state for replay
121 virtual void SaveState(StateRecorder &inStream) const;
122 virtual void RestoreState(StateRecorder &inStream);
123
124protected:
125 // Cached physics system
127
128 // The shape that the body currently has
130
131 // The character's world space up axis
133
134 // Every contact behind this plane can support the character
136
137 // Beyond this value there is no max slope
138 static constexpr float cNoMaxSlopeAngle = 0.9999f;
139
140 // Cosine of the maximum angle of slope that character can still walk on
142
143 // Ground properties
144 EGroundState mGroundState = EGroundState::InAir;
147 RVec3 mGroundPosition = RVec3::sZero();
148 Vec3 mGroundNormal = Vec3::sZero();
149 Vec3 mGroundVelocity = Vec3::sZero();
151 uint64 mGroundUserData = 0;
152};
153
#define JPH_EXPORT
Definition: Core.h:227
std::uint64_t uint64
Definition: Core.h:443
#define JPH_NAMESPACE_END
Definition: Core.h:367
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
constexpr float DegreesToRadians(float inV)
Convert a value from degrees to radians.
Definition: Math.h:13
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition: Memory.h:29
JPH_INLINE float Cos(float inX)
Cosine of x (input in radians)
Definition: Trigonometry.h:20
ID of a body. This is a way of reasoning about bodies in a multithreaded simulation while avoiding ra...
Definition: BodyID.h:13
Base class for character class.
Definition: CharacterBase.h:51
void SetMaxSlopeAngle(float inMaxSlopeAngle)
Set the maximum angle of slope that character can still walk on (radians)
Definition: CharacterBase.h:62
RVec3 GetGroundPosition() const
Get the contact point with the ground.
Definition: CharacterBase.h:100
Vec3 GetUp() const
Definition: CharacterBase.h:67
SubShapeID GetGroundSubShapeID() const
Sub part of the body that we're standing on.
Definition: CharacterBase.h:115
void SetUp(Vec3Arg inUp)
Set the up vector for the character.
Definition: CharacterBase.h:66
RefConst< Shape > mShape
Definition: CharacterBase.h:129
virtual ~CharacterBase()=default
Destructor.
Vec3 mUp
Definition: CharacterBase.h:132
Vec3 GetGroundVelocity() const
Velocity in world space of ground.
Definition: CharacterBase.h:106
bool IsSupported() const
Returns true if the player is supported by normal or steep ground.
Definition: CharacterBase.h:97
EGroundState
Definition: CharacterBase.h:81
bool IsSlopeTooSteep(Vec3Arg inNormal) const
Check if the normal of the ground surface is too steep to walk on.
Definition: CharacterBase.h:70
BodyID mGroundBodyID
Definition: CharacterBase.h:145
float mCosMaxSlopeAngle
Definition: CharacterBase.h:141
float GetCosMaxSlopeAngle() const
Definition: CharacterBase.h:63
BodyID GetGroundBodyID() const
BodyID of the object the character is standing on. Note may have been removed!
Definition: CharacterBase.h:112
EGroundState GetGroundState() const
Current ground state.
Definition: CharacterBase.h:94
const PhysicsMaterial * GetGroundMaterial() const
Material that the character is standing on.
Definition: CharacterBase.h:109
PhysicsSystem * mSystem
Definition: CharacterBase.h:126
Vec3 GetGroundNormal() const
Get the contact normal with the ground.
Definition: CharacterBase.h:103
uint64 GetGroundUserData() const
User data value of the body that we're standing on.
Definition: CharacterBase.h:118
Plane mSupportingVolume
Definition: CharacterBase.h:135
SubShapeID mGroundBodySubShapeID
Definition: CharacterBase.h:146
const Shape * GetShape() const
Get the current shape that the character is using.
Definition: CharacterBase.h:78
Base class for configuration of a character.
Definition: CharacterBase.h:21
virtual ~CharacterBaseSettings()=default
Virtual destructor.
CharacterBaseSettings(const CharacterBaseSettings &inSettings)=default
RefConst< Shape > mShape
Definition: CharacterBase.h:46
JPH_OVERRIDE_NEW_DELETE CharacterBaseSettings()=default
Constructor.
Class that makes another class non-copyable. Usage: Inherit from NonCopyable.
Definition: NonCopyable.h:11
Definition: PhysicsMaterial.h:23
static RefConst< PhysicsMaterial > sDefault
Default material that is used when a shape has no materials defined.
Definition: PhysicsMaterial.h:31
Definition: PhysicsSystem.h:29
An infinite plane described by the formula X . Normal + Constant = 0.
Definition: Plane.h:11
Definition: Reference.h:151
Definition: Reference.h:35
RefTarget & operator=(const RefTarget &)
Assignment operator.
Definition: Reference.h:48
Base class for all shapes (collision volume of a body). Defines a virtual interface for collision det...
Definition: Shape.h:178
Definition: StateRecorder.h:48
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
JPH_INLINE float Dot(Vec3Arg inV2) const
Dot product.
Definition: Vec3.inl:637
static JPH_INLINE Vec3 sAxisY()
Definition: Vec3.h:53
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition: Vec3.inl:107