Plasma Engine  2.0
Loading...
Searching...
No Matches
JoltCharacterControllerComponent.h
1#pragma once
2
3#include <Core/World/World.h>
4#include <JoltPlugin/JoltPluginDLL.h>
5#include <JoltPlugin/System/JoltCollisionFiltering.h>
6#include <GameEngine/Physics/CharacterControllerComponent.h>
7
10
11namespace JPH
12{
13 class CharacterVirtual;
14 class TempAllocator;
15} // namespace JPH
16
17PL_DECLARE_FLAGS(plUInt32, plJoltCharacterDebugFlags, PrintState, VisShape, VisContacts, VisCasts, VisGroundContact, VisFootCheck);
18PL_DECLARE_REFLECTABLE_TYPE(PL_JOLTPLUGIN_DLL, plJoltCharacterDebugFlags);
19
27{
28 PL_DECLARE_ABSTRACT_COMPONENT_TYPE(plJoltCharacterControllerComponent, plCharacterControllerComponent);
29
31 // plComponent
32
33public:
34 virtual void SerializeComponent(plWorldWriter& inout_stream) const override;
35 virtual void DeserializeComponent(plWorldReader& inout_stream) override;
36
37protected:
38 virtual void OnSimulationStarted() override;
39 virtual void OnDeactivated() override;
40
42 // plJoltCharacterControllerComponent
43
44public:
47
50 {
51 float m_fCastFraction = 0.0f;
52 plVec3 m_vPosition;
53 plVec3 m_vSurfaceNormal;
54 plVec3 m_vContactNormal;
55 JPH::BodyID m_BodyID;
56 JPH::SubShapeID m_SubShapeID;
57 };
58
65 void SetObjectToIgnore(plUInt32 uiObjectFilterID);
66
68 void ClearObjectToIgnore();
69
70public:
72 plUInt8 m_uiCollisionLayer = 0; // [ property ]
73
75 plUInt8 m_uiPresenceCollisionLayer = 0; // [ property ]
76
79
81 void SetMaxClimbingSlope(plAngle slope); // [ property ]
82 plAngle GetMaxClimbingSlope() const { return m_MaxClimbingSlope; } // [ property ]
83
85 void SetMass(float fMass); // [ property ]
86 float GetMass() const { return m_fMass; } // [ property ]
87
89 void SetStrength(float fStrength); // [ property ]
90 float GetStrength() const { return m_fStrength; } // [ property ]
91
97 virtual void RawMove(const plVec3& vMoveDeltaGlobal) override{}; // [ scriptable ]
98
102 virtual void MoveCharacter(plMsgMoveCharacterController& msg) override{}; // [ msg handler ]
103
108 virtual void TeleportCharacter(const plVec3& vGlobalFootPos) override{}; // [ scriptable ]
109
116 virtual bool IsDestinationUnobstructed(const plVec3& vGlobalFootPos, float fCharacterHeight) override { return false; }; // [ scriptable ]
117
119 virtual bool IsTouchingGround() override { return true; }; // [ scriptable ]
120
122 virtual bool IsCrouching() override { return false; }; // [ scriptable ]
123
124private:
125 plAngle m_MaxClimbingSlope = plAngle::MakeFromDegree(45); // [ property ]
126 float m_fMass = 70.0f; // [ property ]
127 float m_fStrength = 500.0f; // [ property ]
128
129protected:
131 PL_ALWAYS_INLINE float GetUpdateTimeDelta() const { return m_fUpdateTimeDelta; }
132
134 PL_ALWAYS_INLINE float GetInverseUpdateTimeDelta() const { return m_fInverseUpdateTimeDelta; }
135
142 virtual JPH::Ref<JPH::Shape> MakeNextCharacterShape() = 0;
143
145 virtual float GetShapeRadius() const = 0;
146
151 virtual void UpdateCharacter() = 0;
152
154 JPH::CharacterVirtual* GetJoltCharacter() { return m_pCharacter; }
155 const JPH::CharacterVirtual* GetJoltCharacter() const { return m_pCharacter; }
156
158 plResult TryChangeShape(JPH::Shape* pNewShape);
159
161 void RawMoveWithVelocity(const plVec3& vVelocity, float fMaxStairStepUp, float fMaxStepDown);
162
164 void RawMoveIntoDirection(const plVec3& vDirection);
165
167 void RawMoveToPosition(const plVec3& vTargetPosition);
168
170 void TeleportToPosition(const plVec3& vGlobalFootPos);
171
175 bool StickToGround(float fMaxDist);
176
178 void CollectCastContacts(plDynamicArray<ContactPoint>& out_Contacts, const JPH::Shape* pShape, const plVec3& vQueryPosition, const plQuat& qQueryRotation, const plVec3& vSweepDir) const;
179
183 void CollectContacts(plDynamicArray<ContactPoint>& out_Contacts, const JPH::Shape* pShape, const plVec3& vQueryPosition, const plQuat& qQueryRotation, float fCollisionTolerance) const;
184
191 plVec3 GetContactVelocityAndPushAway(const ContactPoint& contact, float fPushForce);
192
196 void SpawnContactInteraction(const ContactPoint& contact, const plHashedString& sSurfaceInteraction, plSurfaceResourceHandle hFallbackSurface, const plVec3& vInteractionNormal = plVec3(0, 0, 1));
197
199 void VisualizeContact(const ContactPoint& contact, const plColor& color) const;
200
202 void VisualizeContacts(const plDynamicArray<ContactPoint>& contacts, const plColor& color) const;
203
204private:
205 friend class plJoltWorldModule;
206
207 void Update(plTime deltaTime);
208
209 float m_fUpdateTimeDelta = 0.1f;
210 float m_fInverseUpdateTimeDelta = 1.0f;
211 JPH::CharacterVirtual* m_pCharacter = nullptr;
212
213 void CreatePresenceBody();
214 void RemovePresenceBody();
215 void MovePresenceBody(plTime deltaTime);
216
217 plUInt32 m_uiPresenceBodyID = plInvalidIndex;
218 plUInt32 m_uiPresenceBodyAddCounter = 0;
219
220 plJoltBodyFilter m_BodyFilter;
221 plUInt32 m_uiUserDataIndex = plInvalidIndex;
222};
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
Base class for implementations of a character controller.
Definition CharacterControllerComponent.h:30
virtual void DeserializeComponent(plWorldReader &inout_stream) override
Override this to load the current state of the component from the given stream.
Definition CharacterControllerComponent.cpp:64
virtual void SerializeComponent(plWorldWriter &inout_stream) const override
Override this to save the current state of the component to the given stream.
Definition CharacterControllerComponent.cpp:58
plColor represents an RGBA color in linear color space. Values are stored as float,...
Definition Color.h:44
virtual void OnDeactivated()
This method is called when the component gets deactivated.
Definition Component.cpp:142
virtual void OnSimulationStarted()
This method is called once for active components, at the start of the next world update,...
Definition Component.cpp:144
Definition DynamicArray.h:81
This class is optimized to take nearly no memory (sizeof(void*)) and to allow very fast checks whethe...
Definition HashedString.h:25
Definition JoltCollisionFiltering.h:103
Base class for character controllers (CC).
Definition JoltCharacterControllerComponent.h:27
virtual float GetShapeRadius() const =0
Returns the radius of the shape. This never changes at runtime.
plBitflags< plJoltCharacterDebugFlags > m_DebugFlags
What aspects of the CC to visualize.
Definition JoltCharacterControllerComponent.h:78
PL_ALWAYS_INLINE float GetInverseUpdateTimeDelta() const
Returns the inverse of update time delta.
Definition JoltCharacterControllerComponent.h:134
PL_ALWAYS_INLINE float GetUpdateTimeDelta() const
Returns the time delta to use for updating the character. This may differ from the world delta.
Definition JoltCharacterControllerComponent.h:131
virtual void RawMove(const plVec3 &vMoveDeltaGlobal) override
Attempts to move the character into the given direction.
Definition JoltCharacterControllerComponent.h:97
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 bool IsTouchingGround() override
Checks whether the CC is currently touching the ground.
Definition JoltCharacterControllerComponent.h:119
virtual bool IsCrouching() override
Checks whether the CC is currently in the crouch state.
Definition JoltCharacterControllerComponent.h:122
virtual void UpdateCharacter()=0
Called up to once per frame, but potentially less often, if physics updates were skipped due to high ...
virtual void MoveCharacter(plMsgMoveCharacterController &msg) override
Instructs the CC to move in certain directions. An implementation can queue the request for later pro...
Definition JoltCharacterControllerComponent.h:102
JPH::CharacterVirtual * GetJoltCharacter()
Gives access to the internally used JPH::CharacterVirtual.
Definition JoltCharacterControllerComponent.h:154
virtual bool IsDestinationUnobstructed(const plVec3 &vGlobalFootPos, float fCharacterHeight) override
Checks whether the CC can be teleported to the target position without getting stuck.
Definition JoltCharacterControllerComponent.h:116
Definition JoltWorldModule.h:29
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
The plBitflags class allows you to work with type-safe bitflags.
Definition Bitflags.h:82
Describes a point where the CC collided with other geometry.
Definition JoltCharacterControllerComponent.h:50
Definition CharacterControllerComponent.h:10
Definition UpdateLocalBoundsMessage.h:9
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12