/**************************************************************************/1/* ccd_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 "ccd_ik_3d.h"3132void CCDIK3D::_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// Backwards.37for (int ancestor = joint_size - 1; ancestor >= 0; ancestor--) {38// Forwards.39for (int i = ancestor; i < joint_size; i++) {40IKModifier3DSolverInfo *solver_info = p_setting->solver_info_list[i];41if (!solver_info || Math::is_zero_approx(solver_info->length)) {42continue;43}4445int HEAD = i;46int TAIL = i + 1;4748Vector3 current_head = p_setting->chain[HEAD];49Vector3 current_effector = p_setting->chain[chain_size - 1];50Vector3 head_to_effector = current_effector - current_head;51Vector3 head_to_destination = p_destination - current_head;5253if (Math::is_zero_approx(head_to_destination.length_squared() * head_to_effector.length_squared())) {54continue;55}5657Quaternion to_rot = Quaternion(head_to_effector.normalized(), head_to_destination.normalized());58Vector3 to_tail = p_setting->chain[TAIL] - current_head;5960p_setting->update_chain_coordinate_fw(p_skeleton, TAIL, current_head + to_rot.xform(to_tail));6162if (p_setting->joint_settings[HEAD]->rotation_axis != ROTATION_AXIS_ALL) {63p_setting->update_chain_coordinate_fw(p_skeleton, TAIL, p_setting->chain[HEAD] + p_setting->joint_settings[HEAD]->get_projected_rotation(solver_info->current_grest, p_setting->chain[TAIL] - p_setting->chain[HEAD]));64}65if (p_setting->joint_settings[HEAD]->limitation.is_valid()) {66p_setting->update_chain_coordinate_fw(p_skeleton, TAIL, p_setting->chain[HEAD] + p_setting->joint_settings[HEAD]->get_limited_rotation(solver_info->current_grest, p_setting->chain[TAIL] - p_setting->chain[HEAD], solver_info->forward_vector));67}68}69}70}717273