Plasma Engine  2.0
Loading...
Searching...
No Matches
JoltDefaultCharacterComponent.h
1#pragma once
2
3#include <JoltPlugin/Character/JoltCharacterControllerComponent.h>
4
6
8
17{
19
21 // plComponent
22
23public:
24 virtual void SerializeComponent(plWorldWriter& inout_stream) const override;
25 virtual void DeserializeComponent(plWorldReader& inout_stream) override;
26
27 bool GetAllowJumpInAir() const { return m_bAllowJumpInAir; }
28 void SetAllowJumpInAir(bool inAllow) { m_bAllowJumpInAir = inAllow; }
29protected:
30 virtual void OnSimulationStarted() override;
31 virtual void OnActivated() override;
32 virtual void OnDeactivated() override;
33
35 // plJoltCharacterControllerComponent
36
37public:
40
41 enum class GroundState : plUInt8
42 {
43 OnGround,
44 Sliding,
45 InAir,
46 };
47
49 plAngle m_RotateSpeed = plAngle::MakeFromDegree(90.0f); // [ property ]
50
52 float m_fShapeRadius = 0.25f;
53
55 float m_fCylinderHeightCrouch = 0.9f;
56
58 float m_fCylinderHeightStand = 1.7f;
59
61 float m_fFootRadius = 0.15f;
62
64 float m_fWalkSpeedCrouching = 0.5f;
65
67 float m_fWalkSpeedStanding = 1.5f;
68
70 float m_fWalkSpeedRunning = 3.5f;
71
73 float m_fMaxStepUp = 0.25f;
74
76 float m_fMaxStepDown = 0.25f;
77
79 float m_fJumpImpulse = 5.0f;
80
82 bool m_bAllowJumpInAir = false;
83
86
89
91 float m_fWalkInteractionDistance = 1.0f; // [ property ]
92
94 float m_fRunInteractionDistance = 3.0f; // [ property ]
95
96 void SetWalkSurfaceInteraction(const char* szName) { m_sWalkSurfaceInteraction.Assign(szName); } // [ property ]
97 const char* GetWalkSurfaceInteraction() const { return m_sWalkSurfaceInteraction.GetData(); } // [ property ]
98
99 void SetFallbackWalkSurfaceFile(const char* szFile); // [ property ]
100 const char* GetFallbackWalkSurfaceFile() const; // [ property ]
101
103 float m_fAirSpeed = 2.5f; // [ property ]
104
106 float m_fAirFriction = 0.5f; // [ property ]
107
109 void SetHeadObjectReference(const char* szReference); // [ property ]
110
112 void SetInputState(plMsgMoveCharacterController& ref_msg); // [ msg handler ]
113
115 float GetCurrentCapsuleHeight() const;
116
118 float GetCurrentCylinderHeight() const;
119
121 virtual float GetShapeRadius() const override;
122
123 virtual bool IsTouchingGround() override { return m_LastGroundState == GroundState::OnGround; }
124
125 GroundState GetGroundState() const { return m_LastGroundState; }
126 bool IsStandingOnGround() const { return m_LastGroundState == GroundState::OnGround; } // [ scriptable ]
127 bool IsSlidingOnGround() const { return m_LastGroundState == GroundState::Sliding; } // [ scriptable ]
128 bool IsInAir() const { return m_LastGroundState == GroundState::InAir; } // [ scriptable ]
129 virtual bool IsCrouching() override { return m_uiIsCrouchingBit; } // [ scriptable ]
130
132 virtual void TeleportCharacter(const plVec3& vGlobalFootPosition) override;
133
134 struct Config
135 {
136 bool m_bAllowJump = true;
137 bool m_bAllowCrouch = true;
138 bool m_bApplyGroundVelocity = true;
139 plVec3 m_vVelocity = plVec3::MakeZero();
140 float m_fPushDownForce = 0;
141 plHashedString m_sGroundInteraction;
142 float m_fGroundInteractionDistanceThreshold = 1.0f;
143 float m_fMaxStepUp = 0;
144 float m_fMaxStepDown = 0;
145 };
146
147protected:
148 void OnUpdateLocalBounds(plMsgUpdateLocalBounds& msg) const;
149 virtual void OnApplyRootMotion(plMsgApplyRootMotion& msg);
150
151 virtual void DetermineConfig(Config& out_inputs);
152
153 virtual void UpdateCharacter() override;
154 virtual void ApplyRotationZ();
155
157 void ResetInputState();
158
159 void ResetInternalState();
160
162 virtual JPH::Ref<JPH::Shape> MakeNextCharacterShape() override;
163
164 void ApplyCrouchState();
165 void InteractWithSurfaces(const ContactPoint& contact, const Config& cfg);
166
167 void StoreLateralVelocity();
168 void ClampLateralVelocity();
169 void MoveHeadObject();
170 void DebugVisualizations();
171
172 void CheckFeet();
173
174 GroundState m_LastGroundState = GroundState::InAir;
175
176 plUInt8 m_uiInputJumpBit : 1;
177 plUInt8 m_uiInputCrouchBit : 1;
178 plUInt8 m_uiInputRunBit : 1;
179 plUInt8 m_uiIsCrouchingBit : 1;
180 plAngle m_InputRotateZ;
181 plVec2 m_vInputDirection = plVec2::MakeZero();
182 float m_fVelocityUp = 0.0f;
183 float m_fNextCylinderHeight = 0;
184 float m_fAccumulatedWalkDistance = 0.0f;
185 plVec2 m_vVelocityLateral = plVec2::MakeZero();
186 plTransform m_PreviousTransform;
187 bool m_bFeetOnSolidGround = false;
188
189 float m_fCurrentCylinderHeight = 0;
190
191 float m_fHeadHeightOffset = 0.0f;
192 float m_fHeadTargetHeight = 0.0f;
193 plGameObjectHandle m_hHeadObject;
194
195 plVec3 m_vAbsoluteRootMotion = plVec3::MakeZero();
196
197 plUInt32 m_uiUserDataIndex = plInvalidIndex;
198 plUInt32 m_uiJoltBodyID = plInvalidIndex;
199
200private:
201 const char* DummyGetter() const { return nullptr; }
202};
Float wrapper struct for a safe usage and conversions of angles.
Definition Angle.h:10
static constexpr plAngle MakeFromDegree(float fDegree)
Creates an instance of plAngle that was initialized from degree. (Performs a conversion)
Definition Angle_inl.h:33
virtual void OnActivated()
This method is called when the component gets activated.
Definition Component.cpp:140
Definition ComponentManager.h:88
This class is optimized to take nearly no memory (sizeof(void*)) and to allow very fast checks whethe...
Definition HashedString.h:25
void Assign(const char(&string)[N])
Assigning a new string from a string constant is a slow operation, but the hash computation can happe...
const char * GetData() const
Gives access to the actual string data, so you can do all the typical (read-only) string operations o...
Definition HashedString_inl.h:110
Base class for character controllers (CC).
Definition JoltCharacterControllerComponent.h:27
virtual void OnSimulationStarted() override
This method is called once for active components, at the start of the next world update,...
Definition JoltCharacterControllerComponent.cpp:102
virtual float GetShapeRadius() const =0
Returns the radius of the shape. This never changes at runtime.
virtual void DeserializeComponent(plWorldReader &inout_stream) override
Override this to load the current state of the component from the given stream.
Definition JoltCharacterControllerComponent.cpp:69
virtual void TeleportCharacter(const plVec3 &vGlobalFootPos) override
Teleports the CC to the desired global position. Ignores obstacles on the path.
Definition JoltCharacterControllerComponent.h:108
virtual JPH::Ref< JPH::Shape > MakeNextCharacterShape()=0
Returns the shape that the character is supposed to use next.
virtual void OnDeactivated() override
This method is called when the component gets deactivated.
Definition JoltCharacterControllerComponent.cpp:84
virtual void SerializeComponent(plWorldWriter &inout_stream) const override
Override this to save the current state of the component to the given stream.
Definition JoltCharacterControllerComponent.cpp:55
virtual void UpdateCharacter()=0
Called up to once per frame, but potentially less often, if physics updates were skipped due to high ...
An example character controller (CC) implementation build upon plJoltCharacterControllerComponent.
Definition JoltDefaultCharacterComponent.h:17
virtual bool IsCrouching() override
Checks whether the CC is currently in the crouch state.
Definition JoltDefaultCharacterComponent.h:129
plHashedString m_sWalkSurfaceInteraction
The surface interaction to spawn regularly when walking.
Definition JoltDefaultCharacterComponent.h:85
GroundState
Definition JoltDefaultCharacterComponent.h:42
virtual bool IsTouchingGround() override
Checks whether the CC is currently touching the ground.
Definition JoltDefaultCharacterComponent.h:123
plSurfaceResourceHandle m_hFallbackWalkSurface
The surface type to use for interactions, when no other surface type is available.
Definition JoltDefaultCharacterComponent.h:88
static constexpr plVec2Template< float > MakeZero()
Definition Vec2.h:49
static plVec3Template< float > MakeZero()
Definition Vec3.h:38
Reads a world description from a stream. Allows to instantiate that world multiple times in different...
Definition WorldReader.h:47
Stores an entire plWorld in a stream.
Definition WorldWriter.h:13
A handle to a game object.
Definition Declarations.h:76
Describes a point where the CC collided with other geometry.
Definition JoltCharacterControllerComponent.h:50
Definition JoltDefaultCharacterComponent.h:135
This message is sent when animation root motion data is available.
Definition Declarations.h:109
Definition CharacterControllerComponent.h:10
Definition UpdateLocalBoundsMessage.h:9