| 1 | /**************************************************************************/ |
| 2 | /* two_bone_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 "two_bone_ik_3d.h" |
| 32 | |
| 33 | bool TwoBoneIK3D::_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 == "target_node") { |
| 42 | set_target_node(which, p_value); |
| 43 | } else if (what == "pole_node") { |
| 44 | set_pole_node(which, p_value); |
| 45 | } else if (what == "root_bone_name") { |
| 46 | set_root_bone_name(which, p_value); |
| 47 | } else if (what == "root_bone") { |
| 48 | set_root_bone(which, p_value); |
| 49 | } else if (what == "middle_bone_name") { |
| 50 | set_middle_bone_name(which, p_value); |
| 51 | } else if (what == "middle_bone") { |
| 52 | set_middle_bone(which, p_value); |
| 53 | } else if (what == "pole_direction") { |
| 54 | set_pole_direction(which, static_cast<SecondaryDirection>((int)p_value)); |
| 55 | } else if (what == "pole_direction_vector") { |
| 56 | set_pole_direction_vector(which, p_value); |
| 57 | } else if (what == "end_bone_name") { |
| 58 | set_end_bone_name(which, p_value); |
| 59 | } else if (what == "end_bone") { |
| 60 | String opt = path.get_slicec('/', 3); |
| 61 | if (opt.is_empty()) { |
| 62 | set_end_bone(which, p_value); |
| 63 | } else if (opt == "direction") { |
| 64 | set_end_bone_direction(which, static_cast<BoneDirection>((int)p_value)); |
| 65 | } else if (opt == "length") { |
| 66 | set_end_bone_length(which, p_value); |
| 67 | } else { |
| 68 | return false; |
| 69 | } |
| 70 | } else if (what == "use_virtual_end") { |
| 71 | set_use_virtual_end(which, p_value); |
| 72 | } else if (what == "extend_end_bone") { |
| 73 | set_extend_end_bone(which, p_value); |
| 74 | } else { |
| 75 | return false; |
| 76 | } |
| 77 | } |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | bool TwoBoneIK3D::_get(const StringName &p_path, Variant &r_ret) const { |
| 82 | String path = p_path; |
| 83 | |
| 84 | if (path.begins_with("settings/")) { |
| 85 | int which = path.get_slicec('/', 1).to_int(); |
| 86 | String what = path.get_slicec('/', 2); |
| 87 | ERR_FAIL_INDEX_V(which, (int)settings.size(), false); |
| 88 | |
| 89 | if (what == "target_node") { |
| 90 | r_ret = get_target_node(which); |
| 91 | } else if (what == "pole_node") { |
| 92 | r_ret = get_pole_node(which); |
| 93 | } else if (what == "root_bone_name") { |
| 94 | r_ret = get_root_bone_name(which); |
| 95 | } else if (what == "root_bone") { |
| 96 | r_ret = get_root_bone(which); |
| 97 | } else if (what == "middle_bone_name") { |
| 98 | r_ret = get_middle_bone_name(which); |
| 99 | } else if (what == "middle_bone") { |
| 100 | r_ret = get_middle_bone(which); |
| 101 | } else if (what == "pole_direction") { |
| 102 | r_ret = (int)get_pole_direction(which); |
| 103 | } else if (what == "pole_direction_vector") { |
| 104 | r_ret = get_pole_direction_vector(which); |
| 105 | } else if (what == "end_bone_name") { |
| 106 | r_ret = get_end_bone_name(which); |
| 107 | } else if (what == "end_bone") { |
| 108 | String opt = path.get_slicec('/', 3); |
| 109 | if (opt.is_empty()) { |
| 110 | r_ret = get_end_bone(which); |
| 111 | } else if (opt == "direction") { |
| 112 | r_ret = (int)get_end_bone_direction(which); |
| 113 | } else if (opt == "length") { |
| 114 | r_ret = get_end_bone_length(which); |
| 115 | } else { |
| 116 | return false; |
| 117 | } |
| 118 | } else if (what == "use_virtual_end") { |
| 119 | r_ret = is_using_virtual_end(which); |
| 120 | } else if (what == "extend_end_bone") { |
| 121 | r_ret = is_end_bone_extended(which); |
| 122 | } else { |
| 123 | return false; |
| 124 | } |
| 125 | } |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | void TwoBoneIK3D::_get_property_list(List<PropertyInfo> *p_list) const { |
| 130 | String enum_hint; |
| 131 | Skeleton3D *skeleton = get_skeleton(); |
| 132 | if (skeleton) { |
| 133 | enum_hint = skeleton->get_concatenated_bone_names(); |
| 134 | } |
| 135 | |
| 136 | LocalVector<PropertyInfo> props; |
| 137 | |
| 138 | for (uint32_t i = 0; i < settings.size(); i++) { |
| 139 | String path = "settings/" + itos(i) + "/"; |
| 140 | props.push_back(PropertyInfo(Variant::NODE_PATH, path + "target_node")); |
| 141 | props.push_back(PropertyInfo(Variant::NODE_PATH, path + "pole_node")); |
| 142 | props.push_back(PropertyInfo(Variant::STRING, path + "root_bone_name", PROPERTY_HINT_ENUM_SUGGESTION, enum_hint)); |
| 143 | props.push_back(PropertyInfo(Variant::INT, path + "root_bone", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR)); |
| 144 | props.push_back(PropertyInfo(Variant::STRING, path + "middle_bone_name", PROPERTY_HINT_ENUM_SUGGESTION, enum_hint)); |
| 145 | props.push_back(PropertyInfo(Variant::INT, path + "middle_bone", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR)); |
| 146 | props.push_back(PropertyInfo(Variant::INT, path + "pole_direction", PROPERTY_HINT_ENUM, SkeletonModifier3D::get_hint_secondary_direction())); |
| 147 | props.push_back(PropertyInfo(Variant::VECTOR3, path + "pole_direction_vector")); |
| 148 | props.push_back(PropertyInfo(Variant::STRING, path + "end_bone_name", PROPERTY_HINT_ENUM_SUGGESTION, enum_hint)); |
| 149 | props.push_back(PropertyInfo(Variant::INT, path + "end_bone", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR)); |
| 150 | props.push_back(PropertyInfo(Variant::BOOL, path + "use_virtual_end")); |
| 151 | props.push_back(PropertyInfo(Variant::BOOL, path + "extend_end_bone")); |
| 152 | props.push_back(PropertyInfo(Variant::INT, path + "end_bone/direction", PROPERTY_HINT_ENUM, SkeletonModifier3D::get_hint_bone_direction())); |
| 153 | props.push_back(PropertyInfo(Variant::FLOAT, path + "end_bone/length", PROPERTY_HINT_RANGE, "0,1,0.001,or_greater,suffix:m")); |
| 154 | } |
| 155 | |
| 156 | for (PropertyInfo &p : props) { |
| 157 | _validate_dynamic_prop(p); |
| 158 | p_list->push_back(p); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | void TwoBoneIK3D::_validate_dynamic_prop(PropertyInfo &p_property) const { |
| 163 | PackedStringArray split = p_property.name.split("/"); |
| 164 | if (split.size() > 2 && split[0] == "settings") { |
| 165 | int which = split[1].to_int(); |
| 166 | |
| 167 | bool force_hide = false; |
| 168 | if ((split[2] == "end_bone" || split[2] == "end_bone_name") && split.size() == 3 && is_using_virtual_end(which)) { |
| 169 | p_property.usage = PROPERTY_USAGE_NONE; |
| 170 | } |
| 171 | if (split[2] == "use_virtual_end" && get_middle_bone(which) == -1) { |
| 172 | p_property.usage = PROPERTY_USAGE_NONE; |
| 173 | } |
| 174 | if (split[2] == "extend_end_bone") { |
| 175 | if (is_using_virtual_end(which)) { |
| 176 | p_property.usage = PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_READ_ONLY; |
| 177 | } else if (get_end_bone(which) == -1) { |
| 178 | p_property.usage = PROPERTY_USAGE_NONE; |
| 179 | force_hide = true; |
| 180 | } |
| 181 | } |
| 182 | if (force_hide || (split[2] == "end_bone" && !is_end_bone_extended(which) && split.size() > 3)) { |
| 183 | p_property.usage = PROPERTY_USAGE_NONE; |
| 184 | } |
| 185 | |
| 186 | if (split[2] == "pole_direction_vector" && get_pole_direction(which) != SECONDARY_DIRECTION_CUSTOM) { |
| 187 | p_property.usage = PROPERTY_USAGE_NONE; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | PackedStringArray TwoBoneIK3D::get_configuration_warnings() const { |
| 193 | PackedStringArray warnings = SkeletonModifier3D::get_configuration_warnings(); |
| 194 | for (uint32_t i = 0; i < tb_settings.size(); i++) { |
| 195 | if (tb_settings[i]->target_node.is_empty()) { |
| 196 | warnings.push_back(RTR("Detecting settings with no target set! TwoBoneIK3D must have a target to work.")); |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | for (uint32_t i = 0; i < tb_settings.size(); i++) { |
| 201 | if (tb_settings[i]->target_node.is_empty()) { |
| 202 | warnings.push_back(RTR("Detecting settings with no pole target set! TwoBoneIK3D must have a pole target to work.")); |
| 203 | break; |
| 204 | } |
| 205 | } |
| 206 | return warnings; |
| 207 | } |
| 208 | |
| 209 | // Setting. |
| 210 | |
| 211 | void TwoBoneIK3D::set_root_bone_name(int p_index, const String &p_bone_name) { |
| 212 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 213 | tb_settings[p_index]->root_bone.name = p_bone_name; |
| 214 | Skeleton3D *sk = get_skeleton(); |
| 215 | if (sk) { |
| 216 | set_root_bone(p_index, sk->find_bone(tb_settings[p_index]->root_bone.name)); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | String TwoBoneIK3D::get_root_bone_name(int p_index) const { |
| 221 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), String()); |
| 222 | return tb_settings[p_index]->root_bone.name; |
| 223 | } |
| 224 | |
| 225 | void TwoBoneIK3D::set_root_bone(int p_index, int p_bone) { |
| 226 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 227 | bool changed = tb_settings[p_index]->root_bone.bone != p_bone; |
| 228 | tb_settings[p_index]->root_bone.bone = p_bone; |
| 229 | Skeleton3D *sk = get_skeleton(); |
| 230 | if (sk) { |
| 231 | if (tb_settings[p_index]->root_bone.bone <= -1 || tb_settings[p_index]->root_bone.bone >= sk->get_bone_count()) { |
| 232 | WARN_PRINT("Root bone index out of range!"); |
| 233 | tb_settings[p_index]->root_bone.bone = -1; |
| 234 | } else { |
| 235 | tb_settings[p_index]->root_bone.name = sk->get_bone_name(tb_settings[p_index]->root_bone.bone); |
| 236 | } |
| 237 | } |
| 238 | if (changed) { |
| 239 | _update_joints(p_index); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | int TwoBoneIK3D::get_root_bone(int p_index) const { |
| 244 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), -1); |
| 245 | return tb_settings[p_index]->root_bone.bone; |
| 246 | } |
| 247 | |
| 248 | void TwoBoneIK3D::set_middle_bone_name(int p_index, const String &p_bone_name) { |
| 249 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 250 | tb_settings[p_index]->middle_bone.name = p_bone_name; |
| 251 | Skeleton3D *sk = get_skeleton(); |
| 252 | if (sk) { |
| 253 | set_middle_bone(p_index, sk->find_bone(tb_settings[p_index]->middle_bone.name)); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | String TwoBoneIK3D::get_middle_bone_name(int p_index) const { |
| 258 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), String()); |
| 259 | return tb_settings[p_index]->middle_bone.name; |
| 260 | } |
| 261 | |
| 262 | void TwoBoneIK3D::set_middle_bone(int p_index, int p_bone) { |
| 263 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 264 | bool changed = tb_settings[p_index]->middle_bone.bone != p_bone; |
| 265 | tb_settings[p_index]->middle_bone.bone = p_bone; |
| 266 | Skeleton3D *sk = get_skeleton(); |
| 267 | if (sk) { |
| 268 | if (tb_settings[p_index]->middle_bone.bone <= -1 || tb_settings[p_index]->middle_bone.bone >= sk->get_bone_count()) { |
| 269 | WARN_PRINT("Middle bone index out of range!"); |
| 270 | tb_settings[p_index]->middle_bone.bone = -1; |
| 271 | tb_settings[p_index]->use_virtual_end = false; // To sync inspector. |
| 272 | } else { |
| 273 | tb_settings[p_index]->middle_bone.name = sk->get_bone_name(tb_settings[p_index]->middle_bone.bone); |
| 274 | } |
| 275 | } |
| 276 | if (changed) { |
| 277 | _update_joints(p_index); |
| 278 | } |
| 279 | notify_property_list_changed(); |
| 280 | } |
| 281 | |
| 282 | int TwoBoneIK3D::get_middle_bone(int p_index) const { |
| 283 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), -1); |
| 284 | return tb_settings[p_index]->middle_bone.bone; |
| 285 | } |
| 286 | |
| 287 | void TwoBoneIK3D::set_end_bone_name(int p_index, const String &p_bone_name) { |
| 288 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 289 | tb_settings[p_index]->end_bone.name = p_bone_name; |
| 290 | Skeleton3D *sk = get_skeleton(); |
| 291 | if (sk) { |
| 292 | set_end_bone(p_index, sk->find_bone(tb_settings[p_index]->end_bone.name)); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | String TwoBoneIK3D::get_end_bone_name(int p_index) const { |
| 297 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), String()); |
| 298 | return tb_settings[p_index]->end_bone.name; |
| 299 | } |
| 300 | |
| 301 | void TwoBoneIK3D::set_end_bone(int p_index, int p_bone) { |
| 302 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 303 | bool changed = tb_settings[p_index]->end_bone.bone != p_bone; |
| 304 | tb_settings[p_index]->end_bone.bone = p_bone; |
| 305 | Skeleton3D *sk = get_skeleton(); |
| 306 | if (sk) { |
| 307 | if (tb_settings[p_index]->end_bone.bone <= -1 || tb_settings[p_index]->end_bone.bone >= sk->get_bone_count()) { |
| 308 | WARN_PRINT("End bone index out of range!"); |
| 309 | tb_settings[p_index]->end_bone.bone = -1; |
| 310 | } else { |
| 311 | tb_settings[p_index]->end_bone.name = sk->get_bone_name(tb_settings[p_index]->end_bone.bone); |
| 312 | } |
| 313 | } |
| 314 | if (changed) { |
| 315 | _update_joints(p_index); |
| 316 | } |
| 317 | notify_property_list_changed(); |
| 318 | } |
| 319 | |
| 320 | int TwoBoneIK3D::get_end_bone(int p_index) const { |
| 321 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), -1); |
| 322 | return tb_settings[p_index]->get_end_bone(); |
| 323 | } |
| 324 | |
| 325 | void TwoBoneIK3D::set_use_virtual_end(int p_index, bool p_enabled) { |
| 326 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 327 | bool changed = tb_settings[p_index]->use_virtual_end != p_enabled; |
| 328 | tb_settings[p_index]->use_virtual_end = p_enabled; |
| 329 | if (p_enabled) { |
| 330 | // To sync inspector. |
| 331 | tb_settings[p_index]->extend_end_bone = true; |
| 332 | } |
| 333 | tb_settings[p_index]->simulation_dirty = true; |
| 334 | if (changed) { |
| 335 | _update_joints(p_index); |
| 336 | } |
| 337 | notify_property_list_changed(); |
| 338 | } |
| 339 | |
| 340 | bool TwoBoneIK3D::is_using_virtual_end(int p_index) const { |
| 341 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), false); |
| 342 | return tb_settings[p_index]->use_virtual_end; |
| 343 | } |
| 344 | |
| 345 | void TwoBoneIK3D::set_extend_end_bone(int p_index, bool p_enabled) { |
| 346 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 347 | tb_settings[p_index]->extend_end_bone = p_enabled; |
| 348 | tb_settings[p_index]->simulation_dirty = true; |
| 349 | Skeleton3D *sk = get_skeleton(); |
| 350 | if (sk) { |
| 351 | _validate_pole_direction(sk, p_index); |
| 352 | } |
| 353 | notify_property_list_changed(); |
| 354 | #ifdef TOOLS_ENABLED |
| 355 | _make_gizmo_dirty(); |
| 356 | #endif // TOOLS_ENABLED |
| 357 | } |
| 358 | |
| 359 | bool TwoBoneIK3D::is_end_bone_extended(int p_index) const { |
| 360 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), false); |
| 361 | return tb_settings[p_index]->extend_end_bone; |
| 362 | } |
| 363 | |
| 364 | void TwoBoneIK3D::set_end_bone_direction(int p_index, BoneDirection p_bone_direction) { |
| 365 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 366 | tb_settings[p_index]->end_bone_direction = p_bone_direction; |
| 367 | Skeleton3D *sk = get_skeleton(); |
| 368 | if (sk) { |
| 369 | _validate_pole_direction(sk, p_index); |
| 370 | } |
| 371 | #ifdef TOOLS_ENABLED |
| 372 | _make_gizmo_dirty(); |
| 373 | #endif // TOOLS_ENABLED |
| 374 | if (mutable_bone_axes) { |
| 375 | return; // Chain dir will be recaluclated in _update_bone_axis(). |
| 376 | } |
| 377 | tb_settings[p_index]->simulation_dirty = true; |
| 378 | } |
| 379 | |
| 380 | SkeletonModifier3D::BoneDirection TwoBoneIK3D::get_end_bone_direction(int p_index) const { |
| 381 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), BONE_DIRECTION_FROM_PARENT); |
| 382 | return tb_settings[p_index]->end_bone_direction; |
| 383 | } |
| 384 | |
| 385 | void TwoBoneIK3D::set_end_bone_length(int p_index, float p_length) { |
| 386 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 387 | float old = tb_settings[p_index]->end_bone_length; |
| 388 | tb_settings[p_index]->end_bone_length = p_length; |
| 389 | #ifdef TOOLS_ENABLED |
| 390 | _make_gizmo_dirty(); |
| 391 | #endif // TOOLS_ENABLED |
| 392 | if (mutable_bone_axes && Math::is_zero_approx(old) == Math::is_zero_approx(p_length)) { |
| 393 | return; // If chain size is not changed, length will be recaluclated in _update_bone_axis(). |
| 394 | } |
| 395 | tb_settings[p_index]->simulation_dirty = true; |
| 396 | } |
| 397 | |
| 398 | float TwoBoneIK3D::get_end_bone_length(int p_index) const { |
| 399 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), 0); |
| 400 | return tb_settings[p_index]->end_bone_length; |
| 401 | } |
| 402 | |
| 403 | void TwoBoneIK3D::set_target_node(int p_index, const NodePath &p_node_path) { |
| 404 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 405 | tb_settings[p_index]->target_node = p_node_path; |
| 406 | update_configuration_warnings(); |
| 407 | } |
| 408 | |
| 409 | NodePath TwoBoneIK3D::get_target_node(int p_index) const { |
| 410 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), NodePath()); |
| 411 | return tb_settings[p_index]->target_node; |
| 412 | } |
| 413 | |
| 414 | void TwoBoneIK3D::set_pole_node(int p_index, const NodePath &p_node_path) { |
| 415 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 416 | tb_settings[p_index]->pole_node = p_node_path; |
| 417 | update_configuration_warnings(); |
| 418 | } |
| 419 | |
| 420 | NodePath TwoBoneIK3D::get_pole_node(int p_index) const { |
| 421 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), NodePath()); |
| 422 | return tb_settings[p_index]->pole_node; |
| 423 | } |
| 424 | |
| 425 | void TwoBoneIK3D::set_pole_direction(int p_index, SecondaryDirection p_direction) { |
| 426 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 427 | tb_settings[p_index]->pole_direction = p_direction; |
| 428 | tb_settings[p_index]->simulation_dirty = true; |
| 429 | Skeleton3D *sk = get_skeleton(); |
| 430 | if (sk) { |
| 431 | _validate_pole_direction(sk, p_index); |
| 432 | } |
| 433 | notify_property_list_changed(); |
| 434 | #ifdef TOOLS_ENABLED |
| 435 | _make_gizmo_dirty(); |
| 436 | #endif // TOOLS_ENABLED |
| 437 | } |
| 438 | |
| 439 | SkeletonModifier3D::SecondaryDirection TwoBoneIK3D::get_pole_direction(int p_index) const { |
| 440 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), SECONDARY_DIRECTION_NONE); |
| 441 | return tb_settings[p_index]->pole_direction; |
| 442 | } |
| 443 | |
| 444 | void TwoBoneIK3D::set_pole_direction_vector(int p_index, const Vector3 &p_vector) { |
| 445 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 446 | if (tb_settings[p_index]->pole_direction != SECONDARY_DIRECTION_CUSTOM) { |
| 447 | return; |
| 448 | } |
| 449 | tb_settings[p_index]->pole_direction_vector = p_vector; |
| 450 | tb_settings[p_index]->simulation_dirty = true; |
| 451 | Skeleton3D *sk = get_skeleton(); |
| 452 | if (sk) { |
| 453 | _validate_pole_direction(sk, p_index); |
| 454 | } |
| 455 | #ifdef TOOLS_ENABLED |
| 456 | _make_gizmo_dirty(); |
| 457 | #endif // TOOLS_ENABLED |
| 458 | } |
| 459 | |
| 460 | Vector3 TwoBoneIK3D::get_pole_direction_vector(int p_index) const { |
| 461 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), Vector3()); |
| 462 | return tb_settings[p_index]->get_pole_direction_vector(); |
| 463 | } |
| 464 | |
| 465 | bool TwoBoneIK3D::is_valid(int p_index) const { |
| 466 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), false); |
| 467 | return tb_settings[p_index]->root_bone.bone != -1 && tb_settings[p_index]->middle_bone.bone != -1 && tb_settings[p_index]->is_end_valid(); |
| 468 | } |
| 469 | |
| 470 | void TwoBoneIK3D::_bind_methods() { |
| 471 | // Setting. |
| 472 | ClassDB::bind_method(D_METHOD("set_target_node", "index", "target_node"), &TwoBoneIK3D::set_target_node); |
| 473 | ClassDB::bind_method(D_METHOD("get_target_node", "index"), &TwoBoneIK3D::get_target_node); |
| 474 | ClassDB::bind_method(D_METHOD("set_pole_node", "index", "pole_node"), &TwoBoneIK3D::set_pole_node); |
| 475 | ClassDB::bind_method(D_METHOD("get_pole_node", "index"), &TwoBoneIK3D::get_pole_node); |
| 476 | |
| 477 | ClassDB::bind_method(D_METHOD("set_root_bone_name", "index", "bone_name"), &TwoBoneIK3D::set_root_bone_name); |
| 478 | ClassDB::bind_method(D_METHOD("get_root_bone_name", "index"), &TwoBoneIK3D::get_root_bone_name); |
| 479 | ClassDB::bind_method(D_METHOD("set_root_bone", "index", "bone"), &TwoBoneIK3D::set_root_bone); |
| 480 | ClassDB::bind_method(D_METHOD("get_root_bone", "index"), &TwoBoneIK3D::get_root_bone); |
| 481 | |
| 482 | ClassDB::bind_method(D_METHOD("set_middle_bone_name", "index", "bone_name"), &TwoBoneIK3D::set_middle_bone_name); |
| 483 | ClassDB::bind_method(D_METHOD("get_middle_bone_name", "index"), &TwoBoneIK3D::get_middle_bone_name); |
| 484 | ClassDB::bind_method(D_METHOD("set_middle_bone", "index", "bone"), &TwoBoneIK3D::set_middle_bone); |
| 485 | ClassDB::bind_method(D_METHOD("get_middle_bone", "index"), &TwoBoneIK3D::get_middle_bone); |
| 486 | |
| 487 | ClassDB::bind_method(D_METHOD("set_pole_direction", "index", "direction"), &TwoBoneIK3D::set_pole_direction); |
| 488 | ClassDB::bind_method(D_METHOD("get_pole_direction", "index"), &TwoBoneIK3D::get_pole_direction); |
| 489 | ClassDB::bind_method(D_METHOD("set_pole_direction_vector", "index", "vector"), &TwoBoneIK3D::set_pole_direction_vector); |
| 490 | ClassDB::bind_method(D_METHOD("get_pole_direction_vector", "index"), &TwoBoneIK3D::get_pole_direction_vector); |
| 491 | |
| 492 | ClassDB::bind_method(D_METHOD("set_end_bone_name", "index", "bone_name"), &TwoBoneIK3D::set_end_bone_name); |
| 493 | ClassDB::bind_method(D_METHOD("get_end_bone_name", "index"), &TwoBoneIK3D::get_end_bone_name); |
| 494 | ClassDB::bind_method(D_METHOD("set_end_bone", "index", "bone"), &TwoBoneIK3D::set_end_bone); |
| 495 | ClassDB::bind_method(D_METHOD("get_end_bone", "index"), &TwoBoneIK3D::get_end_bone); |
| 496 | |
| 497 | ClassDB::bind_method(D_METHOD("set_use_virtual_end", "index", "enabled"), &TwoBoneIK3D::set_use_virtual_end); |
| 498 | ClassDB::bind_method(D_METHOD("is_using_virtual_end", "index"), &TwoBoneIK3D::is_using_virtual_end); |
| 499 | ClassDB::bind_method(D_METHOD("set_extend_end_bone", "index", "enabled"), &TwoBoneIK3D::set_extend_end_bone); |
| 500 | ClassDB::bind_method(D_METHOD("is_end_bone_extended", "index"), &TwoBoneIK3D::is_end_bone_extended); |
| 501 | ClassDB::bind_method(D_METHOD("set_end_bone_direction", "index", "bone_direction"), &TwoBoneIK3D::set_end_bone_direction); |
| 502 | ClassDB::bind_method(D_METHOD("get_end_bone_direction", "index"), &TwoBoneIK3D::get_end_bone_direction); |
| 503 | ClassDB::bind_method(D_METHOD("set_end_bone_length", "index", "length"), &TwoBoneIK3D::set_end_bone_length); |
| 504 | ClassDB::bind_method(D_METHOD("get_end_bone_length", "index"), &TwoBoneIK3D::get_end_bone_length); |
| 505 | |
| 506 | ADD_ARRAY_COUNT("Settings", "setting_count", "set_setting_count", "get_setting_count", "settings/"); |
| 507 | } |
| 508 | |
| 509 | void TwoBoneIK3D::_validate_bone_names() { |
| 510 | for (uint32_t i = 0; i < settings.size(); i++) { |
| 511 | // Prior bone name. |
| 512 | if (!tb_settings[i]->root_bone.name.is_empty()) { |
| 513 | set_root_bone_name(i, tb_settings[i]->root_bone.name); |
| 514 | } else if (tb_settings[i]->root_bone.bone != -1) { |
| 515 | set_root_bone(i, tb_settings[i]->root_bone.bone); |
| 516 | } |
| 517 | // Prior bone name. |
| 518 | if (!tb_settings[i]->middle_bone.name.is_empty()) { |
| 519 | set_middle_bone_name(i, tb_settings[i]->middle_bone.name); |
| 520 | } else if (tb_settings[i]->middle_bone.bone != -1) { |
| 521 | set_middle_bone(i, tb_settings[i]->middle_bone.bone); |
| 522 | } |
| 523 | // Prior bone name. |
| 524 | if (!tb_settings[i]->end_bone.name.is_empty()) { |
| 525 | set_end_bone_name(i, tb_settings[i]->end_bone.name); |
| 526 | } else if (tb_settings[i]->end_bone.bone != -1) { |
| 527 | set_end_bone(i, tb_settings[i]->end_bone.bone); |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | void TwoBoneIK3D::_validate_pole_directions(Skeleton3D *p_skeleton) const { |
| 533 | for (uint32_t i = 0; i < settings.size(); i++) { |
| 534 | _validate_pole_direction(p_skeleton, i); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | void TwoBoneIK3D::_validate_pole_direction(Skeleton3D *p_skeleton, int p_index) const { |
| 539 | TwoBoneIK3DSetting *setting = tb_settings[p_index]; |
| 540 | SecondaryDirection dir = setting->pole_direction; |
| 541 | if (!is_valid(p_index) || dir == SECONDARY_DIRECTION_NONE) { |
| 542 | return; |
| 543 | } |
| 544 | Vector3 kv = get_pole_direction_vector(p_index).normalized(); |
| 545 | Vector3 fwd; |
| 546 | |
| 547 | // End bone. |
| 548 | int valid_end_bone = setting->get_end_bone(); |
| 549 | Vector3 axis = IKModifier3D::get_bone_axis(p_skeleton, valid_end_bone, setting->end_bone_direction, mutable_bone_axes); |
| 550 | Vector3 global_rest_origin; |
| 551 | if (setting->extend_end_bone && setting->end_bone_length > 0 && !axis.is_zero_approx()) { |
| 552 | global_rest_origin = p_skeleton->get_bone_global_rest(valid_end_bone).xform(axis * setting->end_bone_length); |
| 553 | } else { |
| 554 | // Shouldn't be using virtual end. |
| 555 | global_rest_origin = p_skeleton->get_bone_global_rest(valid_end_bone).origin; |
| 556 | } |
| 557 | |
| 558 | // Middle bone. |
| 559 | axis = global_rest_origin - p_skeleton->get_bone_global_rest(setting->middle_bone.bone).origin; |
| 560 | if (!axis.is_zero_approx()) { |
| 561 | fwd = p_skeleton->get_bone_global_rest(setting->middle_bone.bone).basis.get_rotation_quaternion().xform_inv(axis).normalized(); |
| 562 | } else { |
| 563 | return; |
| 564 | } |
| 565 | |
| 566 | if (Math::is_equal_approx(Math::abs(kv.dot(fwd)), 1)) { |
| 567 | WARN_PRINT_ED("Setting: " + itos(p_index) + ": Pole direction and forward vector are colinear. This is not advised as it may cause unwanted rotation."); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | void TwoBoneIK3D::_make_all_joints_dirty() { |
| 572 | for (uint32_t i = 0; i < settings.size(); i++) { |
| 573 | _update_joints(i); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | void TwoBoneIK3D::_init_joints(Skeleton3D *p_skeleton, int p_index) { |
| 578 | TwoBoneIK3DSetting *setting = tb_settings[p_index]; |
| 579 | cached_space = p_skeleton->get_global_transform_interpolated(); |
| 580 | if (!setting->simulation_dirty) { |
| 581 | if (mutable_bone_axes) { |
| 582 | _update_bone_axis(p_skeleton, p_index); |
| 583 | } |
| 584 | return; |
| 585 | } |
| 586 | _clear_joints(p_index); |
| 587 | if (setting->root_bone.bone == -1 || setting->middle_bone.bone == -1 || !setting->is_end_valid()) { |
| 588 | return; |
| 589 | } |
| 590 | _update_bone_axis(p_skeleton, p_index); |
| 591 | setting->simulation_dirty = false; |
| 592 | } |
| 593 | |
| 594 | void TwoBoneIK3D::_clear_joints(int p_index) { |
| 595 | TwoBoneIK3DSetting *setting = tb_settings[p_index]; |
| 596 | if (!setting) { |
| 597 | return; |
| 598 | } |
| 599 | setting->root_pos = Vector3(); |
| 600 | setting->mid_pos = Vector3(); |
| 601 | setting->end_pos = Vector3(); |
| 602 | if (setting->root_joint_solver_info) { |
| 603 | memdelete(setting->root_joint_solver_info); |
| 604 | setting->root_joint_solver_info = nullptr; |
| 605 | } |
| 606 | if (setting->mid_joint_solver_info) { |
| 607 | memdelete(setting->mid_joint_solver_info); |
| 608 | setting->mid_joint_solver_info = nullptr; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | void TwoBoneIK3D::_update_bone_axis(Skeleton3D *p_skeleton, int p_index) { |
| 613 | TwoBoneIK3DSetting *setting = tb_settings[p_index]; |
| 614 | if (!setting) { |
| 615 | return; |
| 616 | } |
| 617 | if (!setting->mid_joint_solver_info) { |
| 618 | setting->mid_joint_solver_info = memnew(IKModifier3DSolverInfo); |
| 619 | } |
| 620 | if (!setting->root_joint_solver_info) { |
| 621 | setting->root_joint_solver_info = memnew(IKModifier3DSolverInfo); |
| 622 | } |
| 623 | #ifdef TOOLS_ENABLED |
| 624 | Vector3 old_mv = setting->mid_joint_solver_info->forward_vector; |
| 625 | float old_ml = setting->mid_joint_solver_info->length; |
| 626 | Vector3 old_rv = setting->root_joint_solver_info->forward_vector; |
| 627 | float old_rl = setting->root_joint_solver_info->length; |
| 628 | #endif // TOOLS_ENABLED |
| 629 | |
| 630 | // End bone. |
| 631 | int valid_end_bone = setting->get_end_bone(); |
| 632 | Vector3 axis = IKModifier3D::get_bone_axis(p_skeleton, valid_end_bone, setting->end_bone_direction, mutable_bone_axes); |
| 633 | Vector3 global_rest_origin; |
| 634 | if (setting->extend_end_bone && setting->end_bone_length > 0 && !axis.is_zero_approx()) { |
| 635 | setting->end_pos = p_skeleton->get_bone_global_pose(valid_end_bone).xform(axis * setting->end_bone_length); |
| 636 | global_rest_origin = _get_bone_global_rest(p_skeleton, valid_end_bone, setting->root_bone.bone).xform(axis * setting->end_bone_length); |
| 637 | } else { |
| 638 | // Shouldn't be using virtual end. |
| 639 | setting->end_pos = p_skeleton->get_bone_global_pose(valid_end_bone).origin; |
| 640 | global_rest_origin = _get_bone_global_rest(p_skeleton, valid_end_bone, setting->root_bone.bone).origin; |
| 641 | } |
| 642 | |
| 643 | // Middle bone. |
| 644 | Vector3 orig = _get_bone_global_rest(p_skeleton, setting->middle_bone.bone, setting->root_bone.bone).origin; |
| 645 | axis = global_rest_origin - orig; |
| 646 | global_rest_origin = orig; |
| 647 | if (!axis.is_zero_approx()) { |
| 648 | setting->mid_pos = p_skeleton->get_bone_global_pose(setting->middle_bone.bone).origin; |
| 649 | setting->mid_joint_solver_info->forward_vector = p_skeleton->get_bone_global_rest(setting->middle_bone.bone).basis.get_rotation_quaternion().xform_inv(axis).normalized(); |
| 650 | setting->mid_joint_solver_info->length = axis.length(); |
| 651 | } else { |
| 652 | _clear_joints(p_index); |
| 653 | return; |
| 654 | } |
| 655 | |
| 656 | // Root bone. |
| 657 | orig = _get_bone_global_rest(p_skeleton, setting->root_bone.bone, setting->root_bone.bone).origin; |
| 658 | axis = global_rest_origin - orig; |
| 659 | if (!axis.is_zero_approx()) { |
| 660 | setting->root_pos = p_skeleton->get_bone_global_pose(setting->root_bone.bone).origin; |
| 661 | setting->root_joint_solver_info->forward_vector = p_skeleton->get_bone_global_rest(setting->root_bone.bone).basis.get_rotation_quaternion().xform_inv(axis).normalized(); |
| 662 | setting->root_joint_solver_info->length = axis.length(); |
| 663 | } else { |
| 664 | _clear_joints(p_index); |
| 665 | return; |
| 666 | } |
| 667 | |
| 668 | setting->init_current_joint_rotations(p_skeleton); |
| 669 | |
| 670 | double total_length = setting->root_joint_solver_info->length + setting->mid_joint_solver_info->length; |
| 671 | setting->cached_length_sq = total_length * total_length; |
| 672 | #ifdef TOOLS_ENABLED |
| 673 | if (!Math::is_equal_approx(old_ml, setting->mid_joint_solver_info->length) || !Math::is_equal_approx(old_rl, setting->root_joint_solver_info->length) || !old_mv.is_equal_approx(setting->mid_joint_solver_info->forward_vector) || !old_rv.is_equal_approx(setting->root_joint_solver_info->forward_vector)) { |
| 674 | _make_gizmo_dirty(); |
| 675 | } |
| 676 | #endif // TOOLS_ENABLED |
| 677 | } |
| 678 | |
| 679 | void TwoBoneIK3D::_update_joints(int p_index) { |
| 680 | tb_settings[p_index]->simulation_dirty = true; |
| 681 | |
| 682 | #ifdef TOOLS_ENABLED |
| 683 | _make_gizmo_dirty(); // To clear invalid setting. |
| 684 | #endif // TOOLS_ENABLED |
| 685 | |
| 686 | Skeleton3D *sk = get_skeleton(); |
| 687 | if (!sk || tb_settings[p_index]->root_bone.bone == -1 || tb_settings[p_index]->middle_bone.bone == -1 || !tb_settings[p_index]->is_end_valid()) { |
| 688 | return; |
| 689 | } |
| 690 | |
| 691 | // Validation for middle bone. |
| 692 | int parent_bone = tb_settings[p_index]->root_bone.bone; |
| 693 | int current_bone = tb_settings[p_index]->middle_bone.bone; |
| 694 | bool valid = false; |
| 695 | while (current_bone >= 0) { |
| 696 | if (current_bone == parent_bone) { |
| 697 | valid = true; |
| 698 | break; |
| 699 | } |
| 700 | current_bone = sk->get_bone_parent(current_bone); |
| 701 | } |
| 702 | ERR_FAIL_COND_EDMSG(!valid, "The middle bone must be a child of the root bone."); |
| 703 | |
| 704 | // Validation for end bone. |
| 705 | if (!tb_settings[p_index]->use_virtual_end) { |
| 706 | parent_bone = tb_settings[p_index]->middle_bone.bone; |
| 707 | current_bone = tb_settings[p_index]->end_bone.bone; |
| 708 | valid = false; |
| 709 | while (current_bone >= 0) { |
| 710 | if (current_bone == parent_bone) { |
| 711 | valid = true; |
| 712 | break; |
| 713 | } |
| 714 | current_bone = sk->get_bone_parent(current_bone); |
| 715 | } |
| 716 | ERR_FAIL_COND_EDMSG(!valid, "The end bone must be a child of the middle bone."); |
| 717 | } |
| 718 | |
| 719 | if (sk) { |
| 720 | _validate_pole_directions(sk); |
| 721 | } |
| 722 | |
| 723 | if (mutable_bone_axes) { |
| 724 | _update_bone_axis(sk, p_index); |
| 725 | #ifdef TOOLS_ENABLED |
| 726 | } else { |
| 727 | _make_gizmo_dirty(); |
| 728 | #endif // TOOLS_ENABLED |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | Transform3D TwoBoneIK3D::_get_bone_global_rest(Skeleton3D *p_skeleton, int p_bone, int p_root) const { |
| 733 | if (!mutable_bone_axes) { |
| 734 | return p_skeleton->get_bone_global_rest(p_bone); |
| 735 | } |
| 736 | Transform3D tr = p_skeleton->get_bone_global_rest(p_root); |
| 737 | tr.origin = tr.origin - p_skeleton->get_bone_rest(p_root).origin + p_skeleton->get_bone_pose(p_root).origin; |
| 738 | LocalVector<int> path; |
| 739 | for (int bone = p_bone; bone != p_root && bone != -1; bone = p_skeleton->get_bone_parent(bone)) { |
| 740 | path.push_back(bone); |
| 741 | } |
| 742 | path.reverse(); |
| 743 | for (int bone : path) { |
| 744 | tr = tr * Transform3D(p_skeleton->get_bone_rest(bone).basis, p_skeleton->get_bone_pose(bone).origin); |
| 745 | } |
| 746 | return tr; |
| 747 | } |
| 748 | |
| 749 | void TwoBoneIK3D::_make_simulation_dirty(int p_index) { |
| 750 | TwoBoneIK3DSetting *setting = tb_settings[p_index]; |
| 751 | if (!setting) { |
| 752 | return; |
| 753 | } |
| 754 | setting->simulation_dirty = true; |
| 755 | } |
| 756 | |
| 757 | void TwoBoneIK3D::_process_ik(Skeleton3D *p_skeleton, double p_delta) { |
| 758 | for (uint32_t i = 0; i < settings.size(); i++) { |
| 759 | _init_joints(p_skeleton, i); |
| 760 | Node3D *target = Object::cast_to<Node3D>(get_node_or_null(tb_settings[i]->target_node)); |
| 761 | Node3D *pole = Object::cast_to<Node3D>(get_node_or_null(tb_settings[i]->pole_node)); |
| 762 | if (!target || !pole || !tb_settings[i]->is_valid()) { |
| 763 | continue; // Abort. |
| 764 | } |
| 765 | Vector3 destination = cached_space.affine_inverse().xform(target->get_global_transform_interpolated().origin); |
| 766 | Vector3 pole_destination = cached_space.affine_inverse().xform(pole->get_global_transform_interpolated().origin); |
| 767 | tb_settings[i]->cache_current_joint_rotations(p_skeleton, pole_destination); // Iterate over first to detect parent (outside of the chain) bone pose changes. |
| 768 | _process_joints(p_delta, p_skeleton, tb_settings[i], destination, pole_destination); |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | void TwoBoneIK3D::_process_joints(double p_delta, Skeleton3D *p_skeleton, TwoBoneIK3DSetting *p_setting, const Vector3 &p_destination, const Vector3 &p_pole_destination) { |
| 773 | Vector3 destination = p_destination; |
| 774 | |
| 775 | // Make vector from root to destination. |
| 776 | p_setting->root_pos = p_skeleton->get_bone_global_pose(p_setting->root_bone.bone).origin; // New root position. |
| 777 | Vector3 root_to_destination = destination - p_setting->root_pos; |
| 778 | if (root_to_destination.is_zero_approx()) { |
| 779 | return; // Abort. |
| 780 | } |
| 781 | |
| 782 | double rd_len_sq = root_to_destination.length_squared(); |
| 783 | // Compare the distance to the target with the length of the bones. |
| 784 | if (rd_len_sq >= p_setting->cached_length_sq) { |
| 785 | // Result is straight. |
| 786 | Vector3 rd_nrm = root_to_destination.normalized(); |
| 787 | p_setting->mid_pos = p_setting->root_pos + rd_nrm * p_setting->root_joint_solver_info->length; |
| 788 | p_setting->end_pos = p_setting->mid_pos + rd_nrm * p_setting->mid_joint_solver_info->length; |
| 789 | } else { |
| 790 | // Check if the target can be reached by subtracting the lengths of the bones. |
| 791 | // If not, push out target to normal of the root bone sphere. |
| 792 | double sub = p_setting->root_joint_solver_info->length - p_setting->mid_joint_solver_info->length; |
| 793 | if (rd_len_sq < sub * sub) { |
| 794 | Vector3 push_nrm = (destination - p_setting->root_pos).normalized(); |
| 795 | destination = p_setting->root_pos + push_nrm * Math::abs(sub); |
| 796 | root_to_destination = destination - p_setting->root_pos; |
| 797 | } |
| 798 | |
| 799 | // End is snapped to the target. |
| 800 | p_setting->end_pos = destination; |
| 801 | |
| 802 | // Result is bent, determine the mid position to respect the pole target. |
| 803 | // Mid-position should be a point of intersection of two circles. |
| 804 | double l_chain = root_to_destination.length(); |
| 805 | Vector3 u = root_to_destination.normalized(); |
| 806 | Vector3 pole_vec = get_projected_normal(p_setting->root_pos, p_setting->end_pos, p_pole_destination); |
| 807 | |
| 808 | // Circle1: center is the root, radius is the length of the root bone. |
| 809 | double r_root = p_setting->root_joint_solver_info->length; |
| 810 | // Circle2: center is the target, radius is the length of the middle bone. |
| 811 | double r_mid = p_setting->mid_joint_solver_info->length; |
| 812 | |
| 813 | double a = (l_chain * l_chain + r_root * r_root - r_mid * r_mid) / (2.0 * l_chain); |
| 814 | double h2 = r_root * r_root - a * a; |
| 815 | if (h2 < 0) { |
| 816 | h2 = 0; |
| 817 | } |
| 818 | double h = Math::sqrt(h2); |
| 819 | |
| 820 | Vector3 det_plus = (p_setting->root_pos + u * a) + pole_vec * h; |
| 821 | Vector3 det_minus = (p_setting->root_pos + u * a) - pole_vec * h; |
| 822 | |
| 823 | // Pick the intersection that is closest to the pole target. |
| 824 | p_setting->mid_pos = p_pole_destination.distance_squared_to(det_plus) < p_pole_destination.distance_squared_to(det_minus) ? det_plus : det_minus; |
| 825 | } |
| 826 | |
| 827 | // Update virtual bone rest/poses. |
| 828 | p_setting->cache_current_vectors(p_skeleton); |
| 829 | p_setting->cache_current_joint_rotations(p_skeleton, p_pole_destination); |
| 830 | |
| 831 | // Apply the virtual bone rest/poses to the actual bones. |
| 832 | p_skeleton->set_bone_pose_rotation(p_setting->root_bone.bone, p_setting->root_joint_solver_info->current_lpose); |
| 833 | // Mid joint pose is relative to the root joint pose for the case root-mid or mid-end have more than 1 joints. |
| 834 | p_skeleton->set_bone_pose_rotation(p_setting->middle_bone.bone, get_local_pose_rotation(p_skeleton, p_setting->middle_bone.bone, p_setting->mid_joint_solver_info->current_gpose)); |
| 835 | } |
| 836 | |
| 837 | #ifdef TOOLS_ENABLED |
| 838 | Vector3 TwoBoneIK3D::get_root_bone_vector(int p_index) const { |
| 839 | Skeleton3D *skeleton = get_skeleton(); |
| 840 | if (!skeleton) { |
| 841 | return Vector3(); |
| 842 | } |
| 843 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), Vector3()); |
| 844 | TwoBoneIK3DSetting *setting = tb_settings[p_index]; |
| 845 | if (!setting) { |
| 846 | return Vector3(); |
| 847 | } |
| 848 | if (!setting->root_joint_solver_info) { |
| 849 | return _get_bone_global_rest(skeleton, setting->middle_bone.bone, setting->root_bone.bone).origin - _get_bone_global_rest(skeleton, setting->root_bone.bone, setting->root_bone.bone).origin; |
| 850 | } |
| 851 | return setting->root_joint_solver_info->forward_vector * setting->root_joint_solver_info->length; |
| 852 | } |
| 853 | |
| 854 | Vector3 TwoBoneIK3D::get_middle_bone_vector(int p_index) const { |
| 855 | Skeleton3D *skeleton = get_skeleton(); |
| 856 | if (!skeleton) { |
| 857 | return Vector3(); |
| 858 | } |
| 859 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), Vector3()); |
| 860 | TwoBoneIK3DSetting *setting = tb_settings[p_index]; |
| 861 | if (!setting) { |
| 862 | return Vector3(); |
| 863 | } |
| 864 | if (!setting->mid_joint_solver_info) { |
| 865 | int valid_end_bone = setting->get_end_bone(); |
| 866 | Vector3 axis = IKModifier3D::get_bone_axis(skeleton, valid_end_bone, setting->end_bone_direction, mutable_bone_axes); |
| 867 | Vector3 global_rest_origin; |
| 868 | if (setting->extend_end_bone && setting->end_bone_length > 0 && !axis.is_zero_approx()) { |
| 869 | global_rest_origin = _get_bone_global_rest(skeleton, valid_end_bone, setting->root_bone.bone).xform(axis * setting->end_bone_length); |
| 870 | } else { |
| 871 | // Shouldn't be using virtual end. |
| 872 | global_rest_origin = _get_bone_global_rest(skeleton, valid_end_bone, setting->root_bone.bone).origin; |
| 873 | } |
| 874 | return global_rest_origin - _get_bone_global_rest(skeleton, setting->middle_bone.bone, setting->root_bone.bone).origin; |
| 875 | } |
| 876 | return setting->mid_joint_solver_info->forward_vector * setting->mid_joint_solver_info->length; |
| 877 | } |
| 878 | #endif // TOOLS_ENABLED |
| 879 | |
| 880 | TwoBoneIK3D::~TwoBoneIK3D() { |
| 881 | clear_settings(); |
| 882 | } |