Debian Codegraph @

scene/3d/limit_angular_velocity_modifier_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* limit_angular_velocity_modifier_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 "limit_angular_velocity_modifier_3d.h"
32
33 bool LimitAngularVelocityModifier3D::_set(const StringName &p_path, const Variant &p_value) {
34 String path = p_path;
35
36 if (path.begins_with("chains/")) {
37 int which = path.get_slicec('/', 1).to_int();
38 String what = path.get_slicec('/', 2);
39 ERR_FAIL_INDEX_V(which, (int)chains.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 set_end_bone(which, p_value);
49 } else {
50 return false;
51 }
52 }
53 return true;
54 }
55
56 bool LimitAngularVelocityModifier3D::_get(const StringName &p_path, Variant &r_ret) const {
57 String path = p_path;
58
59 if (path.begins_with("chains/")) {
60 int which = path.get_slicec('/', 1).to_int();
61 ERR_FAIL_INDEX_V(which, (int)chains.size(), false);
62 String what = path.get_slicec('/', 2);
63 if (what == "root_bone_name") {
64 r_ret = get_root_bone_name(which);
65 } else if (what == "root_bone") {
66 r_ret = get_root_bone(which);
67 } else if (what == "end_bone_name") {
68 r_ret = get_end_bone_name(which);
69 } else if (what == "end_bone") {
70 r_ret = get_end_bone(which);
71 } else {
72 return false;
73 }
74 }
75 if (path.begins_with("joints/")) {
76 int which = path.get_slicec('/', 1).to_int();
77 ERR_FAIL_INDEX_V(which, (int)joints.size(), false);
78 String what = path.get_slicec('/', 2);
79 if (what == "bone_name") {
80 r_ret = get_joint_bone_name(which);
81 } else if (what == "bone") {
82 r_ret = get_joint_bone(which);
83 } else {
84 return false;
85 }
86 }
87 return true;
88 }
89
90 void LimitAngularVelocityModifier3D::_get_property_list(List<PropertyInfo> *p_list) const {
91 String enum_hint;
92 Skeleton3D *skeleton = get_skeleton();
93 if (skeleton) {
94 enum_hint = skeleton->get_concatenated_bone_names();
95 }
96
97 for (uint32_t i = 0; i < chains.size(); i++) {
98 String path = "chains/" + itos(i) + "/";
99 p_list->push_back(PropertyInfo(Variant::STRING, path + "root_bone_name", PROPERTY_HINT_ENUM_SUGGESTION, enum_hint));
100 p_list->push_back(PropertyInfo(Variant::INT, path + "root_bone", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
101 p_list->push_back(PropertyInfo(Variant::STRING, path + "end_bone_name", PROPERTY_HINT_ENUM_SUGGESTION, enum_hint));
102 p_list->push_back(PropertyInfo(Variant::INT, path + "end_bone", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
103 }
104
105 for (uint32_t i = 0; i < joints.size(); i++) {
106 String path = "joints/" + itos(i) + "/";
107 p_list->push_back(PropertyInfo(Variant::STRING, path + "bone_name", PROPERTY_HINT_ENUM_SUGGESTION, enum_hint, PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_READ_ONLY));
108 p_list->push_back(PropertyInfo(Variant::INT, path + "bone", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_READ_ONLY));
109 }
110 }
111
112 void LimitAngularVelocityModifier3D::_validate_property(PropertyInfo &p_property) const {
113 if (p_property.name == "joint_count") {
114 p_property.usage = PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_ARRAY | PROPERTY_USAGE_READ_ONLY;
115 p_property.class_name = "Joints,joints/,static,const";
116 }
117 }
118
119 void LimitAngularVelocityModifier3D::_notification(int p_what) {
120 switch (p_what) {
121 case NOTIFICATION_ENTER_TREE: {
122 _make_joints_dirty();
123 } break;
124 }
125 }
126
127 // Setting.
128
129 void LimitAngularVelocityModifier3D::set_root_bone_name(int p_index, const String &p_bone_name) {
130 ERR_FAIL_INDEX(p_index, (int)chains.size());
131 chains[p_index].root_bone.name = p_bone_name;
132 Skeleton3D *sk = get_skeleton();
133 if (sk) {
134 set_root_bone(p_index, sk->find_bone(chains[p_index].root_bone.name));
135 }
136 }
137
138 String LimitAngularVelocityModifier3D::get_root_bone_name(int p_index) const {
139 ERR_FAIL_INDEX_V(p_index, (int)chains.size(), String());
140 return chains[p_index].root_bone.name;
141 }
142
143 void LimitAngularVelocityModifier3D::set_root_bone(int p_index, int p_bone) {
144 ERR_FAIL_INDEX(p_index, (int)chains.size());
145 bool changed = chains[p_index].root_bone.bone != p_bone;
146 chains[p_index].root_bone.bone = p_bone;
147 Skeleton3D *sk = get_skeleton();
148 if (sk) {
149 if (chains[p_index].root_bone.bone <= -1 || chains[p_index].root_bone.bone >= sk->get_bone_count()) {
150 WARN_PRINT("Root bone index out of range!");
151 chains[p_index].root_bone.bone = -1;
152 } else {
153 chains[p_index].root_bone.name = sk->get_bone_name(chains[p_index].root_bone.bone);
154 }
155 }
156 if (changed) {
157 _make_joints_dirty();
158 }
159 }
160
161 int LimitAngularVelocityModifier3D::get_root_bone(int p_index) const {
162 ERR_FAIL_INDEX_V(p_index, (int)chains.size(), -1);
163 return chains[p_index].root_bone.bone;
164 }
165
166 void LimitAngularVelocityModifier3D::set_end_bone_name(int p_index, const String &p_bone_name) {
167 ERR_FAIL_INDEX(p_index, (int)chains.size());
168 chains[p_index].end_bone.name = p_bone_name;
169 Skeleton3D *sk = get_skeleton();
170 if (sk) {
171 set_end_bone(p_index, sk->find_bone(chains[p_index].end_bone.name));
172 }
173 }
174
175 String LimitAngularVelocityModifier3D::get_end_bone_name(int p_index) const {
176 ERR_FAIL_INDEX_V(p_index, (int)chains.size(), String());
177 return chains[p_index].end_bone.name;
178 }
179
180 void LimitAngularVelocityModifier3D::set_end_bone(int p_index, int p_bone) {
181 ERR_FAIL_INDEX(p_index, (int)chains.size());
182 bool changed = chains[p_index].end_bone.bone != p_bone;
183 chains[p_index].end_bone.bone = p_bone;
184 Skeleton3D *sk = get_skeleton();
185 if (sk) {
186 if (chains[p_index].end_bone.bone <= -1 || chains[p_index].end_bone.bone >= sk->get_bone_count()) {
187 WARN_PRINT("End bone index out of range!");
188 chains[p_index].end_bone.bone = -1;
189 } else {
190 chains[p_index].end_bone.name = sk->get_bone_name(chains[p_index].end_bone.bone);
191 }
192 }
193 if (changed) {
194 _make_joints_dirty();
195 }
196 }
197
198 int LimitAngularVelocityModifier3D::get_end_bone(int p_index) const {
199 ERR_FAIL_INDEX_V(p_index, (int)chains.size(), -1);
200 return chains[p_index].end_bone.bone;
201 }
202
203 void LimitAngularVelocityModifier3D::set_chain_count(int p_count) {
204 ERR_FAIL_COND(p_count < 0);
205 chains.resize(p_count);
206 _make_joints_dirty();
207 }
208
209 int LimitAngularVelocityModifier3D::get_chain_count() const {
210 return chains.size();
211 }
212
213 void LimitAngularVelocityModifier3D::clear_chains() {
214 set_chain_count(0);
215 }
216
217 String LimitAngularVelocityModifier3D::get_joint_bone_name(int p_index) const {
218 ERR_FAIL_INDEX_V(p_index, (int)joints.size(), String());
219 return joints[p_index].name;
220 }
221
222 int LimitAngularVelocityModifier3D::get_joint_bone(int p_index) const {
223 ERR_FAIL_INDEX_V(p_index, (int)joints.size(), -1);
224 return joints[p_index].bone;
225 }
226
227 int LimitAngularVelocityModifier3D::_get_joint_count() const {
228 return joints.size();
229 }
230
231 void LimitAngularVelocityModifier3D::set_max_angular_velocity(double p_angular_velocity) {
232 max_angular_velocity = p_angular_velocity;
233 }
234
235 double LimitAngularVelocityModifier3D::get_max_angular_velocity() const {
236 return max_angular_velocity;
237 }
238
239 void LimitAngularVelocityModifier3D::set_exclude(bool p_exclude) {
240 exclude = p_exclude;
241 }
242
243 bool LimitAngularVelocityModifier3D::is_exclude() const {
244 return exclude;
245 }
246
247 void LimitAngularVelocityModifier3D::_bind_methods() {
248 // Setting.
249 ClassDB::bind_method(D_METHOD("set_root_bone_name", "index", "bone_name"), &LimitAngularVelocityModifier3D::set_root_bone_name);
250 ClassDB::bind_method(D_METHOD("get_root_bone_name", "index"), &LimitAngularVelocityModifier3D::get_root_bone_name);
251 ClassDB::bind_method(D_METHOD("set_root_bone", "index", "bone"), &LimitAngularVelocityModifier3D::set_root_bone);
252 ClassDB::bind_method(D_METHOD("get_root_bone", "index"), &LimitAngularVelocityModifier3D::get_root_bone);
253
254 ClassDB::bind_method(D_METHOD("set_end_bone_name", "index", "bone_name"), &LimitAngularVelocityModifier3D::set_end_bone_name);
255 ClassDB::bind_method(D_METHOD("get_end_bone_name", "index"), &LimitAngularVelocityModifier3D::get_end_bone_name);
256 ClassDB::bind_method(D_METHOD("set_end_bone", "index", "bone"), &LimitAngularVelocityModifier3D::set_end_bone);
257 ClassDB::bind_method(D_METHOD("get_end_bone", "index"), &LimitAngularVelocityModifier3D::get_end_bone);
258
259 ClassDB::bind_method(D_METHOD("set_chain_count", "count"), &LimitAngularVelocityModifier3D::set_chain_count);
260 ClassDB::bind_method(D_METHOD("get_chain_count"), &LimitAngularVelocityModifier3D::get_chain_count);
261 ClassDB::bind_method(D_METHOD("clear_chains"), &LimitAngularVelocityModifier3D::clear_chains);
262
263 ClassDB::bind_method(D_METHOD("set_max_angular_velocity", "angular_velocity"), &LimitAngularVelocityModifier3D::set_max_angular_velocity);
264 ClassDB::bind_method(D_METHOD("get_max_angular_velocity"), &LimitAngularVelocityModifier3D::get_max_angular_velocity);
265 ClassDB::bind_method(D_METHOD("set_exclude", "exclude"), &LimitAngularVelocityModifier3D::set_exclude);
266 ClassDB::bind_method(D_METHOD("is_exclude"), &LimitAngularVelocityModifier3D::is_exclude);
267
268 ClassDB::bind_method(D_METHOD("reset"), &LimitAngularVelocityModifier3D::reset);
269
270 ClassDB::bind_method(D_METHOD("_get_joint_count"), &LimitAngularVelocityModifier3D::_get_joint_count);
271
272 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_angular_velocity", PROPERTY_HINT_RANGE, "0,720,or_greater,radians_as_degrees,suffix:" + String(U"°") + "/s"), "set_max_angular_velocity", "get_max_angular_velocity");
273 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exclude"), "set_exclude", "is_exclude");
274 ADD_ARRAY_COUNT("Chains", "chain_count", "set_chain_count", "get_chain_count", "chains/");
275 ADD_ARRAY_COUNT("Joints", "joint_count", "", "_get_joint_count", "joints/");
276 }
277
278 void LimitAngularVelocityModifier3D::_set_active(bool p_active) {
279 if (p_active) {
280 reset();
281 }
282 }
283
284 void LimitAngularVelocityModifier3D::_skeleton_changed(Skeleton3D *p_old, Skeleton3D *p_new) {
285 _make_joints_dirty();
286 }
287
288 void LimitAngularVelocityModifier3D::_validate_bone_names() {
289 for (uint32_t i = 0; i < chains.size(); i++) {
290 // Prior bone name.
291 if (!chains[i].root_bone.name.is_empty()) {
292 set_root_bone_name(i, chains[i].root_bone.name);
293 } else if (chains[i].root_bone.bone != -1) {
294 set_root_bone(i, chains[i].root_bone.bone);
295 }
296 // Prior bone name.
297 if (!chains[i].end_bone.name.is_empty()) {
298 set_end_bone_name(i, chains[i].end_bone.name);
299 } else if (chains[i].end_bone.bone != -1) {
300 set_end_bone(i, chains[i].end_bone.bone);
301 }
302 }
303 }
304
305 void LimitAngularVelocityModifier3D::_make_joints_dirty() {
306 if (joints_dirty) {
307 return;
308 }
309 joints_dirty = true;
310 callable_mp(this, &LimitAngularVelocityModifier3D::_update_joints).call_deferred();
311 }
312
313 bool LimitAngularVelocityModifier3D::_is_joint_contained(int p_bone) {
314 bool ret = false;
315 for (const BoneJoint &joint : joints) {
316 if (joint.bone == p_bone) {
317 ret = true;
318 break;
319 }
320 }
321 return ret;
322 }
323
324 void LimitAngularVelocityModifier3D::_update_joints() {
325 joints.clear();
326 bones.clear();
327
328 Skeleton3D *sk = get_skeleton();
329 if (!sk) {
330 joints_dirty = false;
331 notify_property_list_changed();
332 return;
333 }
334
335 LocalVector<int> tmp_joints;
336 for (uint32_t i = 0; i < chains.size(); i++) {
337 tmp_joints.clear();
338 Chain cn = chains[i];
339 int current_bone = cn.end_bone.bone;
340 int root_bone = cn.root_bone.bone;
341 if (current_bone < 0 || root_bone < 0) {
342 continue;
343 }
344 // Validation.
345 bool valid = false;
346 while (current_bone >= 0) {
347 if (current_bone == root_bone) {
348 valid = true;
349 break;
350 }
351 current_bone = sk->get_bone_parent(current_bone);
352 }
353 if (!valid) {
354 ERR_PRINT_ED("Chains[" + itos(i) + "]: End bone must be the same as or a child of root bone.");
355 continue;
356 }
357 current_bone = cn.end_bone.bone;
358 while (current_bone != root_bone) {
359 tmp_joints.push_back(current_bone);
360 current_bone = sk->get_bone_parent(current_bone);
361 }
362 tmp_joints.push_back(current_bone);
363 tmp_joints.reverse();
364 for (uint32_t j = 0; j < tmp_joints.size(); j++) {
365 int bn = tmp_joints[j];
366 if (!_is_joint_contained(bn)) {
367 BoneJoint bj;
368 bj.bone = bn;
369 bj.name = sk->get_bone_name(bn);
370 joints.push_back(bj);
371 }
372 }
373 }
374
375 if (exclude) {
376 for (int b = 0; b < sk->get_bone_count(); b++) {
377 if (_is_joint_contained(b)) {
378 continue;
379 }
380 BoneRot br;
381 br.first = b;
382 br.second = sk->get_bone_pose_rotation(b);
383 bones.push_back(br);
384 }
385 } else {
386 for (const BoneJoint &E : joints) {
387 BoneRot br;
388 br.first = E.bone;
389 br.second = sk->get_bone_pose_rotation(E.bone);
390 bones.push_back(br);
391 }
392 }
393
394 joints_dirty = false;
395 reset();
396
397 notify_property_list_changed();
398 }
399
400 void LimitAngularVelocityModifier3D::_process_modification(double p_delta) {
401 Skeleton3D *skeleton = get_skeleton();
402 if (!skeleton) {
403 return;
404 }
405
406 if (init_needed) {
407 // Note:
408 // The pose retrieval within `_update_joints()` is done outside the skeleton's update process,
409 // so it ignores the pose resulting from the previous modifier's modification.
410 // This causes unintended initialization when `active` is set to true, so it must be initialized here.
411 for (uint32_t i = 0; i < bones.size(); i++) {
412 bones[i].second = skeleton->get_bone_pose_rotation(bones[i].first);
413 }
414 init_needed = false;
415 }
416
417 double limit_in_frame = max_angular_velocity * p_delta;
418 for (uint32_t i = 0; i < bones.size(); i++) {
419 int bn = bones[i].first;
420 Quaternion dest = skeleton->get_bone_pose_rotation(bn);
421 double diff = bones[i].second.angle_to(dest);
422 if (!Math::is_zero_approx(diff)) {
423 bones[i].second = bones[i].second.slerp(dest, MIN(1.0, limit_in_frame / diff));
424 }
425 skeleton->set_bone_pose_rotation(bn, bones[i].second);
426 }
427 }
428
429 void LimitAngularVelocityModifier3D::reset() {
430 init_needed = true;
431 }
432
433 LimitAngularVelocityModifier3D::~LimitAngularVelocityModifier3D() {
434 clear_chains();
435 }