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 RagdollSettings : public RefTarget<RagdollSettings>
22{
23public:
25
26
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
54
57 Ragdoll * CreateRagdoll(CollisionGroup::GroupID inCollisionGroup, uint64 inUserData, PhysicsSystem *inSystem) const;
58
60 const Skeleton * GetSkeleton() const { return mSkeleton; }
62
65
67 const Array<int> & GetBodyIndexToConstraintIndex() const { return mBodyIndexToConstraintIndex; }
68
70 int GetConstraintIndexForBodyIndex(int inBodyIndex) const { return mBodyIndexToConstraintIndex[inBodyIndex]; }
71
74
75 using BodyIdxPair = pair<int, int>;
76
78 const Array<BodyIdxPair> & GetConstraintIndexToBodyIdxPair() const { return mConstraintIndexToBodyIdxPair; }
79
81 BodyIdxPair GetBodyIndicesForConstraintIndex(int inConstraintIndex) const { return mConstraintIndexToBodyIdxPair[inConstraintIndex]; }
82
85 {
86 public:
88
90 };
91
94
97
100
101private:
103 Array<int> mBodyIndexToConstraintIndex;
104
106 Array<BodyIdxPair> mConstraintIndexToBodyIdxPair;
107};
108
110class Ragdoll : public RefTarget<Ragdoll>, public NonCopyable
111{
112public:
114
116 explicit Ragdoll(PhysicsSystem *inSystem) : mSystem(inSystem) { }
117
119 ~Ragdoll();
120
122 void AddToPhysicsSystem(EActivation inActivationMode, bool inLockBodies = true);
123
125 void RemoveFromPhysicsSystem(bool inLockBodies = true);
126
128 void Activate(bool inLockBodies = true);
129
131 void SetGroupID(CollisionGroup::GroupID inGroupID, bool inLockBodies = true);
132
134 void SetPose(const SkeletonPose &inPose, bool inLockBodies = true);
135
137 void SetPose(RVec3Arg inRootOffset, const Mat44 *inJointMatrices, bool inLockBodies = true);
138
140 void GetPose(SkeletonPose &outPose, bool inLockBodies = true);
141
143 void GetPose(RVec3 &outRootOffset, Mat44 *outJointMatrices, bool inLockBodies = true);
144
146 void DriveToPoseUsingKinematics(const SkeletonPose &inPose, float inDeltaTime, bool inLockBodies = true);
147
149 void DriveToPoseUsingKinematics(RVec3Arg inRootOffset, const Mat44 *inJointMatrices, float inDeltaTime, bool inLockBodies = true);
150
152 void DriveToPoseUsingMotors(const SkeletonPose &inPose);
153
155 void SetLinearAndAngularVelocity(Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity, bool inLockBodies = true);
156
158 void SetLinearVelocity(Vec3Arg inLinearVelocity, bool inLockBodies = true);
159
161 void AddLinearVelocity(Vec3Arg inLinearVelocity, bool inLockBodies = true);
162
164 void AddImpulse(Vec3Arg inImpulse, bool inLockBodies = true);
165
167 void GetRootTransform(RVec3 &outPosition, Quat &outRotation, bool inLockBodies = true) const;
168
170 size_t GetBodyCount() const { return mBodyIDs.size(); }
171
173 BodyID GetBodyID(int inBodyIndex) const { return mBodyIDs[inBodyIndex]; }
174
176 const Array<BodyID> & GetBodyIDs() const { return mBodyIDs; }
177
179 size_t GetConstraintCount() const { return mConstraints.size(); }
180
182 TwoBodyConstraint * GetConstraint(int inConstraintIndex) { return mConstraints[inConstraintIndex]; }
183
185 const TwoBodyConstraint * GetConstraint(int inConstraintIndex) const { return mConstraints[inConstraintIndex]; }
186
188 AABox GetWorldSpaceBounds(bool inLockBodies = true) const;
189
191 const RagdollSettings * GetRagdollSettings() const { return mRagdollSettings; }
192
193private:
195 friend class RagdollSettings;
196
198 RefConst<RagdollSettings> mRagdollSettings;
199
201 Array<BodyID> mBodyIDs;
202
204 Array<Ref<TwoBodyConstraint>> mConstraints;
205
207 PhysicsSystem * mSystem;
208};
209
#define JPH_NAMESPACE_END
Definition: Core.h:240
uint64_t uint64
Definition: Core.h:313
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:234
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:29
std::vector< T, STLAllocator< T > > Array
Definition: STLAllocator.h:81
#define JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(class_name)
Definition: SerializableObject.h:71
Axis aligned box.
Definition: AABox.h:16
Settings for constructing a rigid body.
Definition: BodyCreationSettings.h:29
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:28
Definition: Quat.h:33
Runtime ragdoll information.
Definition: Ragdoll.h:111
const TwoBodyConstraint * GetConstraint(int inConstraintIndex) const
Access a constraint by index.
Definition: Ragdoll.h:185
size_t GetConstraintCount() const
Get number of constraints in the ragdoll.
Definition: Ragdoll.h:179
size_t GetBodyCount() const
Get number of bodies in the ragdoll.
Definition: Ragdoll.h:170
TwoBodyConstraint * GetConstraint(int inConstraintIndex)
Access a constraint by index.
Definition: Ragdoll.h:182
const Array< BodyID > & GetBodyIDs() const
Access to the array of body IDs.
Definition: Ragdoll.h:176
JPH_OVERRIDE_NEW_DELETE Ragdoll(PhysicsSystem *inSystem)
Constructor.
Definition: Ragdoll.h:116
BodyID GetBodyID(int inBodyIndex) const
Access a body ID.
Definition: Ragdoll.h:173
const RagdollSettings * GetRagdollSettings() const
Get the settings object that created this ragdoll.
Definition: Ragdoll.h:191
A single rigid body sub part of the ragdoll.
Definition: Ragdoll.h:85
Ref< TwoBodyConstraintSettings > mToParent
Definition: Ragdoll.h:89
Contains the structure of a ragdoll.
Definition: Ragdoll.h:22
const Array< int > & GetBodyIndexToConstraintIndex() const
Get table that maps a body index to the constraint index with which it is connected to its parent....
Definition: Ragdoll.h:67
Skeleton * GetSkeleton()
Definition: Ragdoll.h:61
int GetConstraintIndexForBodyIndex(int inBodyIndex) const
Map a single body index to a constraint index.
Definition: Ragdoll.h:70
void CalculateBodyIndexToConstraintIndex()
Calculate the map needed for GetBodyIndexToConstraintIndex()
Definition: Ragdoll.cpp:379
static RagdollResult sRestoreFromBinaryState(StreamIn &inStream)
Restore a saved ragdoll from inStream.
Definition: Ragdoll.cpp:275
pair< int, int > BodyIdxPair
Definition: Ragdoll.h:75
void DisableParentChildCollisions(const Mat44 *inJointMatrices=nullptr, float inMinSeparationDistance=0.0f)
Definition: Ragdoll.cpp:185
void CalculateConstraintIndexToBodyIdxPair()
Calculate the map needed for GetConstraintIndexToBodyIdxPair()
Definition: Ragdoll.cpp:394
void SaveBinaryState(StreamOut &inStream, bool inSaveShapes, bool inSaveGroupFilter) const
Definition: Ragdoll.cpp:252
const Skeleton * GetSkeleton() const
Access to the skeleton of this ragdoll.
Definition: Ragdoll.h:60
Ragdoll * CreateRagdoll(CollisionGroup::GroupID inCollisionGroup, uint64 inUserData, PhysicsSystem *inSystem) const
Definition: Ragdoll.cpp:338
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:78
bool Stabilize()
Definition: Ragdoll.cpp:44
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:81
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:99
Ref< Skeleton > mSkeleton
The skeleton for this ragdoll.
Definition: Ragdoll.h:96
Array< Part > PartVector
The constraint that connects this part to its parent part (should be null for the root)
Definition: Ragdoll.h:93
Definition: Reference.h:154
Definition: Reference.h:101
Definition: Reference.h:35
Helper class that either contains a valid result or an error.
Definition: Result.h:15
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:11
Simple binary output stream.
Definition: StreamOut.h:11
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:16