Plasma Engine  2.0
Loading...
Searching...
No Matches
JoltRagdollComponent.h
1#pragma once
2
3#include <Core/ResourceManager/ResourceHandle.h>
4#include <Core/World/ComponentManager.h>
5#include <Foundation/Math/Declarations.h>
6#include <JoltPlugin/JoltPluginDLL.h>
7
11class plJoltMaterial;
17
18namespace JPH
19{
20 class Ragdoll;
21 class RagdollSettings;
22 class Shape;
23} // namespace JPH
24
27
28class PL_JOLTPLUGIN_DLL plJoltRagdollComponentManager : public plComponentManager<class plJoltRagdollComponent, plBlockStorageType::FreeList>
29{
30public:
33
34 virtual void Initialize() override;
35
36private:
37 friend class plJoltWorldModule;
38 friend class plJoltRagdollComponent;
39
40 void Update(const plWorldModule::UpdateContext& context);
41};
42
45{
46 using StorageType = plUInt8;
47
55};
56
57PL_DECLARE_REFLECTABLE_TYPE(PL_JOLTPLUGIN_DLL, plJoltRagdollStartMode);
58
60
74class PL_JOLTPLUGIN_DLL plJoltRagdollComponent : public plComponent
75{
77
79 // plComponent
80
81public:
82 virtual void SerializeComponent(plWorldWriter& inout_stream) const override;
83 virtual void DeserializeComponent(plWorldReader& inout_stream) override;
84
85protected:
86 virtual void OnSimulationStarted() override;
87 virtual void OnDeactivated() override;
88
90 // plJoltRagdollComponent
91
92public:
95
97 plUInt32 GetObjectFilterID() const { return m_uiObjectFilterID; } // [ scriptable ]
98
102 void SetGravityFactor(float fFactor); // [ property ]
103 float GetGravityFactor() const { return m_fGravityFactor; } // [ property ]
104
109 bool m_bSelfCollision = false; // [ property ]
110
112 float m_fStiffnessFactor = 1.0f; // [ property ]
113
115 float m_fMass = 50.0f; // [ property ]
116
118 void SetStartMode(plEnum<plJoltRagdollStartMode> mode); // [ property ]
119 plEnum<plJoltRagdollStartMode> GetStartMode() const { return m_StartMode; } // [ property ]
120
122 void OnMsgPhysicsAddImpulse(plMsgPhysicsAddImpulse& ref_msg); // [ msg handler ]
123
128 void OnMsgPhysicsAddForce(plMsgPhysicsAddForce& ref_msg); // [ msg handler ]
129
140 void SetInitialImpulse(const plVec3& vPosition, const plVec3& vDirectionAndStrength); // [ scriptable ]
141
143 void AddInitialImpulse(const plVec3& vPosition, const plVec3& vDirectionAndStrength); // [ scriptable ]
144
146 float m_fOwnerVelocityScale = 1.0f; // [ property ]
147
152 float m_fCenterVelocity = 0.0f; // [ property ]
153
155 float m_fCenterAngularVelocity = 0.0f; // [ property ]
156
158 plVec3 m_vCenterPosition = plVec3::MakeZero(); // [ property ]
159
171 void SetJointTypeOverride(plStringView sJointName, plEnum<plSkeletonJointType> type);
172
173 void OnAnimationPoseUpdated(plMsgAnimationPoseUpdated& ref_msg); // [ msg handler ]
174 void OnRetrieveBoneState(plMsgRetrieveBoneState& ref_msg) const; // [ msg handler ]
175
176protected:
177 plEnum<plJoltRagdollStartMode> m_StartMode; // [ property ]
178 float m_fGravityFactor = 1.0f; // [ property ]
179
180 struct Limb
181 {
182 plUInt16 m_uiPartIndex = plInvalidJointIndex;
183 };
184
186 {
187 plTransform m_GlobalTransform;
188 plUInt16 m_uiJoltPartIndex = plInvalidJointIndex;
189 };
190
191 void Update(bool bForce);
192 plResult EnsureSkeletonIsKnown();
193 void CreateLimbsFromBindPose();
194 void CreateLimbsFromCurrentMeshPose();
195 void DestroyAllLimbs();
196 void CreateLimbsFromPose(const plMsgAnimationPoseUpdated& pose);
197 bool HasCreatedLimbs() const;
198 plTransform GetRagdollRootTransform() const;
199 void UpdateOwnerPosition();
200 void RetrieveRagdollPose();
201 void SendAnimationPoseMsg();
202 void ConfigureRagdollPart(void* pRagdollSettingsPart, const plTransform& globalTransform, plUInt8 uiCollisionLayer, plJoltWorldModule& worldModule);
203 void CreateAllLimbs(const plSkeletonResource& skeletonResource, const plMsgAnimationPoseUpdated& pose, plJoltWorldModule& worldModule, float fObjectScale);
204 void ComputeLimbModelSpaceTransform(plTransform& transform, const plMsgAnimationPoseUpdated& pose, plUInt32 uiPoseJointIndex);
205 void ComputeLimbGlobalTransform(plTransform& transform, const plMsgAnimationPoseUpdated& pose, plUInt32 uiPoseJointIndex);
206 void CreateLimb(const plSkeletonResource& skeletonResource, plMap<plUInt16, LimbConstructionInfo>& limbConstructionInfos, plArrayPtr<const plSkeletonResourceGeometry*> geometries, const plMsgAnimationPoseUpdated& pose, plJoltWorldModule& worldModule, float fObjectScale);
207 JPH::Shape* CreateLimbGeoShape(const LimbConstructionInfo& limbConstructionInfo, const plSkeletonResourceGeometry& geo, const plJoltMaterial* pJoltMaterial, const plQuat& qBoneDirAdjustment, const plTransform& skeletonRootTransform, plTransform& out_shapeTransform, float fObjectScale);
208 void CreateAllLimbGeoShapes(const LimbConstructionInfo& limbConstructionInfo, plArrayPtr<const plSkeletonResourceGeometry*> geometries, const plSkeletonJoint& thisLimbJoint, const plSkeletonResource& skeletonResource, float fObjectScale);
209 virtual void ApplyPartInitialVelocity();
210 void ApplyBodyMass();
211 void ApplyInitialImpulse(plJoltWorldModule& worldModule, float fMaxImpulse);
212
213 plSkeletonResourceHandle m_hSkeleton;
214 plDynamicArray<plMat4> m_CurrentLimbTransforms;
215
216 plUInt32 m_uiObjectFilterID = plInvalidIndex;
217 plUInt32 m_uiJoltUserDataIndex = plInvalidIndex;
218 plJoltUserData* m_pJoltUserData = nullptr;
219
220 JPH::Ragdoll* m_pRagdoll = nullptr;
221 JPH::RagdollSettings* m_pRagdollSettings = nullptr;
222 plDynamicArray<Limb> m_Limbs;
223 plTransform m_RootBodyLocalTransform;
224 plTime m_ElapsedTimeSinceUpdate = plTime::MakeZero();
225
226 plVec3 m_vInitialImpulsePosition = plVec3::MakeZero();
227 plVec3 m_vInitialImpulseDirection = plVec3::MakeZero();
228 plUInt8 m_uiNumInitialImpulses = 0;
229
231 {
232 plTempHashedString m_sJointName;
233 bool m_bOverrideType = false;
234 plEnum<plSkeletonJointType> m_JointType;
235 };
236
237 plDynamicArray<JointOverride> m_JointOverrides;
238
240
241 void SetupLimbJoints(const plSkeletonResource* pSkeleton);
242 void CreateLimbJoint(const plSkeletonJoint& thisJoint, void* pParentBodyDesc, void* pThisBodyDesc);
243};
This class encapsulates an array and it's size. It is recommended to use this class instead of plain ...
Definition ArrayPtr.h:37
Base class of all component types.
Definition Component.h:25
Definition ComponentManager.h:88
Definition DynamicArray.h:81
Definition JoltMaterial.h:7
Creates a physics ragdoll for an animated mesh and creates animation poses from the physics simulatio...
Definition JoltRagdollComponent.h:75
plUInt32 GetObjectFilterID() const
Returns the object ID used for all bodies in the ragdoll. This can be used to ignore the entire ragdo...
Definition JoltRagdollComponent.h:97
Definition JoltRagdollComponent.h:29
Definition JoltUserData.h:18
Definition JoltWorldModule.h:29
Definition Map.h:408
Describes a single joint. The transforms of the joints are in their local space and thus need to be c...
Definition Skeleton.h:27
Definition SkeletonResource.h:47
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
A class to use together with plHashedString for quick comparisons with temporary strings that need no...
Definition HashedString.h:151
static plVec3Template< float > MakeZero()
Definition Vec3.h:38
A world encapsulates a scene graph of game objects and various component managers and their component...
Definition World.h:22
virtual void OnSimulationStarted()
This method is called at the start of the next world update when the world is simulated....
Definition WorldModule.h:105
virtual void Initialize()
This method is called after the constructor. A derived type can override this method to do initializa...
Definition WorldModule.h:98
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 custom enum implementation that allows to define the underlying storage type to control its memory ...
Definition Enum.h:37
Definition JoltRagdollComponent.h:231
Definition JoltRagdollComponent.h:186
Definition JoltRagdollComponent.h:181
With which pose a ragdoll should start.
Definition JoltRagdollComponent.h:45
Enum
Definition JoltRagdollComponent.h:49
@ WithNextAnimPose
The ragdoll waits for a new pose and then starts simulating with that.
Definition JoltRagdollComponent.h:51
@ WithBindPose
The ragdoll uses the bind pose (or rest pose) to start with.
Definition JoltRagdollComponent.h:50
@ WithCurrentMeshPose
The ragdoll retrieves the current pose from the animated mesh and starts simulating with that.
Definition JoltRagdollComponent.h:52
Used by components that skin a mesh to inform children whenever a new pose has been computed.
Definition Declarations.h:68
Used to apply a physical force on the object.
Definition PhysicsWorldModule.h:201
Used to apply a physical impulse on the object.
Definition PhysicsWorldModule.h:187
Queries the local transforms of each bone in an object with a skeleton.
Definition Declarations.h:122
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54
Definition SkeletonResource.h:12
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12
PL_ALWAYS_INLINE static constexpr plTime MakeZero()
Creates an instance of plTime that was initialized with zero.
Definition Time.h:42
Definition WorldModule.h:33