Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
Ragdoll.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
8#include <Jolt/Core/Result.h>
14
16
17class Ragdoll;
18class PhysicsSystem;
19
21class JPH_EXPORT RagdollSettings : public RefTarget<RagdollSettings>
22{
24
25public:
28 bool Stabilize();
29
42 void DisableParentChildCollisions(const Mat44 *inJointMatrices = nullptr, float inMinSeparationDistance = 0.0f);
43
48 void SaveBinaryState(StreamOut &inStream, bool inSaveShapes, bool inSaveGroupFilter) const;
49
51
53 static RagdollResult sRestoreFromBinaryState(StreamIn &inStream);
54
57 Ragdoll * CreateRagdoll(CollisionGroup::GroupID inCollisionGroup, uint64 inUserData, PhysicsSystem *inSystem) const;
58
60 const Skeleton * GetSkeleton() const { return mSkeleton; }
61 Skeleton * GetSkeleton() { return mSkeleton; }
62
64 void CalculateBodyIndexToConstraintIndex();
65
68 const Array<int> & GetBodyIndexToConstraintIndex() const { return mBodyIndexToConstraintIndex; }
69
71 int GetConstraintIndexForBodyIndex(int inBodyIndex) const { return mBodyIndexToConstraintIndex[inBodyIndex]; }
72
74 void CalculateConstraintIndexToBodyIdxPair();
75
76 using BodyIdxPair = pair<int, int>;
77
79 const Array<BodyIdxPair> & GetConstraintIndexToBodyIdxPair() const { return mConstraintIndexToBodyIdxPair; }
80
82 BodyIdxPair GetBodyIndicesForConstraintIndex(int inConstraintIndex) const { return mConstraintIndexToBodyIdxPair[inConstraintIndex]; }
83
92
95
98 {
100
101 public:
104 AdditionalConstraint(int inBodyIdx1, int inBodyIdx2, TwoBodyConstraintSettings *inConstraint) : mBodyIdx { inBodyIdx1, inBodyIdx2 }, mConstraint(inConstraint) { }
105
106 int mBodyIdx[2];
108 };
109
112
115
118
121
122private:
124 Array<int> mBodyIndexToConstraintIndex;
125
127 Array<BodyIdxPair> mConstraintIndexToBodyIdxPair;
128};
129
131class JPH_EXPORT Ragdoll : public RefTarget<Ragdoll>, public NonCopyable
132{
133public:
135
137 explicit Ragdoll(PhysicsSystem *inSystem) : mSystem(inSystem) { }
138
140 ~Ragdoll();
141
143 void AddToPhysicsSystem(EActivation inActivationMode, bool inLockBodies = true);
144
146 void RemoveFromPhysicsSystem(bool inLockBodies = true);
147
149 void Activate(bool inLockBodies = true);
150
155 bool IsActive(bool inLockBodies = true) const;
156
158 void SetGroupID(CollisionGroup::GroupID inGroupID, bool inLockBodies = true);
159
161 void SetPose(const SkeletonPose &inPose, bool inLockBodies = true);
162
164 void SetPose(RVec3Arg inRootOffset, const Mat44 *inJointMatrices, bool inLockBodies = true);
165
167 void GetPose(SkeletonPose &outPose, bool inLockBodies = true);
168
170 void GetPose(RVec3 &outRootOffset, Mat44 *outJointMatrices, bool inLockBodies = true);
171
173 void ResetWarmStart();
174
176 void DriveToPoseUsingKinematics(const SkeletonPose &inPose, float inDeltaTime, bool inLockBodies = true);
177
179 void DriveToPoseUsingKinematics(RVec3Arg inRootOffset, const Mat44 *inJointMatrices, float inDeltaTime, bool inLockBodies = true);
180
182 void DriveToPoseUsingMotors(const SkeletonPose &inPose);
183
185 void SetLinearAndAngularVelocity(Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity, bool inLockBodies = true);
186
188 void SetLinearVelocity(Vec3Arg inLinearVelocity, bool inLockBodies = true);
189
191 void AddLinearVelocity(Vec3Arg inLinearVelocity, bool inLockBodies = true);
192
194 void AddImpulse(Vec3Arg inImpulse, bool inLockBodies = true);
195
197 void GetRootTransform(RVec3 &outPosition, Quat &outRotation, bool inLockBodies = true) const;
198
200 size_t GetBodyCount() const { return mBodyIDs.size(); }
201
203 BodyID GetBodyID(int inBodyIndex) const { return mBodyIDs[inBodyIndex]; }
204
206 const Array<BodyID> & GetBodyIDs() const { return mBodyIDs; }
207
209 size_t GetConstraintCount() const { return mConstraints.size(); }
210
212 TwoBodyConstraint * GetConstraint(int inConstraintIndex) { return mConstraints[inConstraintIndex]; }
213
215 const TwoBodyConstraint * GetConstraint(int inConstraintIndex) const { return mConstraints[inConstraintIndex]; }
216
218 AABox GetWorldSpaceBounds(bool inLockBodies = true) const;
219
221 const RagdollSettings * GetRagdollSettings() const { return mRagdollSettings; }
222
223private:
225 friend class RagdollSettings;
226
228 RefConst<RagdollSettings> mRagdollSettings;
229
231 Array<BodyID> mBodyIDs;
232
234 Array<Ref<TwoBodyConstraint>> mConstraints;
235
237 PhysicsSystem * mSystem;
238};
239
#define JPH_EXPORT
Definition Core.h:236
std::uint64_t uint64
Definition Core.h:457
#define JPH_NAMESPACE_END
Definition Core.h:379
#define JPH_NAMESPACE_BEGIN
Definition Core.h:373
EActivation
Enum used by AddBody to determine if the body needs to be initially active.
Definition EActivation.h:11
@ Activate
Activate the body, making it part of the simulation.
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition Memory.h:31
#define JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(linkage, class_name)
Definition SerializableObject.h:80
Axis aligned box.
Definition AABox.h:16
Definition Array.h:36
Settings for constructing a rigid body.
Definition BodyCreationSettings.h:31
ID of a body. This is a way of reasoning about bodies in a multithreaded simulation while avoiding ra...
Definition BodyID.h:13
uint32 GroupID
Definition CollisionGroup.h:24
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition Mat44.h:13
Class that makes another class non-copyable. Usage: Inherit from NonCopyable.
Definition NonCopyable.h:11
Definition PhysicsSystem.h:29
Definition Quat.h:33
Runtime ragdoll information.
Definition Ragdoll.h:132
const TwoBodyConstraint * GetConstraint(int inConstraintIndex) const
Access a constraint by index.
Definition Ragdoll.h:215
size_t GetConstraintCount() const
Get number of constraints in the ragdoll.
Definition Ragdoll.h:209
size_t GetBodyCount() const
Get number of bodies in the ragdoll.
Definition Ragdoll.h:200
TwoBodyConstraint * GetConstraint(int inConstraintIndex)
Access a constraint by index.
Definition Ragdoll.h:212
const Array< BodyID > & GetBodyIDs() const
Access to the array of body IDs.
Definition Ragdoll.h:206
JPH_OVERRIDE_NEW_DELETE Ragdoll(PhysicsSystem *inSystem)
Constructor.
Definition Ragdoll.h:137
BodyID GetBodyID(int inBodyIndex) const
Access a body ID.
Definition Ragdoll.h:203
const RagdollSettings * GetRagdollSettings() const
Get the settings object that created this ragdoll.
Definition Ragdoll.h:221
A constraint that connects two bodies in a ragdoll (for non parent child related constraints)
Definition Ragdoll.h:98
AdditionalConstraint()=default
Constructors.
Ref< TwoBodyConstraintSettings > mConstraint
The constraint that connects these bodies.
Definition Ragdoll.h:107
AdditionalConstraint(int inBodyIdx1, int inBodyIdx2, TwoBodyConstraintSettings *inConstraint)
Definition Ragdoll.h:104
A single rigid body sub part of the ragdoll.
Definition Ragdoll.h:86
Ref< TwoBodyConstraintSettings > mToParent
Definition Ragdoll.h:90
Contains the structure of a ragdoll.
Definition Ragdoll.h:22
const Array< int > & GetBodyIndexToConstraintIndex() const
Definition Ragdoll.h:68
Skeleton * GetSkeleton()
Definition Ragdoll.h:61
int GetConstraintIndexForBodyIndex(int inBodyIndex) const
Map a single body index to a constraint index.
Definition Ragdoll.h:71
pair< int, int > BodyIdxPair
Definition Ragdoll.h:76
const Skeleton * GetSkeleton() const
Access to the skeleton of this ragdoll.
Definition Ragdoll.h:60
const Array< BodyIdxPair > & GetConstraintIndexToBodyIdxPair() const
Table that maps a constraint index (index in mConstraints) to the indices of the bodies that the cons...
Definition Ragdoll.h:79
AdditionalConstraintVector mAdditionalConstraints
A list of constraints that connects two bodies in a ragdoll (for non parent child related constraints...
Definition Ragdoll.h:120
BodyIdxPair GetBodyIndicesForConstraintIndex(int inConstraintIndex) const
Map a single constraint index (index in mConstraints) to the indices of the bodies that the constrain...
Definition Ragdoll.h:82
PartVector mParts
For each of the joints, the body and constraint attaching it to its parent body (1-on-1 with mSkeleto...
Definition Ragdoll.h:117
Ref< Skeleton > mSkeleton
The skeleton for this ragdoll.
Definition Ragdoll.h:114
Definition Reference.h:163
Definition Reference.h:107
Definition Reference.h:35
Helper class that either contains a valid result or an error.
Definition Result.h:12
Resource that contains the joint hierarchy for a skeleton.
Definition Skeleton.h:18
Instance of a skeleton, contains the pose the current skeleton is in.
Definition SkeletonPose.h:18
Simple binary input stream.
Definition StreamIn.h:13
Simple binary output stream.
Definition StreamOut.h:13
Base class for all constraints that involve 2 bodies. Body1 is usually considered the parent,...
Definition TwoBodyConstraint.h:27
Base class for settings for all constraints that involve 2 bodies.
Definition TwoBodyConstraint.h:16
Definition Vec3.h:17