Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/3d/chain_ik_3d.h
14709 views
1
/**************************************************************************/
2
/* chain_ik_3d.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "scene/3d/ik_modifier_3d.h"
34
35
class ChainIK3D : public IKModifier3D {
36
GDCLASS(ChainIK3D, IKModifier3D);
37
38
public:
39
struct ChainIK3DSetting : public IKModifier3DSetting {
40
#ifdef TOOLS_ENABLED
41
// Note:
42
// To cache global rest on global pose in SkeletonModifier process.
43
// Since gizmo drawing might be processed after SkeletonModifier process,
44
// so the gizmo which depend on modified pose is not drawn correctly.
45
// Especially, limitation sphere is needed this since it bound mutable bone axis which retrieve by bone pose to the parent bone rest.
46
Transform3D root_global_rest;
47
#endif // TOOLS_ENABLED
48
49
BoneJoint root_bone;
50
BoneJoint end_bone;
51
52
// To make virtual end joint.
53
bool extend_end_bone = false;
54
BoneDirection end_bone_direction = BONE_DIRECTION_FROM_PARENT;
55
float end_bone_length = 0.0;
56
57
LocalVector<BoneJoint> joints;
58
LocalVector<IKModifier3DSolverInfo *> solver_info_list;
59
LocalVector<Vector3> chain;
60
61
// Only update chain coordinates to avoid to override previous result (bone poses).
62
// Chain coordinates will be converted to bone pose by child class cache_current_joint_rotations() in the end of iterating.
63
void update_chain_coordinate(Skeleton3D *p_skeleton, int p_index, const Vector3 &p_position) {
64
// Don't update if the position is same as the current position ().
65
// But distance_squared_to() is unsuitable because converting position to rotation requires a certain level of precision.
66
if (Math::is_zero_approx(chain[p_index].distance_to(p_position))) {
67
return;
68
}
69
70
// Allow flipping.
71
chain[p_index] = p_position;
72
cache_current_vector(p_skeleton, p_index);
73
}
74
75
void update_chain_coordinate_bw(Skeleton3D *p_skeleton, int p_index, const Vector3 &p_position) {
76
// Don't update if the position is same as the current position.
77
// But distance_squared_to() is unsuitable because converting position to rotation requires a certain level of precision.
78
if (Math::is_zero_approx(chain[p_index].distance_to(p_position))) {
79
return;
80
}
81
82
// Prevent flipping from backwards.
83
Vector3 result = p_position;
84
int HEAD = p_index - 1;
85
int TAIL = p_index;
86
if (HEAD >= 0 && HEAD < (int)solver_info_list.size()) {
87
IKModifier3DSolverInfo *solver_info = solver_info_list[HEAD];
88
if (solver_info) {
89
Vector3 old_head_to_tail = solver_info->current_vector;
90
Vector3 new_head_to_tail = (result - chain[HEAD]).normalized();
91
if (Math::is_equal_approx((double)old_head_to_tail.dot(new_head_to_tail), -1.0)) {
92
chain[TAIL] = chain[HEAD] + old_head_to_tail * solver_info->length; // Revert.
93
return; // No change, cache is not updated.
94
}
95
}
96
}
97
98
chain[p_index] = result;
99
cache_current_vector(p_skeleton, p_index);
100
}
101
102
void update_chain_coordinate_fw(Skeleton3D *p_skeleton, int p_index, const Vector3 &p_position) {
103
// Don't update if the position is same as the current position.
104
// But distance_squared_to() is unsuitable because converting position to rotation requires a certain level of precision.
105
if (Math::is_zero_approx(chain[p_index].distance_to(p_position))) {
106
return;
107
}
108
109
// Prevent flipping from forwards.
110
Vector3 result = p_position;
111
int HEAD = p_index;
112
int TAIL = p_index + 1;
113
if (TAIL >= 0 && TAIL < (int)solver_info_list.size()) {
114
IKModifier3DSolverInfo *solver_info = solver_info_list[HEAD];
115
if (solver_info) {
116
Vector3 old_head_to_tail = solver_info->current_vector;
117
Vector3 new_head_to_tail = (chain[TAIL] - result).normalized();
118
if (Math::is_equal_approx((double)old_head_to_tail.dot(new_head_to_tail), -1.0)) {
119
chain[HEAD] = chain[TAIL] - old_head_to_tail * solver_info->length; // Revert.
120
return; // No change, cache is not updated.
121
}
122
}
123
}
124
125
chain[p_index] = result;
126
cache_current_vector(p_skeleton, p_index);
127
}
128
129
void cache_current_vector(Skeleton3D *p_skeleton, int p_index) {
130
int cur_head = p_index - 1;
131
int cur_tail = p_index;
132
if (cur_head >= 0) {
133
IKModifier3DSolverInfo *solver_info = solver_info_list[cur_head];
134
if (solver_info) {
135
solver_info->current_vector = (chain[cur_tail] - chain[cur_head]).normalized();
136
}
137
}
138
cur_head = p_index;
139
cur_tail = p_index + 1;
140
if (cur_tail < (int)chain.size()) {
141
IKModifier3DSolverInfo *solver_info = solver_info_list[cur_head];
142
if (solver_info) {
143
solver_info->current_vector = (chain[cur_tail] - chain[cur_head]).normalized();
144
}
145
}
146
}
147
148
void cache_current_vectors(Skeleton3D *p_skeleton) {
149
for (uint32_t i = 0; i < joints.size(); i++) {
150
int HEAD = i;
151
int TAIL = i + 1;
152
IKModifier3DSolverInfo *solver_info = solver_info_list[HEAD];
153
if (!solver_info) {
154
continue;
155
}
156
solver_info->current_vector = (chain[TAIL] - chain[HEAD]).normalized();
157
}
158
}
159
160
void init_current_joint_rotations(Skeleton3D *p_skeleton) {
161
if (root_bone.bone < 0) {
162
return;
163
}
164
165
Quaternion parent_gpose;
166
int parent = p_skeleton->get_bone_parent(root_bone.bone);
167
if (parent >= 0) {
168
parent_gpose = p_skeleton->get_bone_global_pose(parent).basis.get_rotation_quaternion();
169
}
170
171
for (uint32_t i = 0; i < joints.size(); i++) {
172
IKModifier3DSolverInfo *solver_info = solver_info_list[i];
173
if (!solver_info) {
174
continue;
175
}
176
solver_info->current_lrest = p_skeleton->get_bone_pose(joints[i].bone).basis.get_rotation_quaternion();
177
solver_info->current_grest = parent_gpose * solver_info->current_lrest;
178
solver_info->current_grest.normalize();
179
solver_info->current_lpose = p_skeleton->get_bone_pose(joints[i].bone).basis.get_rotation_quaternion();
180
solver_info->current_gpose = parent_gpose * solver_info->current_lpose;
181
solver_info->current_gpose.normalize();
182
parent_gpose = solver_info->current_gpose;
183
}
184
185
cache_current_vectors(p_skeleton);
186
}
187
188
~ChainIK3DSetting() {
189
for (uint32_t i = 0; i < solver_info_list.size(); i++) {
190
if (solver_info_list[i]) {
191
memdelete(solver_info_list[i]);
192
solver_info_list[i] = nullptr;
193
}
194
}
195
solver_info_list.clear();
196
}
197
};
198
199
protected:
200
#ifdef TOOLS_ENABLED
201
virtual void _update_mutable_info() override;
202
#endif // TOOLS_ENABLED
203
204
LocalVector<ChainIK3DSetting *> chain_settings; // For caching.
205
206
bool _get(const StringName &p_path, Variant &r_ret) const;
207
bool _set(const StringName &p_path, const Variant &p_value);
208
void get_property_list(List<PropertyInfo> *p_list) const;
209
void _validate_dynamic_prop(PropertyInfo &p_property) const;
210
211
static void _bind_methods();
212
213
virtual void _validate_bone_names() override;
214
void _validate_axes(Skeleton3D *p_skeleton) const;
215
virtual void _validate_axis(Skeleton3D *p_skeleton, int p_index, int p_joint) const;
216
217
virtual void _make_all_joints_dirty() override;
218
virtual void _update_joints(int p_index) override;
219
220
virtual void _process_ik(Skeleton3D *p_skeleton, double p_delta) override;
221
222
virtual void _set_joint_count(int p_index, int p_count);
223
224
public:
225
virtual void set_setting_count(int p_count) override {
226
_set_setting_count<ChainIK3DSetting>(p_count);
227
chain_settings = _cast_settings<ChainIK3DSetting>();
228
}
229
virtual void clear_settings() override {
230
_set_setting_count<ChainIK3DSetting>(0);
231
chain_settings.clear();
232
}
233
234
// Setting.
235
void set_root_bone_name(int p_index, const String &p_bone_name);
236
String get_root_bone_name(int p_index) const;
237
void set_root_bone(int p_index, int p_bone);
238
int get_root_bone(int p_index) const;
239
240
void set_end_bone_name(int p_index, const String &p_bone_name);
241
String get_end_bone_name(int p_index) const;
242
void set_end_bone(int p_index, int p_bone);
243
int get_end_bone(int p_index) const;
244
245
void set_extend_end_bone(int p_index, bool p_enabled);
246
bool is_end_bone_extended(int p_index) const;
247
void set_end_bone_direction(int p_index, BoneDirection p_bone_direction);
248
BoneDirection get_end_bone_direction(int p_index) const;
249
void set_end_bone_length(int p_index, float p_length);
250
float get_end_bone_length(int p_index) const;
251
252
// Individual joints.
253
void set_joint_bone_name(int p_index, int p_joint, const String &p_bone_name);
254
String get_joint_bone_name(int p_index, int p_joint) const;
255
void set_joint_bone(int p_index, int p_joint, int p_bone);
256
int get_joint_bone(int p_index, int p_joint) const;
257
258
void set_joint_count(int p_index, int p_count);
259
int get_joint_count(int p_index) const;
260
261
#ifdef TOOLS_ENABLED
262
// Helper.
263
static Transform3D get_bone_global_rest_mutable(Skeleton3D *p_skeleton, int p_bone);
264
Transform3D get_chain_root_global_rest(int p_index);
265
virtual Vector3 get_bone_vector(int p_index, int p_joint) const;
266
#endif // TOOLS_ENABLED
267
268
~ChainIK3D();
269
};
270
271