Path: blob/master/scene/resources/2d/skeleton/skeleton_modification_2d_twoboneik.cpp
9903 views
/**************************************************************************/1/* skeleton_modification_2d_twoboneik.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 "skeleton_modification_2d_twoboneik.h"31#include "scene/2d/skeleton_2d.h"3233#ifdef TOOLS_ENABLED34#include "editor/settings/editor_settings.h"35#endif // TOOLS_ENABLED3637bool SkeletonModification2DTwoBoneIK::_set(const StringName &p_path, const Variant &p_value) {38String path = p_path;3940if (path == "joint_one_bone_idx") {41set_joint_one_bone_idx(p_value);42} else if (path == "joint_one_bone2d_node") {43set_joint_one_bone2d_node(p_value);44} else if (path == "joint_two_bone_idx") {45set_joint_two_bone_idx(p_value);46} else if (path == "joint_two_bone2d_node") {47set_joint_two_bone2d_node(p_value);48}49#ifdef TOOLS_ENABLED50else if (path.begins_with("editor/draw_gizmo")) {51set_editor_draw_gizmo(p_value);52} else if (path.begins_with("editor/draw_min_max")) {53set_editor_draw_min_max(p_value);54}55#endif // TOOLS_ENABLED56else {57return false;58}5960return true;61}6263bool SkeletonModification2DTwoBoneIK::_get(const StringName &p_path, Variant &r_ret) const {64String path = p_path;6566if (path == "joint_one_bone_idx") {67r_ret = get_joint_one_bone_idx();68} else if (path == "joint_one_bone2d_node") {69r_ret = get_joint_one_bone2d_node();70} else if (path == "joint_two_bone_idx") {71r_ret = get_joint_two_bone_idx();72} else if (path == "joint_two_bone2d_node") {73r_ret = get_joint_two_bone2d_node();74}75#ifdef TOOLS_ENABLED76else if (path.begins_with("editor/draw_gizmo")) {77r_ret = get_editor_draw_gizmo();78} else if (path.begins_with("editor/draw_min_max")) {79r_ret = get_editor_draw_min_max();80}81#endif // TOOLS_ENABLED82else {83return false;84}8586return true;87}8889void SkeletonModification2DTwoBoneIK::_get_property_list(List<PropertyInfo> *p_list) const {90p_list->push_back(PropertyInfo(Variant::INT, "joint_one_bone_idx", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));91p_list->push_back(PropertyInfo(Variant::NODE_PATH, "joint_one_bone2d_node", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Bone2D", PROPERTY_USAGE_DEFAULT));9293p_list->push_back(PropertyInfo(Variant::INT, "joint_two_bone_idx", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));94p_list->push_back(PropertyInfo(Variant::NODE_PATH, "joint_two_bone2d_node", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Bone2D", PROPERTY_USAGE_DEFAULT));9596#ifdef TOOLS_ENABLED97if (Engine::get_singleton()->is_editor_hint()) {98p_list->push_back(PropertyInfo(Variant::BOOL, "editor/draw_gizmo", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));99p_list->push_back(PropertyInfo(Variant::BOOL, "editor/draw_min_max", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));100}101#endif // TOOLS_ENABLED102}103104void SkeletonModification2DTwoBoneIK::_execute(float p_delta) {105ERR_FAIL_COND_MSG(!stack || !is_setup || stack->skeleton == nullptr,106"Modification is not setup and therefore cannot execute!");107if (!enabled) {108return;109}110111if (target_node_cache.is_null()) {112WARN_PRINT_ONCE("Target cache is out of date. Attempting to update...");113update_target_cache();114return;115}116117if (joint_one_bone2d_node_cache.is_null() && !joint_one_bone2d_node.is_empty()) {118WARN_PRINT_ONCE("Joint one Bone2D node cache is out of date. Attempting to update...");119update_joint_one_bone2d_cache();120}121if (joint_two_bone2d_node_cache.is_null() && !joint_two_bone2d_node.is_empty()) {122WARN_PRINT_ONCE("Joint two Bone2D node cache is out of date. Attempting to update...");123update_joint_two_bone2d_cache();124}125126Node2D *target = ObjectDB::get_instance<Node2D>(target_node_cache);127if (!target || !target->is_inside_tree()) {128ERR_PRINT_ONCE("Target node is not in the scene tree. Cannot execute modification!");129return;130}131132Bone2D *joint_one_bone = stack->skeleton->get_bone(joint_one_bone_idx);133if (joint_one_bone == nullptr) {134ERR_PRINT_ONCE("Joint one bone_idx does not point to a valid bone! Cannot execute modification!");135return;136}137138Bone2D *joint_two_bone = stack->skeleton->get_bone(joint_two_bone_idx);139if (joint_two_bone == nullptr) {140ERR_PRINT_ONCE("Joint two bone_idx does not point to a valid bone! Cannot execute modification!");141return;142}143144// Adopted from the links below:145// http://theorangeduck.com/page/simple-two-joint146// https://www.alanzucconi.com/2018/05/02/ik-2d-2/147// With modifications by TwistedTwigleg148Vector2 target_difference = target->get_global_position() - joint_one_bone->get_global_position();149float joint_one_to_target = target_difference.length();150float angle_atan = target_difference.angle();151152float bone_one_length = joint_one_bone->get_length() * MIN(joint_one_bone->get_global_scale().x, joint_one_bone->get_global_scale().y);153float bone_two_length = joint_two_bone->get_length() * MIN(joint_two_bone->get_global_scale().x, joint_two_bone->get_global_scale().y);154bool override_angles_due_to_out_of_range = false;155156if (joint_one_to_target < target_minimum_distance) {157joint_one_to_target = target_minimum_distance;158}159if (joint_one_to_target > target_maximum_distance && target_maximum_distance > 0.0) {160joint_one_to_target = target_maximum_distance;161}162163if (bone_one_length + bone_two_length < joint_one_to_target) {164override_angles_due_to_out_of_range = true;165}166167if (!override_angles_due_to_out_of_range) {168float angle_0 = Math::acos(((joint_one_to_target * joint_one_to_target) + (bone_one_length * bone_one_length) - (bone_two_length * bone_two_length)) / (2.0 * joint_one_to_target * bone_one_length));169float angle_1 = Math::acos(((bone_two_length * bone_two_length) + (bone_one_length * bone_one_length) - (joint_one_to_target * joint_one_to_target)) / (2.0 * bone_two_length * bone_one_length));170171if (flip_bend_direction) {172angle_0 = -angle_0;173angle_1 = -angle_1;174}175176if (std::isnan(angle_0) || std::isnan(angle_1)) {177// We cannot solve for this angle! Do nothing to avoid setting the rotation (and scale) to NaN.178} else {179joint_one_bone->set_global_rotation(angle_atan - angle_0 - joint_one_bone->get_bone_angle());180joint_two_bone->set_rotation(-Math::PI - angle_1 - joint_two_bone->get_bone_angle() + joint_one_bone->get_bone_angle());181}182} else {183joint_one_bone->set_global_rotation(angle_atan - joint_one_bone->get_bone_angle());184joint_two_bone->set_global_rotation(angle_atan - joint_two_bone->get_bone_angle());185}186187stack->skeleton->set_bone_local_pose_override(joint_one_bone_idx, joint_one_bone->get_transform(), stack->strength, true);188stack->skeleton->set_bone_local_pose_override(joint_two_bone_idx, joint_two_bone->get_transform(), stack->strength, true);189}190191void SkeletonModification2DTwoBoneIK::_setup_modification(SkeletonModificationStack2D *p_stack) {192stack = p_stack;193194if (stack) {195is_setup = true;196update_target_cache();197update_joint_one_bone2d_cache();198update_joint_two_bone2d_cache();199}200}201202void SkeletonModification2DTwoBoneIK::_draw_editor_gizmo() {203if (!enabled || !is_setup) {204return;205}206207Bone2D *operation_bone_one = stack->skeleton->get_bone(joint_one_bone_idx);208if (!operation_bone_one) {209return;210}211stack->skeleton->draw_set_transform(212stack->skeleton->to_local(operation_bone_one->get_global_position()),213operation_bone_one->get_global_rotation() - stack->skeleton->get_global_rotation());214215Color bone_ik_color = Color(1.0, 0.65, 0.0, 0.4);216#ifdef TOOLS_ENABLED217if (Engine::get_singleton()->is_editor_hint()) {218bone_ik_color = EDITOR_GET("editors/2d/bone_ik_color");219}220#endif // TOOLS_ENABLED221222if (flip_bend_direction) {223float angle = -(Math::PI * 0.5) + operation_bone_one->get_bone_angle();224stack->skeleton->draw_line(Vector2(0, 0), Vector2(Math::cos(angle), std::sin(angle)) * (operation_bone_one->get_length() * 0.5), bone_ik_color, 2.0);225} else {226float angle = (Math::PI * 0.5) + operation_bone_one->get_bone_angle();227stack->skeleton->draw_line(Vector2(0, 0), Vector2(Math::cos(angle), std::sin(angle)) * (operation_bone_one->get_length() * 0.5), bone_ik_color, 2.0);228}229230#ifdef TOOLS_ENABLED231if (Engine::get_singleton()->is_editor_hint()) {232if (editor_draw_min_max) {233if (target_maximum_distance != 0.0 || target_minimum_distance != 0.0) {234Vector2 target_direction = Vector2(0, 1);235if (target_node_cache.is_valid()) {236stack->skeleton->draw_set_transform(Vector2(0, 0), 0.0);237Node2D *target = ObjectDB::get_instance<Node2D>(target_node_cache);238target_direction = operation_bone_one->get_global_position().direction_to(target->get_global_position());239}240241stack->skeleton->draw_circle(target_direction * target_minimum_distance, 8, bone_ik_color);242stack->skeleton->draw_circle(target_direction * target_maximum_distance, 8, bone_ik_color);243stack->skeleton->draw_line(target_direction * target_minimum_distance, target_direction * target_maximum_distance, bone_ik_color, 2.0);244}245}246}247#endif // TOOLS_ENABLED248}249250void SkeletonModification2DTwoBoneIK::update_target_cache() {251if (!is_setup || !stack) {252if (is_setup) {253ERR_PRINT_ONCE("Cannot update target cache: modification is not properly setup!");254}255return;256}257258target_node_cache = ObjectID();259if (stack->skeleton) {260if (stack->skeleton->is_inside_tree()) {261if (stack->skeleton->has_node(target_node)) {262Node *node = stack->skeleton->get_node(target_node);263ERR_FAIL_COND_MSG(!node || stack->skeleton == node,264"Cannot update target cache: node is this modification's skeleton or cannot be found!");265ERR_FAIL_COND_MSG(!node->is_inside_tree(),266"Cannot update target cache: node is not in the scene tree!");267target_node_cache = node->get_instance_id();268}269}270}271}272273void SkeletonModification2DTwoBoneIK::update_joint_one_bone2d_cache() {274if (!is_setup || !stack) {275if (is_setup) {276ERR_PRINT_ONCE("Cannot update joint one Bone2D cache: modification is not properly setup!");277}278return;279}280281joint_one_bone2d_node_cache = ObjectID();282if (stack->skeleton) {283if (stack->skeleton->is_inside_tree()) {284if (stack->skeleton->has_node(joint_one_bone2d_node)) {285Node *node = stack->skeleton->get_node(joint_one_bone2d_node);286ERR_FAIL_COND_MSG(!node || stack->skeleton == node,287"Cannot update joint one Bone2D cache: node is this modification's skeleton or cannot be found!");288ERR_FAIL_COND_MSG(!node->is_inside_tree(),289"Cannot update joint one Bone2D cache: node is not in the scene tree!");290joint_one_bone2d_node_cache = node->get_instance_id();291292Bone2D *bone = Object::cast_to<Bone2D>(node);293if (bone) {294joint_one_bone_idx = bone->get_index_in_skeleton();295} else {296ERR_FAIL_MSG("Update joint one Bone2D cache: Nodepath to Bone2D is not a Bone2D node!");297}298}299}300}301}302303void SkeletonModification2DTwoBoneIK::update_joint_two_bone2d_cache() {304if (!is_setup || !stack) {305if (is_setup) {306ERR_PRINT_ONCE("Cannot update joint two Bone2D cache: modification is not properly setup!");307}308return;309}310311joint_two_bone2d_node_cache = ObjectID();312if (stack->skeleton) {313if (stack->skeleton->is_inside_tree()) {314if (stack->skeleton->has_node(joint_two_bone2d_node)) {315Node *node = stack->skeleton->get_node(joint_two_bone2d_node);316ERR_FAIL_COND_MSG(!node || stack->skeleton == node,317"Cannot update joint two Bone2D cache: node is this modification's skeleton or cannot be found!");318ERR_FAIL_COND_MSG(!node->is_inside_tree(),319"Cannot update joint two Bone2D cache: node is not in scene tree!");320joint_two_bone2d_node_cache = node->get_instance_id();321322Bone2D *bone = Object::cast_to<Bone2D>(node);323if (bone) {324joint_two_bone_idx = bone->get_index_in_skeleton();325} else {326ERR_FAIL_MSG("Update joint two Bone2D cache: Nodepath to Bone2D is not a Bone2D node!");327}328}329}330}331}332333void SkeletonModification2DTwoBoneIK::set_target_node(const NodePath &p_target_node) {334target_node = p_target_node;335update_target_cache();336}337338NodePath SkeletonModification2DTwoBoneIK::get_target_node() const {339return target_node;340}341342void SkeletonModification2DTwoBoneIK::set_joint_one_bone2d_node(const NodePath &p_target_node) {343joint_one_bone2d_node = p_target_node;344update_joint_one_bone2d_cache();345notify_property_list_changed();346}347348void SkeletonModification2DTwoBoneIK::set_target_minimum_distance(float p_distance) {349ERR_FAIL_COND_MSG(p_distance < 0, "Target minimum distance cannot be less than zero!");350target_minimum_distance = p_distance;351}352353float SkeletonModification2DTwoBoneIK::get_target_minimum_distance() const {354return target_minimum_distance;355}356357void SkeletonModification2DTwoBoneIK::set_target_maximum_distance(float p_distance) {358ERR_FAIL_COND_MSG(p_distance < 0, "Target maximum distance cannot be less than zero!");359target_maximum_distance = p_distance;360}361362float SkeletonModification2DTwoBoneIK::get_target_maximum_distance() const {363return target_maximum_distance;364}365366void SkeletonModification2DTwoBoneIK::set_flip_bend_direction(bool p_flip_direction) {367flip_bend_direction = p_flip_direction;368369#ifdef TOOLS_ENABLED370if (stack && is_setup) {371stack->set_editor_gizmos_dirty(true);372}373#endif // TOOLS_ENABLED374}375376bool SkeletonModification2DTwoBoneIK::get_flip_bend_direction() const {377return flip_bend_direction;378}379380NodePath SkeletonModification2DTwoBoneIK::get_joint_one_bone2d_node() const {381return joint_one_bone2d_node;382}383384void SkeletonModification2DTwoBoneIK::set_joint_two_bone2d_node(const NodePath &p_target_node) {385joint_two_bone2d_node = p_target_node;386update_joint_two_bone2d_cache();387notify_property_list_changed();388}389390NodePath SkeletonModification2DTwoBoneIK::get_joint_two_bone2d_node() const {391return joint_two_bone2d_node;392}393394void SkeletonModification2DTwoBoneIK::set_joint_one_bone_idx(int p_bone_idx) {395ERR_FAIL_COND_MSG(p_bone_idx < 0, "Bone index is out of range: The index is too low!");396397if (is_setup) {398if (stack->skeleton) {399ERR_FAIL_INDEX_MSG(p_bone_idx, stack->skeleton->get_bone_count(), "Passed-in Bone index is out of range!");400joint_one_bone_idx = p_bone_idx;401joint_one_bone2d_node_cache = stack->skeleton->get_bone(p_bone_idx)->get_instance_id();402joint_one_bone2d_node = stack->skeleton->get_path_to(stack->skeleton->get_bone(p_bone_idx));403} else {404WARN_PRINT("TwoBoneIK: Cannot verify the joint bone index for joint one...");405joint_one_bone_idx = p_bone_idx;406}407} else {408joint_one_bone_idx = p_bone_idx;409}410411notify_property_list_changed();412}413414int SkeletonModification2DTwoBoneIK::get_joint_one_bone_idx() const {415return joint_one_bone_idx;416}417418void SkeletonModification2DTwoBoneIK::set_joint_two_bone_idx(int p_bone_idx) {419ERR_FAIL_COND_MSG(p_bone_idx < 0, "Bone index is out of range: The index is too low!");420421if (is_setup) {422if (stack->skeleton) {423ERR_FAIL_INDEX_MSG(p_bone_idx, stack->skeleton->get_bone_count(), "Passed-in Bone index is out of range!");424joint_two_bone_idx = p_bone_idx;425joint_two_bone2d_node_cache = stack->skeleton->get_bone(p_bone_idx)->get_instance_id();426joint_two_bone2d_node = stack->skeleton->get_path_to(stack->skeleton->get_bone(p_bone_idx));427} else {428WARN_PRINT("TwoBoneIK: Cannot verify the joint bone index for joint two...");429joint_two_bone_idx = p_bone_idx;430}431} else {432joint_two_bone_idx = p_bone_idx;433}434435notify_property_list_changed();436}437438int SkeletonModification2DTwoBoneIK::get_joint_two_bone_idx() const {439return joint_two_bone_idx;440}441442#ifdef TOOLS_ENABLED443void SkeletonModification2DTwoBoneIK::set_editor_draw_min_max(bool p_draw) {444editor_draw_min_max = p_draw;445}446447bool SkeletonModification2DTwoBoneIK::get_editor_draw_min_max() const {448return editor_draw_min_max;449}450#endif // TOOLS_ENABLED451452void SkeletonModification2DTwoBoneIK::_bind_methods() {453ClassDB::bind_method(D_METHOD("set_target_node", "target_nodepath"), &SkeletonModification2DTwoBoneIK::set_target_node);454ClassDB::bind_method(D_METHOD("get_target_node"), &SkeletonModification2DTwoBoneIK::get_target_node);455456ClassDB::bind_method(D_METHOD("set_target_minimum_distance", "minimum_distance"), &SkeletonModification2DTwoBoneIK::set_target_minimum_distance);457ClassDB::bind_method(D_METHOD("get_target_minimum_distance"), &SkeletonModification2DTwoBoneIK::get_target_minimum_distance);458ClassDB::bind_method(D_METHOD("set_target_maximum_distance", "maximum_distance"), &SkeletonModification2DTwoBoneIK::set_target_maximum_distance);459ClassDB::bind_method(D_METHOD("get_target_maximum_distance"), &SkeletonModification2DTwoBoneIK::get_target_maximum_distance);460ClassDB::bind_method(D_METHOD("set_flip_bend_direction", "flip_direction"), &SkeletonModification2DTwoBoneIK::set_flip_bend_direction);461ClassDB::bind_method(D_METHOD("get_flip_bend_direction"), &SkeletonModification2DTwoBoneIK::get_flip_bend_direction);462463ClassDB::bind_method(D_METHOD("set_joint_one_bone2d_node", "bone2d_node"), &SkeletonModification2DTwoBoneIK::set_joint_one_bone2d_node);464ClassDB::bind_method(D_METHOD("get_joint_one_bone2d_node"), &SkeletonModification2DTwoBoneIK::get_joint_one_bone2d_node);465ClassDB::bind_method(D_METHOD("set_joint_one_bone_idx", "bone_idx"), &SkeletonModification2DTwoBoneIK::set_joint_one_bone_idx);466ClassDB::bind_method(D_METHOD("get_joint_one_bone_idx"), &SkeletonModification2DTwoBoneIK::get_joint_one_bone_idx);467468ClassDB::bind_method(D_METHOD("set_joint_two_bone2d_node", "bone2d_node"), &SkeletonModification2DTwoBoneIK::set_joint_two_bone2d_node);469ClassDB::bind_method(D_METHOD("get_joint_two_bone2d_node"), &SkeletonModification2DTwoBoneIK::get_joint_two_bone2d_node);470ClassDB::bind_method(D_METHOD("set_joint_two_bone_idx", "bone_idx"), &SkeletonModification2DTwoBoneIK::set_joint_two_bone_idx);471ClassDB::bind_method(D_METHOD("get_joint_two_bone_idx"), &SkeletonModification2DTwoBoneIK::get_joint_two_bone_idx);472473ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target_nodepath", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node2D"), "set_target_node", "get_target_node");474ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "target_minimum_distance", PROPERTY_HINT_RANGE, "0,100000000,0.01,suffix:px"), "set_target_minimum_distance", "get_target_minimum_distance");475ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "target_maximum_distance", PROPERTY_HINT_NONE, "0,100000000,0.01,suffix:px"), "set_target_maximum_distance", "get_target_maximum_distance");476ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_bend_direction", PROPERTY_HINT_NONE, ""), "set_flip_bend_direction", "get_flip_bend_direction");477ADD_GROUP("", "");478}479480SkeletonModification2DTwoBoneIK::SkeletonModification2DTwoBoneIK() {481stack = nullptr;482is_setup = false;483enabled = true;484editor_draw_gizmo = true;485}486487SkeletonModification2DTwoBoneIK::~SkeletonModification2DTwoBoneIK() {488}489490491