Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Skeleton/SkeletonMapper.h
9912 views
1
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2
// SPDX-FileCopyrightText: 2022 Jorrit Rouwe
3
// SPDX-License-Identifier: MIT
4
5
#pragma once
6
7
#include <Jolt/Core/Reference.h>
8
#include <Jolt/Skeleton/Skeleton.h>
9
10
JPH_NAMESPACE_BEGIN
11
12
/// Class that is able to map a low detail (ragdoll) skeleton to a high detail (animation) skeleton and vice versa
13
class JPH_EXPORT SkeletonMapper : public RefTarget<SkeletonMapper>
14
{
15
public:
16
/// A joint that maps 1-on-1 to a joint in the other skeleton
17
class Mapping
18
{
19
public:
20
Mapping() = default;
21
Mapping(int inJointIdx1, int inJointIdx2, Mat44Arg inJoint1To2) : mJointIdx1(inJointIdx1), mJointIdx2(inJointIdx2), mJoint1To2(inJoint1To2), mJoint2To1(inJoint1To2.Inversed())
22
{
23
// Ensure bottom right element is 1 (numerical imprecision in the inverse can make this not so)
24
mJoint2To1(3, 3) = 1.0f;
25
}
26
27
int mJointIdx1; ///< Index of joint from skeleton 1
28
int mJointIdx2; ///< Corresponding index of joint from skeleton 2
29
Mat44 mJoint1To2; ///< Transforms this joint from skeleton 1 to 2
30
Mat44 mJoint2To1; ///< Inverse of the transform above
31
};
32
33
/// A joint chain that starts with a 1-on-1 mapped joint and ends with a 1-on-1 mapped joint with intermediate joints that cannot be mapped
34
class Chain
35
{
36
public:
37
Chain() = default;
38
Chain(Array<int> &&inJointIndices1, Array<int> &&inJointIndices2) : mJointIndices1(std::move(inJointIndices1)), mJointIndices2(std::move(inJointIndices2)) { }
39
40
Array<int> mJointIndices1; ///< Joint chain from skeleton 1
41
Array<int> mJointIndices2; ///< Corresponding joint chain from skeleton 2
42
};
43
44
/// Joints that could not be mapped from skeleton 1 to 2
45
class Unmapped
46
{
47
public:
48
Unmapped() = default;
49
Unmapped(int inJointIdx, int inParentJointIdx) : mJointIdx(inJointIdx), mParentJointIdx(inParentJointIdx) { }
50
51
int mJointIdx; ///< Joint index of unmappable joint
52
int mParentJointIdx; ///< Parent joint index of unmappable joint
53
};
54
55
/// Joints that should have their translation locked (fixed)
56
class Locked
57
{
58
public:
59
int mJointIdx; ///< Joint index of joint with locked translation (in skeleton 2)
60
int mParentJointIdx; ///< Parent joint index of joint with locked translation (in skeleton 2)
61
Vec3 mTranslation; ///< Translation of neutral pose
62
};
63
64
/// A function that is called to determine if a joint can be mapped from source to target skeleton
65
using CanMapJoint = function<bool (const Skeleton *, int, const Skeleton *, int)>;
66
67
/// Default function that checks if the names of the joints are equal
68
static bool sDefaultCanMapJoint(const Skeleton *inSkeleton1, int inIndex1, const Skeleton *inSkeleton2, int inIndex2)
69
{
70
return inSkeleton1->GetJoint(inIndex1).mName == inSkeleton2->GetJoint(inIndex2).mName;
71
}
72
73
/// Initialize the skeleton mapper. Skeleton 1 should be the (low detail) ragdoll skeleton and skeleton 2 the (high detail) animation skeleton.
74
/// We assume that each joint in skeleton 1 can be mapped to a joint in skeleton 2 (if not mapping from animation skeleton to ragdoll skeleton will be undefined).
75
/// Skeleton 2 should have the same hierarchy as skeleton 1 but can contain extra joints between those in skeleton 1 and it can have extra joints at the root and leaves of the skeleton.
76
/// @param inSkeleton1 Source skeleton to map from.
77
/// @param inNeutralPose1 Neutral pose of the source skeleton (model space)
78
/// @param inSkeleton2 Target skeleton to map to.
79
/// @param inNeutralPose2 Neutral pose of the target skeleton (model space), inNeutralPose1 and inNeutralPose2 must match as closely as possible, preferably the position of the mappable joints should be identical.
80
/// @param inCanMapJoint Function that checks if joints in skeleton 1 and skeleton 2 are equal.
81
void Initialize(const Skeleton *inSkeleton1, const Mat44 *inNeutralPose1, const Skeleton *inSkeleton2, const Mat44 *inNeutralPose2, const CanMapJoint &inCanMapJoint = sDefaultCanMapJoint);
82
83
/// This can be called so lock the translation of a specified set of joints in skeleton 2.
84
/// Because constraints are never 100% rigid, there's always a little bit of stretch in the ragdoll when the ragdoll is under stress.
85
/// Locking the translations of the pose will remove the visual stretch from the ragdoll but will introduce a difference between the
86
/// physical simulation and the visual representation.
87
/// @param inSkeleton2 Target skeleton to map to.
88
/// @param inLockedTranslations An array of bools the size of inSkeleton2->GetJointCount(), for each joint indicating if the joint is locked.
89
/// @param inNeutralPose2 Neutral pose to take reference translations from
90
void LockTranslations(const Skeleton *inSkeleton2, const bool *inLockedTranslations, const Mat44 *inNeutralPose2);
91
92
/// After Initialize(), this can be called to lock the translation of all joints in skeleton 2 below the first mapped joint to those of the neutral pose.
93
/// Because constraints are never 100% rigid, there's always a little bit of stretch in the ragdoll when the ragdoll is under stress.
94
/// Locking the translations of the pose will remove the visual stretch from the ragdoll but will introduce a difference between the
95
/// physical simulation and the visual representation.
96
/// @param inSkeleton2 Target skeleton to map to.
97
/// @param inNeutralPose2 Neutral pose to take reference translations from
98
void LockAllTranslations(const Skeleton *inSkeleton2, const Mat44 *inNeutralPose2);
99
100
/// Map a pose. Joints that were directly mappable will be copied in model space from pose 1 to pose 2. Any joints that are only present in skeleton 2
101
/// will get their model space transform calculated through the local space transforms of pose 2. Joints that are part of a joint chain between two
102
/// mapped joints will be reoriented towards the next joint in skeleton 1. This means that it is possible for unmapped joints to have some animation,
103
/// but very extreme animation poses will show artifacts.
104
/// @param inPose1ModelSpace Pose on skeleton 1 in model space
105
/// @param inPose2LocalSpace Pose on skeleton 2 in local space (used for the joints that cannot be mapped)
106
/// @param outPose2ModelSpace Model space pose on skeleton 2 (the output of the mapping)
107
void Map(const Mat44 *inPose1ModelSpace, const Mat44 *inPose2LocalSpace, Mat44 *outPose2ModelSpace) const;
108
109
/// Reverse map a pose, this will only use the mappings and not the chains (it assumes that all joints in skeleton 1 are mapped)
110
/// @param inPose2ModelSpace Model space pose on skeleton 2
111
/// @param outPose1ModelSpace When the function returns this will contain the model space pose for skeleton 1
112
void MapReverse(const Mat44 *inPose2ModelSpace, Mat44 *outPose1ModelSpace) const;
113
114
/// Search through the directly mapped joints (mMappings) and find inJoint1Idx, returns the corresponding Joint2Idx or -1 if not found.
115
int GetMappedJointIdx(int inJoint1Idx) const;
116
117
/// Search through the locked translations (mLockedTranslations) and find if joint inJoint2Idx is locked.
118
bool IsJointTranslationLocked(int inJoint2Idx) const;
119
120
using MappingVector = Array<Mapping>;
121
using ChainVector = Array<Chain>;
122
using UnmappedVector = Array<Unmapped>;
123
using LockedVector = Array<Locked>;
124
125
///@name Access to the mapped joints
126
///@{
127
const MappingVector & GetMappings() const { return mMappings; }
128
MappingVector & GetMappings() { return mMappings; }
129
const ChainVector & GetChains() const { return mChains; }
130
ChainVector & GetChains() { return mChains; }
131
const UnmappedVector & GetUnmapped() const { return mUnmapped; }
132
UnmappedVector & GetUnmapped() { return mUnmapped; }
133
const LockedVector & GetLockedTranslations() const { return mLockedTranslations; }
134
LockedVector & GetLockedTranslations() { return mLockedTranslations; }
135
///@}
136
137
private:
138
/// Joint mappings
139
MappingVector mMappings;
140
ChainVector mChains;
141
UnmappedVector mUnmapped; ///< Joint indices that could not be mapped from 1 to 2 (these are indices in 2)
142
LockedVector mLockedTranslations;
143
};
144
145
JPH_NAMESPACE_END
146
147