Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/3d/chain_ik_3d.cpp
20938 views
1
/**************************************************************************/
2
/* chain_ik_3d.cpp */
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
#include "chain_ik_3d.h"
32
33
bool ChainIK3D::_set(const StringName &p_path, const Variant &p_value) {
34
String path = p_path;
35
36
if (path.begins_with("settings/")) {
37
int which = path.get_slicec('/', 1).to_int();
38
String what = path.get_slicec('/', 2);
39
ERR_FAIL_INDEX_V(which, (int)settings.size(), false);
40
41
if (what == "root_bone_name") {
42
set_root_bone_name(which, p_value);
43
} else if (what == "root_bone") {
44
set_root_bone(which, p_value);
45
} else if (what == "end_bone_name") {
46
set_end_bone_name(which, p_value);
47
} else if (what == "end_bone") {
48
String opt = path.get_slicec('/', 3);
49
if (opt.is_empty()) {
50
set_end_bone(which, p_value);
51
} else if (opt == "direction") {
52
set_end_bone_direction(which, static_cast<BoneDirection>((int)p_value));
53
} else if (opt == "length") {
54
set_end_bone_length(which, p_value);
55
} else {
56
return false;
57
}
58
} else if (what == "extend_end_bone") {
59
set_extend_end_bone(which, p_value);
60
} else if (what == "joint_count") {
61
set_joint_count(which, p_value);
62
} else {
63
return false;
64
}
65
}
66
return true;
67
}
68
69
bool ChainIK3D::_get(const StringName &p_path, Variant &r_ret) const {
70
String path = p_path;
71
72
if (path.begins_with("settings/")) {
73
int which = path.get_slicec('/', 1).to_int();
74
String what = path.get_slicec('/', 2);
75
ERR_FAIL_INDEX_V(which, (int)settings.size(), false);
76
77
if (what == "root_bone_name") {
78
r_ret = get_root_bone_name(which);
79
} else if (what == "root_bone") {
80
r_ret = get_root_bone(which);
81
} else if (what == "end_bone_name") {
82
r_ret = get_end_bone_name(which);
83
} else if (what == "end_bone") {
84
String opt = path.get_slicec('/', 3);
85
if (opt.is_empty()) {
86
r_ret = get_end_bone(which);
87
} else if (opt == "direction") {
88
r_ret = (int)get_end_bone_direction(which);
89
} else if (opt == "length") {
90
r_ret = get_end_bone_length(which);
91
} else {
92
return false;
93
}
94
} else if (what == "extend_end_bone") {
95
r_ret = is_end_bone_extended(which);
96
} else if (what == "joint_count") {
97
r_ret = get_joint_count(which);
98
} else if (what == "joints") {
99
int idx = path.get_slicec('/', 3).to_int();
100
String prop = path.get_slicec('/', 4);
101
if (prop == "bone_name") {
102
r_ret = get_joint_bone_name(which, idx);
103
} else if (prop == "bone") {
104
r_ret = get_joint_bone(which, idx);
105
} else {
106
return false;
107
}
108
} else {
109
return false;
110
}
111
}
112
return true;
113
}
114
115
void ChainIK3D::get_property_list(List<PropertyInfo> *p_list) const {
116
String enum_hint;
117
Skeleton3D *skeleton = get_skeleton();
118
if (skeleton) {
119
enum_hint = skeleton->get_concatenated_bone_names();
120
}
121
122
LocalVector<PropertyInfo> props;
123
124
for (uint32_t i = 0; i < settings.size(); i++) {
125
String path = "settings/" + itos(i) + "/";
126
props.push_back(PropertyInfo(Variant::STRING, path + "root_bone_name", PROPERTY_HINT_ENUM_SUGGESTION, enum_hint));
127
props.push_back(PropertyInfo(Variant::INT, path + "root_bone", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
128
props.push_back(PropertyInfo(Variant::STRING, path + "end_bone_name", PROPERTY_HINT_ENUM_SUGGESTION, enum_hint));
129
props.push_back(PropertyInfo(Variant::INT, path + "end_bone", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
130
props.push_back(PropertyInfo(Variant::BOOL, path + "extend_end_bone"));
131
props.push_back(PropertyInfo(Variant::INT, path + "end_bone/direction", PROPERTY_HINT_ENUM, SkeletonModifier3D::get_hint_bone_direction()));
132
props.push_back(PropertyInfo(Variant::FLOAT, path + "end_bone/length", PROPERTY_HINT_RANGE, "0,1,0.001,or_greater,suffix:m"));
133
props.push_back(PropertyInfo(Variant::INT, path + "joint_count", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_ARRAY, "Joints," + path + "joints/,static,const"));
134
for (uint32_t j = 0; j < chain_settings[i]->joints.size(); j++) {
135
String joint_path = path + "joints/" + itos(j) + "/";
136
props.push_back(PropertyInfo(Variant::STRING, joint_path + "bone_name", PROPERTY_HINT_ENUM_SUGGESTION, enum_hint, PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_READ_ONLY));
137
props.push_back(PropertyInfo(Variant::INT, joint_path + "bone", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_READ_ONLY));
138
}
139
}
140
141
for (PropertyInfo &p : props) {
142
_validate_dynamic_prop(p);
143
p_list->push_back(p);
144
}
145
}
146
147
void ChainIK3D::_validate_dynamic_prop(PropertyInfo &p_property) const {
148
PackedStringArray split = p_property.name.split("/");
149
if (split.size() > 2 && split[0] == "settings") {
150
int which = split[1].to_int();
151
152
// Extended end bone option.
153
bool force_hide = false;
154
if (split[2] == "extend_end_bone" && get_end_bone(which) == -1) {
155
p_property.usage = PROPERTY_USAGE_NONE;
156
force_hide = true;
157
}
158
if (force_hide || (split[2] == "end_bone" && !is_end_bone_extended(which) && split.size() > 3)) {
159
p_property.usage = PROPERTY_USAGE_NONE;
160
}
161
}
162
}
163
164
// Setting.
165
166
void ChainIK3D::set_root_bone_name(int p_index, const String &p_bone_name) {
167
ERR_FAIL_INDEX(p_index, (int)settings.size());
168
chain_settings[p_index]->root_bone.name = p_bone_name;
169
Skeleton3D *sk = get_skeleton();
170
if (sk) {
171
set_root_bone(p_index, sk->find_bone(chain_settings[p_index]->root_bone.name));
172
}
173
}
174
175
String ChainIK3D::get_root_bone_name(int p_index) const {
176
ERR_FAIL_INDEX_V(p_index, (int)settings.size(), String());
177
return chain_settings[p_index]->root_bone.name;
178
}
179
180
void ChainIK3D::set_root_bone(int p_index, int p_bone) {
181
ERR_FAIL_INDEX(p_index, (int)settings.size());
182
bool changed = chain_settings[p_index]->root_bone.bone != p_bone;
183
chain_settings[p_index]->root_bone.bone = p_bone;
184
Skeleton3D *sk = get_skeleton();
185
if (sk) {
186
if (chain_settings[p_index]->root_bone.bone <= -1 || chain_settings[p_index]->root_bone.bone >= sk->get_bone_count()) {
187
WARN_PRINT("Root bone index out of range!");
188
chain_settings[p_index]->root_bone.bone = -1;
189
} else {
190
chain_settings[p_index]->root_bone.name = sk->get_bone_name(chain_settings[p_index]->root_bone.bone);
191
}
192
}
193
if (changed) {
194
_update_joints(p_index);
195
}
196
}
197
198
int ChainIK3D::get_root_bone(int p_index) const {
199
ERR_FAIL_INDEX_V(p_index, (int)settings.size(), -1);
200
return chain_settings[p_index]->root_bone.bone;
201
}
202
203
void ChainIK3D::set_end_bone_name(int p_index, const String &p_bone_name) {
204
ERR_FAIL_INDEX(p_index, (int)settings.size());
205
chain_settings[p_index]->end_bone.name = p_bone_name;
206
Skeleton3D *sk = get_skeleton();
207
if (sk) {
208
set_end_bone(p_index, sk->find_bone(chain_settings[p_index]->end_bone.name));
209
}
210
}
211
212
String ChainIK3D::get_end_bone_name(int p_index) const {
213
ERR_FAIL_INDEX_V(p_index, (int)settings.size(), String());
214
return chain_settings[p_index]->end_bone.name;
215
}
216
217
void ChainIK3D::set_end_bone(int p_index, int p_bone) {
218
ERR_FAIL_INDEX(p_index, (int)settings.size());
219
bool changed = chain_settings[p_index]->end_bone.bone != p_bone;
220
chain_settings[p_index]->end_bone.bone = p_bone;
221
Skeleton3D *sk = get_skeleton();
222
if (sk) {
223
if (chain_settings[p_index]->end_bone.bone <= -1 || chain_settings[p_index]->end_bone.bone >= sk->get_bone_count()) {
224
WARN_PRINT("End bone index out of range!");
225
chain_settings[p_index]->end_bone.bone = -1;
226
} else {
227
chain_settings[p_index]->end_bone.name = sk->get_bone_name(chain_settings[p_index]->end_bone.bone);
228
}
229
}
230
if (changed) {
231
_update_joints(p_index);
232
}
233
notify_property_list_changed();
234
}
235
236
int ChainIK3D::get_end_bone(int p_index) const {
237
ERR_FAIL_INDEX_V(p_index, (int)settings.size(), -1);
238
return chain_settings[p_index]->end_bone.bone;
239
}
240
241
void ChainIK3D::set_extend_end_bone(int p_index, bool p_enabled) {
242
ERR_FAIL_INDEX(p_index, (int)settings.size());
243
chain_settings[p_index]->extend_end_bone = p_enabled;
244
_make_simulation_dirty(p_index);
245
Skeleton3D *sk = get_skeleton();
246
if (sk && !chain_settings[p_index]->joints.is_empty()) {
247
_validate_axis(sk, p_index, chain_settings[p_index]->joints.size() - 1);
248
}
249
notify_property_list_changed();
250
#ifdef TOOLS_ENABLED
251
_make_gizmo_dirty();
252
#endif // TOOLS_ENABLED
253
}
254
255
bool ChainIK3D::is_end_bone_extended(int p_index) const {
256
ERR_FAIL_INDEX_V(p_index, (int)settings.size(), false);
257
return chain_settings[p_index]->extend_end_bone;
258
}
259
260
void ChainIK3D::set_end_bone_direction(int p_index, BoneDirection p_bone_direction) {
261
ERR_FAIL_INDEX(p_index, (int)settings.size());
262
chain_settings[p_index]->end_bone_direction = p_bone_direction;
263
Skeleton3D *sk = get_skeleton();
264
if (sk && !chain_settings[p_index]->joints.is_empty()) {
265
_validate_axis(sk, p_index, chain_settings[p_index]->joints.size() - 1);
266
}
267
#ifdef TOOLS_ENABLED
268
_make_gizmo_dirty();
269
#endif // TOOLS_ENABLED
270
if (mutable_bone_axes) {
271
return; // Chain dir will be recaluclated in _update_bone_axis().
272
}
273
_make_simulation_dirty(p_index);
274
}
275
276
SkeletonModifier3D::BoneDirection ChainIK3D::get_end_bone_direction(int p_index) const {
277
ERR_FAIL_INDEX_V(p_index, (int)settings.size(), BONE_DIRECTION_FROM_PARENT);
278
return chain_settings[p_index]->end_bone_direction;
279
}
280
281
void ChainIK3D::set_end_bone_length(int p_index, float p_length) {
282
ERR_FAIL_INDEX(p_index, (int)settings.size());
283
float old = chain_settings[p_index]->end_bone_length;
284
chain_settings[p_index]->end_bone_length = p_length;
285
#ifdef TOOLS_ENABLED
286
_make_gizmo_dirty();
287
#endif // TOOLS_ENABLED
288
if (mutable_bone_axes && Math::is_zero_approx(old) == Math::is_zero_approx(p_length)) {
289
return; // If chain size is not changed, length will be recaluclated in _update_bone_axis().
290
}
291
_make_simulation_dirty(p_index);
292
}
293
294
float ChainIK3D::get_end_bone_length(int p_index) const {
295
ERR_FAIL_INDEX_V(p_index, (int)settings.size(), 0);
296
return chain_settings[p_index]->end_bone_length;
297
}
298
299
// Individual joints.
300
301
String ChainIK3D::get_joint_bone_name(int p_index, int p_joint) const {
302
ERR_FAIL_INDEX_V(p_index, (int)settings.size(), String());
303
const LocalVector<BoneJoint> &joints = chain_settings[p_index]->joints;
304
ERR_FAIL_INDEX_V(p_joint, (int)joints.size(), String());
305
return joints[p_joint].name;
306
}
307
308
void ChainIK3D::_set_joint_bone(int p_index, int p_joint, int p_bone) {
309
ERR_FAIL_INDEX(p_index, (int)settings.size());
310
LocalVector<BoneJoint> &joints = chain_settings[p_index]->joints;
311
ERR_FAIL_INDEX(p_joint, (int)joints.size());
312
joints[p_joint].bone = p_bone;
313
Skeleton3D *sk = get_skeleton();
314
if (sk) {
315
if (joints[p_joint].bone <= -1 || joints[p_joint].bone >= sk->get_bone_count()) {
316
WARN_PRINT("Joint bone index out of range!");
317
joints[p_joint].bone = -1;
318
} else {
319
joints[p_joint].name = sk->get_bone_name(joints[p_joint].bone);
320
}
321
}
322
}
323
324
int ChainIK3D::get_joint_bone(int p_index, int p_joint) const {
325
ERR_FAIL_INDEX_V(p_index, (int)settings.size(), -1);
326
const LocalVector<BoneJoint> &joints = chain_settings[p_index]->joints;
327
ERR_FAIL_INDEX_V(p_joint, (int)joints.size(), -1);
328
return joints[p_joint].bone;
329
}
330
331
void ChainIK3D::set_joint_count(int p_index, int p_count) {
332
ERR_FAIL_INDEX(p_index, (int)settings.size());
333
ERR_FAIL_COND(p_count < 0);
334
LocalVector<BoneJoint> &joints = chain_settings[p_index]->joints;
335
joints.resize(p_count);
336
_set_joint_count(p_index, p_count);
337
notify_property_list_changed();
338
}
339
340
void ChainIK3D::_set_joint_count(int p_index, int p_count) {
341
//
342
}
343
344
int ChainIK3D::get_joint_count(int p_index) const {
345
ERR_FAIL_INDEX_V(p_index, (int)settings.size(), 0);
346
const LocalVector<BoneJoint> &joints = chain_settings[p_index]->joints;
347
return joints.size();
348
}
349
350
void ChainIK3D::_bind_methods() {
351
// Setting.
352
ClassDB::bind_method(D_METHOD("set_root_bone_name", "index", "bone_name"), &ChainIK3D::set_root_bone_name);
353
ClassDB::bind_method(D_METHOD("get_root_bone_name", "index"), &ChainIK3D::get_root_bone_name);
354
ClassDB::bind_method(D_METHOD("set_root_bone", "index", "bone"), &ChainIK3D::set_root_bone);
355
ClassDB::bind_method(D_METHOD("get_root_bone", "index"), &ChainIK3D::get_root_bone);
356
357
ClassDB::bind_method(D_METHOD("set_end_bone_name", "index", "bone_name"), &ChainIK3D::set_end_bone_name);
358
ClassDB::bind_method(D_METHOD("get_end_bone_name", "index"), &ChainIK3D::get_end_bone_name);
359
ClassDB::bind_method(D_METHOD("set_end_bone", "index", "bone"), &ChainIK3D::set_end_bone);
360
ClassDB::bind_method(D_METHOD("get_end_bone", "index"), &ChainIK3D::get_end_bone);
361
362
ClassDB::bind_method(D_METHOD("set_extend_end_bone", "index", "enabled"), &ChainIK3D::set_extend_end_bone);
363
ClassDB::bind_method(D_METHOD("is_end_bone_extended", "index"), &ChainIK3D::is_end_bone_extended);
364
ClassDB::bind_method(D_METHOD("set_end_bone_direction", "index", "bone_direction"), &ChainIK3D::set_end_bone_direction);
365
ClassDB::bind_method(D_METHOD("get_end_bone_direction", "index"), &ChainIK3D::get_end_bone_direction);
366
ClassDB::bind_method(D_METHOD("set_end_bone_length", "index", "length"), &ChainIK3D::set_end_bone_length);
367
ClassDB::bind_method(D_METHOD("get_end_bone_length", "index"), &ChainIK3D::get_end_bone_length);
368
369
// Individual joints.
370
ClassDB::bind_method(D_METHOD("get_joint_bone_name", "index", "joint"), &ChainIK3D::get_joint_bone_name);
371
ClassDB::bind_method(D_METHOD("get_joint_bone", "index", "joint"), &ChainIK3D::get_joint_bone);
372
373
ClassDB::bind_method(D_METHOD("get_joint_count", "index"), &ChainIK3D::get_joint_count);
374
}
375
376
void ChainIK3D::_validate_bone_names() {
377
for (uint32_t i = 0; i < settings.size(); i++) {
378
// Prior bone name.
379
if (!chain_settings[i]->root_bone.name.is_empty()) {
380
set_root_bone_name(i, chain_settings[i]->root_bone.name);
381
} else if (chain_settings[i]->root_bone.bone != -1) {
382
set_root_bone(i, chain_settings[i]->root_bone.bone);
383
}
384
// Prior bone name.
385
if (!chain_settings[i]->end_bone.name.is_empty()) {
386
set_end_bone_name(i, chain_settings[i]->end_bone.name);
387
} else if (chain_settings[i]->end_bone.bone != -1) {
388
set_end_bone(i, chain_settings[i]->end_bone.bone);
389
}
390
}
391
}
392
393
void ChainIK3D::_validate_axes(Skeleton3D *p_skeleton) const {
394
for (uint32_t i = 0; i < settings.size(); i++) {
395
for (uint32_t j = 0; j < chain_settings[i]->joints.size(); j++) {
396
_validate_axis(p_skeleton, i, j);
397
}
398
}
399
}
400
401
void ChainIK3D::_validate_axis(Skeleton3D *p_skeleton, int p_index, int p_joint) const {
402
//
403
}
404
405
void ChainIK3D::_make_all_joints_dirty() {
406
for (uint32_t i = 0; i < settings.size(); i++) {
407
_update_joints(i);
408
}
409
}
410
411
void ChainIK3D::_update_joints(int p_index) {
412
_make_simulation_dirty(p_index);
413
414
#ifdef TOOLS_ENABLED
415
update_gizmos(); // To clear invalid setting.
416
#endif // TOOLS_ENABLED
417
418
Skeleton3D *sk = get_skeleton();
419
int current_bone = chain_settings[p_index]->end_bone.bone;
420
int root_bone = chain_settings[p_index]->root_bone.bone;
421
if (!sk || current_bone < 0 || root_bone < 0) {
422
set_joint_count(p_index, 0);
423
return;
424
}
425
426
// Validation.
427
bool valid = false;
428
while (current_bone >= 0) {
429
if (current_bone == root_bone) {
430
valid = true;
431
break;
432
}
433
current_bone = sk->get_bone_parent(current_bone);
434
}
435
436
if (!valid) {
437
set_joint_count(p_index, 0);
438
ERR_FAIL_EDMSG("End bone must be the same as or a child of the root bone.");
439
}
440
441
Vector<int> new_joints;
442
current_bone = chain_settings[p_index]->end_bone.bone;
443
while (current_bone != root_bone) {
444
new_joints.push_back(current_bone);
445
current_bone = sk->get_bone_parent(current_bone);
446
}
447
new_joints.push_back(current_bone);
448
new_joints.reverse();
449
450
set_joint_count(p_index, new_joints.size());
451
for (uint32_t i = 0; i < new_joints.size(); i++) {
452
_set_joint_bone(p_index, i, new_joints[i]);
453
}
454
455
if (sk) {
456
_validate_axes(sk);
457
}
458
459
#ifdef TOOLS_ENABLED
460
_make_gizmo_dirty();
461
#endif // TOOLS_ENABLED
462
}
463
464
void ChainIK3D::_process_ik(Skeleton3D *p_skeleton, double p_delta) {
465
//
466
}
467
468
#ifdef TOOLS_ENABLED
469
void ChainIK3D::_update_mutable_info() {
470
if (!is_inside_tree()) {
471
return;
472
}
473
Skeleton3D *skeleton = get_skeleton();
474
if (!skeleton) {
475
for (uint32_t i = 0; i < settings.size(); i++) {
476
chain_settings[i]->root_global_rest = Transform3D();
477
}
478
return;
479
}
480
bool changed = false;
481
for (uint32_t i = 0; i < settings.size(); i++) {
482
int root_bone = chain_settings[i]->root_bone.bone;
483
if (root_bone < 0) {
484
continue;
485
}
486
Transform3D new_tr = get_bone_global_rest_mutable(skeleton, root_bone);
487
changed = changed || !chain_settings[i]->root_global_rest.is_equal_approx(new_tr);
488
chain_settings[i]->root_global_rest = new_tr;
489
}
490
if (changed) {
491
_make_gizmo_dirty();
492
}
493
}
494
495
Transform3D ChainIK3D::get_bone_global_rest_mutable(Skeleton3D *p_skeleton, int p_bone) {
496
int current = p_bone;
497
Transform3D accum;
498
int parent = p_skeleton->get_bone_parent(current);
499
if (parent >= 0) {
500
accum = p_skeleton->get_bone_global_rest(parent);
501
}
502
Transform3D tr = p_skeleton->get_bone_rest(current);
503
// Note:
504
// Chain IK gizmo might not be able to retrieve this pose in SkeletonModifier update process.
505
// So the gizmo uses bone_vector insteads but parent of root bone doesn't have bone_vector.
506
// Then, we needs to cache this pose in IK node.
507
tr.origin = p_skeleton->get_bone_pose_position(current);
508
accum *= tr;
509
return accum;
510
}
511
512
Transform3D ChainIK3D::get_chain_root_global_rest(int p_index) {
513
ERR_FAIL_INDEX_V(p_index, (int)settings.size(), Transform3D());
514
return chain_settings[p_index]->root_global_rest;
515
}
516
517
Vector3 ChainIK3D::get_bone_vector(int p_index, int p_joint) const {
518
return Vector3();
519
}
520
#endif // TOOLS_ENABLED
521
522
ChainIK3D::~ChainIK3D() {
523
clear_settings();
524
}
525
526