| 1 | /**************************************************************************/ |
| 2 | /* jacobian_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 "jacobian_ik_3d.h" |
| 32 | |
| 33 | void JacobianIK3D::_solve_iteration(double p_delta, Skeleton3D *p_skeleton, IterateIK3DSetting *p_setting, const Vector3 &p_destination) { |
| 34 | int joint_size = (int)p_setting->joints.size(); |
| 35 | int chain_size = (int)p_setting->chain.size(); |
| 36 | |
| 37 | // Forwards. |
| 38 | for (int i = 0; i < joint_size; i++) { |
| 39 | IKModifier3DSolverInfo *solver_info = p_setting->solver_info_list[i]; |
| 40 | if (!solver_info || Math::is_zero_approx(solver_info->length)) { |
| 41 | continue; |
| 42 | } |
| 43 | |
| 44 | int HEAD = i; |
| 45 | int TAIL = i + 1; |
| 46 | |
| 47 | Vector3 current_head = p_setting->chain[HEAD]; |
| 48 | Vector3 current_effector = p_setting->chain[chain_size - 1]; |
| 49 | Vector3 head_to_effector = current_effector - current_head; |
| 50 | Vector3 effector_to_destination = p_destination - current_effector; |
| 51 | Vector3 axis = head_to_effector.cross(effector_to_destination); |
| 52 | |
| 53 | if (Math::is_zero_approx(axis.length_squared())) { |
| 54 | continue; |
| 55 | } |
| 56 | |
| 57 | Quaternion to_rot = Quaternion(axis.normalized(), MIN(axis.length() / MAX(CMP_EPSILON, head_to_effector.length_squared()), angular_delta_limit)); // Clip by angular_delta_limit for stability. |
| 58 | |
| 59 | for (int j = TAIL; j < chain_size; j++) { |
| 60 | Vector3 to_tail = p_setting->chain[j] - current_head; |
| 61 | p_setting->update_chain_coordinate_fw(p_skeleton, j, current_head + to_rot.xform(to_tail)); |
| 62 | |
| 63 | int k = j - 1; |
| 64 | if (p_setting->joint_settings[k]->rotation_axis != ROTATION_AXIS_ALL) { |
| 65 | p_setting->update_chain_coordinate_fw(p_skeleton, j, p_setting->chain[k] + p_setting->joint_settings[k]->get_projected_rotation(solver_info->current_grest, p_setting->chain[j] - p_setting->chain[k])); |
| 66 | } |
| 67 | if (p_setting->joint_settings[k]->limitation.is_valid()) { |
| 68 | p_setting->update_chain_coordinate_fw(p_skeleton, j, p_setting->chain[k] + p_setting->joint_settings[k]->get_limited_rotation(solver_info->current_grest, p_setting->chain[j] - p_setting->chain[k], solver_info->forward_vector)); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |