/**************************************************************************/1/* jacobian_ik_3d.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "jacobian_ik_3d.h"3132void JacobianIK3D::_solve_iteration(double p_delta, Skeleton3D *p_skeleton, IterateIK3DSetting *p_setting, const Vector3 &p_destination) {33int joint_size = (int)p_setting->joints.size();34int chain_size = (int)p_setting->chain.size();3536// Forwards.37for (int i = 0; i < joint_size; i++) {38IKModifier3DSolverInfo *solver_info = p_setting->solver_info_list[i];39if (!solver_info || Math::is_zero_approx(solver_info->length)) {40continue;41}4243int HEAD = i;44int TAIL = i + 1;4546Vector3 current_head = p_setting->chain[HEAD];47Vector3 current_effector = p_setting->chain[chain_size - 1];48Vector3 head_to_effector = current_effector - current_head;49Vector3 effector_to_destination = p_destination - current_effector;50Vector3 axis = head_to_effector.cross(effector_to_destination);5152if (Math::is_zero_approx(axis.length_squared())) {53continue;54}5556Quaternion 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.5758for (int j = TAIL; j < chain_size; j++) {59Vector3 to_tail = p_setting->chain[j] - current_head;60p_setting->update_chain_coordinate_fw(p_skeleton, j, current_head + to_rot.xform(to_tail));6162int k = j - 1;63if (p_setting->joint_settings[k]->rotation_axis != ROTATION_AXIS_ALL) {64p_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]));65}66if (p_setting->joint_settings[k]->limitation.is_valid()) {67p_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));68}69}70}71}727374