Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/3d/iterate_ik_3d.h
14709 views
1
/**************************************************************************/
2
/* iterate_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/chain_ik_3d.h"
34
35
#include "scene/resources/3d/joint_limitation_3d.h"
36
37
class IterateIK3D : public ChainIK3D {
38
GDCLASS(IterateIK3D, ChainIK3D);
39
40
public:
41
struct IterateIK3DJointSetting {
42
// To limit rotation.
43
RotationAxis rotation_axis = ROTATION_AXIS_ALL;
44
Vector3 rotation_axis_vector = Vector3(1, 0, 0);
45
Ref<JointLimitation3D> limitation;
46
SecondaryDirection limitation_right_axis = SECONDARY_DIRECTION_NONE;
47
Vector3 limitation_right_axis_vector = Vector3(1, 0, 0);
48
Quaternion limitation_rotation_offset;
49
50
// Rotation axis.
51
Vector3 get_rotation_axis_vector() const {
52
Vector3 ret;
53
switch (rotation_axis) {
54
case ROTATION_AXIS_X:
55
ret = Vector3(1, 0, 0);
56
break;
57
case ROTATION_AXIS_Y:
58
ret = Vector3(0, 1, 0);
59
break;
60
case ROTATION_AXIS_Z:
61
ret = Vector3(0, 0, 1);
62
break;
63
case ROTATION_AXIS_ALL:
64
ret = Vector3(0, 0, 0);
65
break;
66
case ROTATION_AXIS_CUSTOM:
67
ret = rotation_axis_vector;
68
break;
69
}
70
return ret;
71
}
72
73
Vector3 get_limitation_right_axis_vector() const {
74
Vector3 ret;
75
switch (limitation_right_axis) {
76
case SECONDARY_DIRECTION_NONE:
77
ret = Vector3(0, 0, 0);
78
break;
79
case SECONDARY_DIRECTION_PLUS_X:
80
ret = Vector3(1, 0, 0);
81
break;
82
case SECONDARY_DIRECTION_MINUS_X:
83
ret = Vector3(-1, 0, 0);
84
break;
85
case SECONDARY_DIRECTION_PLUS_Y:
86
ret = Vector3(0, 1, 0);
87
break;
88
case SECONDARY_DIRECTION_MINUS_Y:
89
ret = Vector3(0, -1, 0);
90
break;
91
case SECONDARY_DIRECTION_PLUS_Z:
92
ret = Vector3(0, 0, 1);
93
break;
94
case SECONDARY_DIRECTION_MINUS_Z:
95
ret = Vector3(0, 0, -1);
96
break;
97
case SECONDARY_DIRECTION_CUSTOM:
98
ret = limitation_right_axis_vector;
99
break;
100
}
101
return ret;
102
}
103
104
Quaternion get_limitation_space(const Vector3 &p_local_forward) const {
105
if (limitation.is_null()) {
106
return Quaternion();
107
}
108
return limitation->make_space(p_local_forward, get_limitation_right_axis_vector(), limitation_rotation_offset);
109
}
110
111
// Get rotation around normal vector (normal vector is rotation axis).
112
Vector3 get_projected_rotation(const Quaternion &p_offset, const Vector3 &p_vector) const {
113
ERR_FAIL_COND_V(rotation_axis == ROTATION_AXIS_ALL, p_vector);
114
const double ALMOST_ONE = 1.0 - CMP_EPSILON;
115
Vector3 axis = get_rotation_axis_vector().normalized();
116
Vector3 local_vector = p_offset.xform_inv(p_vector);
117
double length = local_vector.length();
118
Vector3 projected = snap_vector_to_plane(axis, local_vector.normalized());
119
if (!Math::is_zero_approx(length)) {
120
projected = projected.normalized() * length;
121
}
122
if (Math::abs(local_vector.normalized().dot(axis)) > ALMOST_ONE) {
123
return p_vector;
124
}
125
return p_offset.xform(projected);
126
}
127
128
// Get limited rotation from forward axis in local rest space.
129
Vector3 get_limited_rotation(const Quaternion &p_offset, const Vector3 &p_vector, const Vector3 &p_forward) const {
130
ERR_FAIL_COND_V(limitation.is_null(), p_vector);
131
Vector3 local_vector = p_offset.xform_inv(p_vector);
132
float length = local_vector.length();
133
if (Math::is_zero_approx(length)) {
134
return p_vector;
135
}
136
Vector3 limited = limitation->solve(p_forward, get_limitation_right_axis_vector(), limitation_rotation_offset, local_vector.normalized()) * length;
137
return p_offset.xform(limited);
138
}
139
140
~IterateIK3DJointSetting() {
141
limitation.unref();
142
}
143
};
144
145
struct IterateIK3DSetting : public ChainIK3DSetting {
146
NodePath target_node;
147
148
LocalVector<IterateIK3DJointSetting *> joint_settings;
149
150
bool simulated = false;
151
152
// Make rotation as bone pose from chain coordinates.
153
// p_extra is delta angle limitation.
154
void cache_current_joint_rotations(Skeleton3D *p_skeleton, double p_angular_delta_limit = Math::PI) {
155
Transform3D parent_gpose_tr;
156
int parent = p_skeleton->get_bone_parent(root_bone.bone);
157
if (parent >= 0) {
158
parent_gpose_tr = p_skeleton->get_bone_global_pose(parent);
159
}
160
Quaternion parent_gpose = parent_gpose_tr.basis.get_rotation_quaternion();
161
162
for (uint32_t i = 0; i < joints.size(); i++) {
163
int HEAD = i;
164
IKModifier3DSolverInfo *solver_info = solver_info_list[HEAD];
165
if (!solver_info) {
166
continue;
167
}
168
solver_info->current_lrest = p_skeleton->get_bone_pose(joints[HEAD].bone).basis.get_rotation_quaternion();
169
solver_info->current_grest = parent_gpose * solver_info->current_lrest;
170
solver_info->current_grest.normalize();
171
Vector3 from = solver_info->forward_vector;
172
Vector3 to = solver_info->current_grest.xform_inv(solver_info->current_vector).normalized();
173
Quaternion prev = solver_info->current_lpose;
174
if (joint_settings[HEAD]->rotation_axis == ROTATION_AXIS_ALL) {
175
solver_info->current_lpose = solver_info->current_lrest * get_swing(Quaternion(from, to), from);
176
} else {
177
// To stabilize rotation path especially nearely 180deg.
178
solver_info->current_lpose = solver_info->current_lrest * get_from_to_rotation_by_axis(from, to, joint_settings[HEAD]->get_rotation_axis_vector().normalized());
179
}
180
double diff = prev.angle_to(solver_info->current_lpose);
181
if (!Math::is_zero_approx(diff)) {
182
solver_info->current_lpose = prev.slerp(solver_info->current_lpose, MIN(1.0, p_angular_delta_limit / diff));
183
}
184
solver_info->current_gpose = parent_gpose * solver_info->current_lpose;
185
solver_info->current_gpose.normalize();
186
parent_gpose = solver_info->current_gpose;
187
}
188
189
// Apply back angular_delta_limit to chain coordinates.
190
if (chain.is_empty()) {
191
return;
192
}
193
chain[0] = p_skeleton->get_bone_global_pose(root_bone.bone).origin;
194
for (uint32_t i = 0; i < solver_info_list.size(); i++) {
195
int HEAD = i;
196
int TAIL = i + 1;
197
IKModifier3DSolverInfo *solver_info = solver_info_list[HEAD];
198
if (!solver_info) {
199
continue;
200
}
201
chain[TAIL] = chain[HEAD] + solver_info->current_gpose.xform(solver_info->forward_vector) * solver_info->length;
202
}
203
cache_current_vectors(p_skeleton);
204
}
205
206
void init_joints(Skeleton3D *p_skeleton, bool p_mutable_bone_axes) {
207
chain.clear();
208
bool extends_end = extend_end_bone && end_bone_length > 0;
209
for (uint32_t i = 0; i < joints.size(); i++) {
210
chain.push_back(p_skeleton->get_bone_global_pose(joints[i].bone).origin);
211
bool last = i == joints.size() - 1;
212
if (last && extends_end) {
213
Vector3 axis = IKModifier3D::get_bone_axis(p_skeleton, end_bone.bone, end_bone_direction, p_mutable_bone_axes);
214
if (axis.is_zero_approx()) {
215
continue;
216
}
217
if (!solver_info_list[i]) {
218
solver_info_list[i] = memnew(IKModifier3DSolverInfo);
219
}
220
solver_info_list[i]->forward_vector = snap_vector_to_plane(joint_settings[i]->get_rotation_axis_vector(), axis.normalized());
221
solver_info_list[i]->length = end_bone_length;
222
chain.push_back(p_skeleton->get_bone_global_pose(joints[i].bone).xform(axis * end_bone_length));
223
} else if (!last) {
224
Vector3 axis = p_skeleton->get_bone_rest(joints[i + 1].bone).origin;
225
if (axis.is_zero_approx()) {
226
continue;
227
}
228
if (!solver_info_list[i]) {
229
solver_info_list[i] = memnew(IKModifier3DSolverInfo);
230
}
231
solver_info_list[i]->forward_vector = snap_vector_to_plane(joint_settings[i]->get_rotation_axis_vector(), axis.normalized());
232
solver_info_list[i]->length = axis.length();
233
}
234
}
235
init_current_joint_rotations(p_skeleton);
236
}
237
238
~IterateIK3DSetting() {
239
for (uint32_t i = 0; i < joint_settings.size(); i++) {
240
if (joint_settings[i]) {
241
memdelete(joint_settings[i]);
242
joint_settings[i] = nullptr;
243
}
244
}
245
joint_settings.clear();
246
}
247
};
248
249
protected:
250
LocalVector<IterateIK3DSetting *> iterate_settings; // For caching.
251
252
int max_iterations = 4;
253
double min_distance = 0.001; // If distance between end joint and target is less than min_distance, finish iteration.
254
double min_distance_squared = min_distance * min_distance; // For cache.
255
double angular_delta_limit = Math::deg_to_rad(2.0); // If the delta is too large, the results before and after iterating can change significantly, and divergence of calculations can easily occur.
256
257
bool deterministic = false;
258
259
bool _get(const StringName &p_path, Variant &r_ret) const;
260
bool _set(const StringName &p_path, const Variant &p_value);
261
void _get_property_list(List<PropertyInfo> *p_list) const;
262
void _validate_dynamic_prop(PropertyInfo &p_property) const;
263
264
static void _bind_methods();
265
266
virtual void _validate_axis(Skeleton3D *p_skeleton, int p_index, int p_joint) const override;
267
virtual void _init_joints(Skeleton3D *p_skeleton, int p_index) override;
268
void _clear_joints(int p_index); // Connect signal with the IterateIK3D node so it shouldn't be included by struct IterateIK3DSetting.
269
270
virtual void _make_simulation_dirty(int p_index) override;
271
virtual void _update_bone_axis(Skeleton3D *p_skeleton, int p_index) override;
272
273
virtual void _process_ik(Skeleton3D *p_skeleton, double p_delta) override;
274
void _process_joints(double p_delta, Skeleton3D *p_skeleton, IterateIK3DSetting *p_setting, const Vector3 &p_target_destination);
275
virtual void _solve_iteration(double p_delta, Skeleton3D *p_skeleton, IterateIK3DSetting *p_setting, const Vector3 &p_destination);
276
277
virtual void _set_joint_count(int p_index, int p_count) override;
278
279
void _update_joint_limitation(int p_index, int p_joint);
280
void _bind_joint_limitation(int p_index, int p_joint);
281
void _unbind_joint_limitation(int p_index, int p_joint);
282
void _bind_joint_limitations(int p_index);
283
void _unbind_joint_limitations(int p_index);
284
285
public:
286
virtual PackedStringArray get_configuration_warnings() const override;
287
virtual void set_setting_count(int p_count) override {
288
_set_setting_count<IterateIK3DSetting>(p_count);
289
iterate_settings = _cast_settings<IterateIK3DSetting>();
290
chain_settings = _cast_settings<ChainIK3DSetting>(); // Don't forget to sync super class settings.
291
}
292
virtual void clear_settings() override {
293
_set_setting_count<IterateIK3DSetting>(0);
294
iterate_settings.clear();
295
chain_settings.clear(); // Don't forget to sync super class settings.
296
}
297
298
void set_max_iterations(int p_max_iterations);
299
int get_max_iterations() const;
300
void set_min_distance(double p_min_distance);
301
double get_min_distance() const;
302
void set_angular_delta_limit(double p_angular_delta_limit);
303
double get_angular_delta_limit() const;
304
305
void set_deterministic(bool p_deterministic);
306
bool is_deterministic() const;
307
308
// Setting.
309
void set_target_node(int p_index, const NodePath &p_target_node);
310
NodePath get_target_node(int p_index) const;
311
312
// Individual joints.
313
void set_joint_rotation_axis(int p_index, int p_joint, RotationAxis p_axis);
314
RotationAxis get_joint_rotation_axis(int p_index, int p_joint) const;
315
void set_joint_rotation_axis_vector(int p_index, int p_joint, const Vector3 &p_vector);
316
Vector3 get_joint_rotation_axis_vector(int p_index, int p_joint) const;
317
void set_joint_limitation(int p_index, int p_joint, const Ref<JointLimitation3D> &p_limitation);
318
Ref<JointLimitation3D> get_joint_limitation(int p_index, int p_joint) const;
319
void set_joint_limitation_right_axis(int p_index, int p_joint, SecondaryDirection p_direction);
320
SecondaryDirection get_joint_limitation_right_axis(int p_index, int p_joint) const;
321
void set_joint_limitation_right_axis_vector(int p_index, int p_joint, const Vector3 &p_vector);
322
Vector3 get_joint_limitation_right_axis_vector(int p_index, int p_joint) const;
323
void set_joint_limitation_rotation_offset(int p_index, int p_joint, const Quaternion &p_offset);
324
Quaternion get_joint_limitation_rotation_offset(int p_index, int p_joint) const;
325
326
// Helper.
327
Quaternion get_joint_limitation_space(int p_index, int p_joint, const Vector3 &p_forward) const;
328
329
#ifdef TOOLS_ENABLED
330
virtual Vector3 get_bone_vector(int p_index, int p_joint) const override;
331
#endif // TOOLS_ENABLED
332
333
~IterateIK3D();
334
};
335
336