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 CharacterBaseSettings : public RefTarget<CharacterBaseSettings>
21{
22public:
24
26 virtual ~CharacterBaseSettings() = default;
27
30
35
38
42};
43
45class CharacterBase : public RefTarget<CharacterBase>, public NonCopyable
46{
47public:
49
51 CharacterBase(const CharacterBaseSettings *inSettings, PhysicsSystem *inSystem);
52
54 virtual ~CharacterBase() = default;
55
57 void SetMaxSlopeAngle(float inMaxSlopeAngle) { mCosMaxSlopeAngle = Cos(inMaxSlopeAngle); }
58 float GetCosMaxSlopeAngle() const { return mCosMaxSlopeAngle; }
59
61 void SetUp(Vec3Arg inUp) { mUp = inUp; }
62 Vec3 GetUp() const { return mUp; }
63
65 bool IsSlopeTooSteep(Vec3Arg inNormal) const
66 {
67 // If cos max slope angle is close to one the system is turned off,
68 // otherwise check the angle between the up and normal vector
70 }
71
73 const Shape * GetShape() const { return mShape; }
74
75 enum class EGroundState
76 {
77 OnGround,
80 InAir,
81 };
82
84
87
90
93
96
99
102
105
108
111
112 // Saving / restoring state for replay
113 virtual void SaveState(StateRecorder &inStream) const;
114 virtual void RestoreState(StateRecorder &inStream);
115
116protected:
117 // Cached physics system
119
120 // The shape that the body currently has
122
123 // The character's world space up axis
125
126 // Every contact behind this plane can support the character
128
129 // Beyond this value there is no max slope
130 static constexpr float cNoMaxSlopeAngle = 0.9999f;
131
132 // Cosine of the maximum angle of slope that character can still walk on
134
135 // Ground properties
144};
145
#define JPH_NAMESPACE_END
Definition: Core.h:240
uint64_t uint64
Definition: Core.h:313
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:234
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:46
void SetMaxSlopeAngle(float inMaxSlopeAngle)
Set the maximum angle of slope that character can still walk on (radians)
Definition: CharacterBase.h:57
RVec3 GetGroundPosition() const
Get the contact point with the ground.
Definition: CharacterBase.h:92
EGroundState mGroundState
Definition: CharacterBase.h:136
RefConst< PhysicsMaterial > mGroundMaterial
Definition: CharacterBase.h:142
Vec3 GetUp() const
Definition: CharacterBase.h:62
SubShapeID GetGroundSubShapeID() const
Sub part of the body that we're standing on.
Definition: CharacterBase.h:107
RVec3 mGroundPosition
Definition: CharacterBase.h:139
static constexpr float cNoMaxSlopeAngle
Definition: CharacterBase.h:130
Vec3 mGroundNormal
Definition: CharacterBase.h:140
void SetUp(Vec3Arg inUp)
Set the up vector for the character.
Definition: CharacterBase.h:61
RefConst< Shape > mShape
Definition: CharacterBase.h:121
virtual ~CharacterBase()=default
Destructor.
Vec3 mUp
Definition: CharacterBase.h:124
Vec3 GetGroundVelocity() const
Velocity in world space of ground.
Definition: CharacterBase.h:98
bool IsSupported() const
Returns true if the player is supported by normal or steep ground.
Definition: CharacterBase.h:89
EGroundState
Definition: CharacterBase.h:76
@ OnGround
Character is on the ground and can move freely.
@ OnSteepGround
Character is on a slope that is too steep and can't climb up any further. The caller should start app...
@ NotSupported
Character is touching an object, but is not supported by it and should fall. The GetGroundXXX functio...
@ InAir
Character is in the air and is not touching anything.
bool IsSlopeTooSteep(Vec3Arg inNormal) const
Check if the normal of the ground surface is too steep to walk on.
Definition: CharacterBase.h:65
BodyID mGroundBodyID
Definition: CharacterBase.h:137
float mCosMaxSlopeAngle
Definition: CharacterBase.h:133
float GetCosMaxSlopeAngle() const
Definition: CharacterBase.h:58
virtual void SaveState(StateRecorder &inStream) const
Definition: CharacterBase.cpp:22
BodyID GetGroundBodyID() const
BodyID of the object the character is standing on. Note may have been removed!
Definition: CharacterBase.h:104
EGroundState GetGroundState() const
Current ground state.
Definition: CharacterBase.h:86
const PhysicsMaterial * GetGroundMaterial() const
Material that the character is standing on.
Definition: CharacterBase.h:101
PhysicsSystem * mSystem
Definition: CharacterBase.h:118
uint64 mGroundUserData
Definition: CharacterBase.h:143
virtual void RestoreState(StateRecorder &inStream)
Definition: CharacterBase.cpp:33
Vec3 mGroundVelocity
Definition: CharacterBase.h:141
Vec3 GetGroundNormal() const
Get the contact normal with the ground.
Definition: CharacterBase.h:95
uint64 GetGroundUserData() const
User data value of the body that we're standing on.
Definition: CharacterBase.h:110
Plane mSupportingVolume
Definition: CharacterBase.h:127
SubShapeID mGroundBodySubShapeID
Definition: CharacterBase.h:138
const Shape * GetShape() const
Get the current shape that the character is using.
Definition: CharacterBase.h:73
Base class for configuration of a character.
Definition: CharacterBase.h:21
Plane mSupportingVolume
Definition: CharacterBase.h:34
float mMaxSlopeAngle
Maximum angle of slope that character can still walk on (radians).
Definition: CharacterBase.h:37
virtual JPH_OVERRIDE_NEW_DELETE ~CharacterBaseSettings()=default
Virtual destructor.
Vec3 mUp
Vector indicating the up direction of the character.
Definition: CharacterBase.h:29
RefConst< Shape > mShape
Definition: CharacterBase.h:41
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:28
An infinite plane described by the formula X . Normal + Constant = 0.
Definition: Plane.h:11
Definition: Reference.h:154
Definition: Reference.h:35
Base class for all shapes (collision volume of a body). Defines a virtual interface for collision det...
Definition: Shape.h:170
Definition: StateRecorder.h:15
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:52
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition: Vec3.inl:107