Path: blob/master/scene/resources/2d/skeleton/skeleton_modification_2d_fabrik.cpp
9906 views
/**************************************************************************/1/* skeleton_modification_2d_fabrik.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_fabrik.h"31#include "scene/2d/skeleton_2d.h"3233bool SkeletonModification2DFABRIK::_set(const StringName &p_path, const Variant &p_value) {34String path = p_path;3536if (path.begins_with("joint_data/")) {37int which = path.get_slicec('/', 1).to_int();38String what = path.get_slicec('/', 2);39ERR_FAIL_INDEX_V(which, fabrik_data_chain.size(), false);4041if (what == "bone2d_node") {42set_fabrik_joint_bone2d_node(which, p_value);43} else if (what == "bone_index") {44set_fabrik_joint_bone_index(which, p_value);45} else if (what == "magnet_position") {46set_fabrik_joint_magnet_position(which, p_value);47} else if (what == "use_target_rotation") {48set_fabrik_joint_use_target_rotation(which, p_value);49} else {50return false;51}52} else {53return false;54}5556return true;57}5859bool SkeletonModification2DFABRIK::_get(const StringName &p_path, Variant &r_ret) const {60String path = p_path;6162if (path.begins_with("joint_data/")) {63int which = path.get_slicec('/', 1).to_int();64String what = path.get_slicec('/', 2);65ERR_FAIL_INDEX_V(which, fabrik_data_chain.size(), false);6667if (what == "bone2d_node") {68r_ret = get_fabrik_joint_bone2d_node(which);69} else if (what == "bone_index") {70r_ret = get_fabrik_joint_bone_index(which);71} else if (what == "magnet_position") {72r_ret = get_fabrik_joint_magnet_position(which);73} else if (what == "use_target_rotation") {74r_ret = get_fabrik_joint_use_target_rotation(which);75} else {76return false;77}78} else {79return false;80}81return true;82}8384void SkeletonModification2DFABRIK::_get_property_list(List<PropertyInfo> *p_list) const {85for (int i = 0; i < fabrik_data_chain.size(); i++) {86String base_string = "joint_data/" + itos(i) + "/";8788p_list->push_back(PropertyInfo(Variant::INT, base_string + "bone_index", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));89p_list->push_back(PropertyInfo(Variant::NODE_PATH, base_string + "bone2d_node", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Bone2D", PROPERTY_USAGE_DEFAULT));9091if (i > 0) {92p_list->push_back(PropertyInfo(Variant::VECTOR2, base_string + "magnet_position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));93}94if (i == fabrik_data_chain.size() - 1) {95p_list->push_back(PropertyInfo(Variant::BOOL, base_string + "use_target_rotation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));96}97}98}99100void SkeletonModification2DFABRIK::_execute(float p_delta) {101ERR_FAIL_COND_MSG(!stack || !is_setup || stack->skeleton == nullptr,102"Modification is not setup and therefore cannot execute!");103if (!enabled) {104return;105}106107if (target_node_cache.is_null()) {108WARN_PRINT_ONCE("Target cache is out of date. Attempting to update...");109update_target_cache();110return;111}112113if (fabrik_data_chain.size() <= 1) {114ERR_PRINT_ONCE("FABRIK requires at least two joints to operate! Cannot execute modification!");115return;116}117118Node2D *target = ObjectDB::get_instance<Node2D>(target_node_cache);119if (!target || !target->is_inside_tree()) {120ERR_PRINT_ONCE("Target node is not in the scene tree. Cannot execute modification!");121return;122}123target_global_pose = target->get_global_transform();124125if (fabrik_data_chain[0].bone2d_node_cache.is_null() && !fabrik_data_chain[0].bone2d_node.is_empty()) {126fabrik_joint_update_bone2d_cache(0);127WARN_PRINT("Bone2D cache for origin joint is out of date. Updating...");128}129130Bone2D *origin_bone2d_node = ObjectDB::get_instance<Bone2D>(fabrik_data_chain[0].bone2d_node_cache);131if (!origin_bone2d_node || !origin_bone2d_node->is_inside_tree()) {132ERR_PRINT_ONCE("Origin joint's Bone2D node is not in the scene tree. Cannot execute modification!");133return;134}135136origin_global_pose = origin_bone2d_node->get_global_transform();137138if (fabrik_transform_chain.size() != fabrik_data_chain.size()) {139fabrik_transform_chain.resize(fabrik_data_chain.size());140}141142for (int i = 0; i < fabrik_data_chain.size(); i++) {143// Update the transform chain144if (fabrik_data_chain[i].bone2d_node_cache.is_null() && !fabrik_data_chain[i].bone2d_node.is_empty()) {145WARN_PRINT_ONCE("Bone2D cache for joint " + itos(i) + " is out of date.. Attempting to update...");146fabrik_joint_update_bone2d_cache(i);147}148Bone2D *joint_bone2d_node = ObjectDB::get_instance<Bone2D>(fabrik_data_chain[i].bone2d_node_cache);149if (!joint_bone2d_node) {150ERR_PRINT_ONCE("FABRIK Joint " + itos(i) + " does not have a Bone2D node set! Cannot execute modification!");151return;152}153fabrik_transform_chain.write[i] = joint_bone2d_node->get_global_transform();154}155156Bone2D *final_bone2d_node = ObjectDB::get_instance<Bone2D>(fabrik_data_chain[fabrik_data_chain.size() - 1].bone2d_node_cache);157float final_bone2d_angle = final_bone2d_node->get_global_rotation();158if (fabrik_data_chain[fabrik_data_chain.size() - 1].use_target_rotation) {159final_bone2d_angle = target_global_pose.get_rotation();160}161Vector2 final_bone2d_direction = Vector2(Math::cos(final_bone2d_angle), Math::sin(final_bone2d_angle));162float final_bone2d_length = final_bone2d_node->get_length() * MIN(final_bone2d_node->get_global_scale().x, final_bone2d_node->get_global_scale().y);163float target_distance = (final_bone2d_node->get_global_position() + (final_bone2d_direction * final_bone2d_length)).distance_to(target->get_global_position());164chain_iterations = 0;165166while (target_distance > chain_tolarance) {167chain_backwards();168chain_forwards();169170final_bone2d_angle = final_bone2d_node->get_global_rotation();171if (fabrik_data_chain[fabrik_data_chain.size() - 1].use_target_rotation) {172final_bone2d_angle = target_global_pose.get_rotation();173}174final_bone2d_direction = Vector2(Math::cos(final_bone2d_angle), Math::sin(final_bone2d_angle));175target_distance = (final_bone2d_node->get_global_position() + (final_bone2d_direction * final_bone2d_length)).distance_to(target->get_global_position());176177chain_iterations += 1;178if (chain_iterations >= chain_max_iterations) {179break;180}181}182183// Apply all of the saved transforms to the Bone2D nodes184for (int i = 0; i < fabrik_data_chain.size(); i++) {185Bone2D *joint_bone2d_node = ObjectDB::get_instance<Bone2D>(fabrik_data_chain[i].bone2d_node_cache);186if (!joint_bone2d_node) {187ERR_PRINT_ONCE("FABRIK Joint " + itos(i) + " does not have a Bone2D node set!");188continue;189}190Transform2D chain_trans = fabrik_transform_chain[i];191192// Apply rotation193if (i + 1 < fabrik_data_chain.size()) {194chain_trans = chain_trans.looking_at(fabrik_transform_chain[i + 1].get_origin());195} else {196if (fabrik_data_chain[i].use_target_rotation) {197chain_trans.set_rotation(target_global_pose.get_rotation());198} else {199chain_trans = chain_trans.looking_at(target_global_pose.get_origin());200}201}202// Adjust for the bone angle203chain_trans.set_rotation(chain_trans.get_rotation() - joint_bone2d_node->get_bone_angle());204205// Reset scale206chain_trans.set_scale(joint_bone2d_node->get_global_scale());207208// Apply to the bone, and to the override209joint_bone2d_node->set_global_transform(chain_trans);210stack->skeleton->set_bone_local_pose_override(fabrik_data_chain[i].bone_idx, joint_bone2d_node->get_transform(), stack->strength, true);211}212}213214void SkeletonModification2DFABRIK::chain_backwards() {215int final_joint_index = fabrik_data_chain.size() - 1;216Bone2D *final_bone2d_node = ObjectDB::get_instance<Bone2D>(fabrik_data_chain[final_joint_index].bone2d_node_cache);217Transform2D final_bone2d_trans = fabrik_transform_chain[final_joint_index];218219// Apply magnet position220if (final_joint_index != 0) {221final_bone2d_trans.set_origin(final_bone2d_trans.get_origin() + fabrik_data_chain[final_joint_index].magnet_position);222}223224// Set the rotation of the tip bone225final_bone2d_trans = final_bone2d_trans.looking_at(target_global_pose.get_origin());226227// Set the position of the tip bone228float final_bone2d_angle = final_bone2d_trans.get_rotation();229if (fabrik_data_chain[final_joint_index].use_target_rotation) {230final_bone2d_angle = target_global_pose.get_rotation();231}232Vector2 final_bone2d_direction = Vector2(Math::cos(final_bone2d_angle), Math::sin(final_bone2d_angle));233float final_bone2d_length = final_bone2d_node->get_length() * MIN(final_bone2d_node->get_global_scale().x, final_bone2d_node->get_global_scale().y);234final_bone2d_trans.set_origin(target_global_pose.get_origin() - (final_bone2d_direction * final_bone2d_length));235236// Save the transform237fabrik_transform_chain.write[final_joint_index] = final_bone2d_trans;238239int i = final_joint_index;240while (i >= 1) {241Transform2D previous_pose = fabrik_transform_chain[i];242i -= 1;243Bone2D *current_bone2d_node = ObjectDB::get_instance<Bone2D>(fabrik_data_chain[i].bone2d_node_cache);244Transform2D current_pose = fabrik_transform_chain[i];245246// Apply magnet position247if (i != 0) {248current_pose.set_origin(current_pose.get_origin() + fabrik_data_chain[i].magnet_position);249}250251float current_bone2d_node_length = current_bone2d_node->get_length() * MIN(current_bone2d_node->get_global_scale().x, current_bone2d_node->get_global_scale().y);252float length = current_bone2d_node_length / (current_pose.get_origin().distance_to(previous_pose.get_origin()));253Vector2 finish_position = previous_pose.get_origin().lerp(current_pose.get_origin(), length);254current_pose.set_origin(finish_position);255256// Save the transform257fabrik_transform_chain.write[i] = current_pose;258}259}260261void SkeletonModification2DFABRIK::chain_forwards() {262Transform2D origin_bone2d_trans = fabrik_transform_chain[0];263origin_bone2d_trans.set_origin(origin_global_pose.get_origin());264// Save the position265fabrik_transform_chain.write[0] = origin_bone2d_trans;266267for (int i = 0; i < fabrik_data_chain.size() - 1; i++) {268Bone2D *current_bone2d_node = ObjectDB::get_instance<Bone2D>(fabrik_data_chain[i].bone2d_node_cache);269Transform2D current_pose = fabrik_transform_chain[i];270Transform2D next_pose = fabrik_transform_chain[i + 1];271272float current_bone2d_node_length = current_bone2d_node->get_length() * MIN(current_bone2d_node->get_global_scale().x, current_bone2d_node->get_global_scale().y);273float length = current_bone2d_node_length / (next_pose.get_origin().distance_to(current_pose.get_origin()));274Vector2 finish_position = current_pose.get_origin().lerp(next_pose.get_origin(), length);275current_pose.set_origin(finish_position);276277// Apply to the bone278fabrik_transform_chain.write[i + 1] = current_pose;279}280}281282void SkeletonModification2DFABRIK::_setup_modification(SkeletonModificationStack2D *p_stack) {283stack = p_stack;284285if (stack != nullptr) {286is_setup = true;287288if (stack->skeleton) {289for (int i = 0; i < fabrik_data_chain.size(); i++) {290fabrik_joint_update_bone2d_cache(i);291}292}293update_target_cache();294}295}296297void SkeletonModification2DFABRIK::update_target_cache() {298if (!is_setup || !stack) {299if (is_setup) {300ERR_PRINT_ONCE("Cannot update target cache: modification is not properly setup!");301}302return;303}304305target_node_cache = ObjectID();306if (stack->skeleton) {307if (stack->skeleton->is_inside_tree()) {308if (stack->skeleton->has_node(target_node)) {309Node *node = stack->skeleton->get_node(target_node);310ERR_FAIL_COND_MSG(!node || stack->skeleton == node,311"Cannot update target cache: node is this modification's skeleton or cannot be found!");312ERR_FAIL_COND_MSG(!node->is_inside_tree(),313"Cannot update target cache: node is not in scene tree!");314target_node_cache = node->get_instance_id();315}316}317}318}319320void SkeletonModification2DFABRIK::fabrik_joint_update_bone2d_cache(int p_joint_idx) {321ERR_FAIL_INDEX_MSG(p_joint_idx, fabrik_data_chain.size(), "Cannot update bone2d cache: joint index out of range!");322if (!is_setup || !stack) {323if (is_setup) {324ERR_PRINT_ONCE("Cannot update FABRIK Bone2D cache: modification is not properly setup!");325}326return;327}328329fabrik_data_chain.write[p_joint_idx].bone2d_node_cache = ObjectID();330if (stack->skeleton) {331if (stack->skeleton->is_inside_tree()) {332if (stack->skeleton->has_node(fabrik_data_chain[p_joint_idx].bone2d_node)) {333Node *node = stack->skeleton->get_node(fabrik_data_chain[p_joint_idx].bone2d_node);334ERR_FAIL_COND_MSG(!node || stack->skeleton == node,335"Cannot update FABRIK joint " + itos(p_joint_idx) + " Bone2D cache: node is this modification's skeleton or cannot be found!");336ERR_FAIL_COND_MSG(!node->is_inside_tree(),337"Cannot update FABRIK joint " + itos(p_joint_idx) + " Bone2D cache: node is not in scene tree!");338fabrik_data_chain.write[p_joint_idx].bone2d_node_cache = node->get_instance_id();339340Bone2D *bone = Object::cast_to<Bone2D>(node);341if (bone) {342fabrik_data_chain.write[p_joint_idx].bone_idx = bone->get_index_in_skeleton();343} else {344ERR_FAIL_MSG("FABRIK joint " + itos(p_joint_idx) + " Bone2D cache: Nodepath to Bone2D is not a Bone2D node!");345}346}347}348}349}350351void SkeletonModification2DFABRIK::set_target_node(const NodePath &p_target_node) {352target_node = p_target_node;353update_target_cache();354}355356NodePath SkeletonModification2DFABRIK::get_target_node() const {357return target_node;358}359360void SkeletonModification2DFABRIK::set_fabrik_data_chain_length(int p_length) {361fabrik_data_chain.resize(p_length);362notify_property_list_changed();363}364365int SkeletonModification2DFABRIK::get_fabrik_data_chain_length() {366return fabrik_data_chain.size();367}368369void SkeletonModification2DFABRIK::set_fabrik_joint_bone2d_node(int p_joint_idx, const NodePath &p_target_node) {370ERR_FAIL_INDEX_MSG(p_joint_idx, fabrik_data_chain.size(), "FABRIK joint out of range!");371fabrik_data_chain.write[p_joint_idx].bone2d_node = p_target_node;372fabrik_joint_update_bone2d_cache(p_joint_idx);373374notify_property_list_changed();375}376377NodePath SkeletonModification2DFABRIK::get_fabrik_joint_bone2d_node(int p_joint_idx) const {378ERR_FAIL_INDEX_V_MSG(p_joint_idx, fabrik_data_chain.size(), NodePath(), "FABRIK joint out of range!");379return fabrik_data_chain[p_joint_idx].bone2d_node;380}381382void SkeletonModification2DFABRIK::set_fabrik_joint_bone_index(int p_joint_idx, int p_bone_idx) {383ERR_FAIL_INDEX_MSG(p_joint_idx, fabrik_data_chain.size(), "FABRIK joint out of range!");384ERR_FAIL_COND_MSG(p_bone_idx < 0, "Bone index is out of range: The index is too low!");385386if (is_setup) {387if (stack->skeleton) {388ERR_FAIL_INDEX_MSG(p_bone_idx, stack->skeleton->get_bone_count(), "Passed-in Bone index is out of range!");389fabrik_data_chain.write[p_joint_idx].bone_idx = p_bone_idx;390fabrik_data_chain.write[p_joint_idx].bone2d_node_cache = stack->skeleton->get_bone(p_bone_idx)->get_instance_id();391fabrik_data_chain.write[p_joint_idx].bone2d_node = stack->skeleton->get_path_to(stack->skeleton->get_bone(p_bone_idx));392} else {393WARN_PRINT("Cannot verify the FABRIK joint " + itos(p_joint_idx) + " bone index for this modification...");394fabrik_data_chain.write[p_joint_idx].bone_idx = p_bone_idx;395}396} else {397fabrik_data_chain.write[p_joint_idx].bone_idx = p_bone_idx;398}399400notify_property_list_changed();401}402403int SkeletonModification2DFABRIK::get_fabrik_joint_bone_index(int p_joint_idx) const {404ERR_FAIL_INDEX_V_MSG(p_joint_idx, fabrik_data_chain.size(), -1, "FABRIK joint out of range!");405return fabrik_data_chain[p_joint_idx].bone_idx;406}407408void SkeletonModification2DFABRIK::set_fabrik_joint_magnet_position(int p_joint_idx, Vector2 p_magnet_position) {409ERR_FAIL_INDEX_MSG(p_joint_idx, fabrik_data_chain.size(), "FABRIK joint out of range!");410fabrik_data_chain.write[p_joint_idx].magnet_position = p_magnet_position;411}412413Vector2 SkeletonModification2DFABRIK::get_fabrik_joint_magnet_position(int p_joint_idx) const {414ERR_FAIL_INDEX_V_MSG(p_joint_idx, fabrik_data_chain.size(), Vector2(), "FABRIK joint out of range!");415return fabrik_data_chain[p_joint_idx].magnet_position;416}417418void SkeletonModification2DFABRIK::set_fabrik_joint_use_target_rotation(int p_joint_idx, bool p_use_target_rotation) {419ERR_FAIL_INDEX_MSG(p_joint_idx, fabrik_data_chain.size(), "FABRIK joint out of range!");420fabrik_data_chain.write[p_joint_idx].use_target_rotation = p_use_target_rotation;421}422423bool SkeletonModification2DFABRIK::get_fabrik_joint_use_target_rotation(int p_joint_idx) const {424ERR_FAIL_INDEX_V_MSG(p_joint_idx, fabrik_data_chain.size(), false, "FABRIK joint out of range!");425return fabrik_data_chain[p_joint_idx].use_target_rotation;426}427428void SkeletonModification2DFABRIK::_bind_methods() {429ClassDB::bind_method(D_METHOD("set_target_node", "target_nodepath"), &SkeletonModification2DFABRIK::set_target_node);430ClassDB::bind_method(D_METHOD("get_target_node"), &SkeletonModification2DFABRIK::get_target_node);431432ClassDB::bind_method(D_METHOD("set_fabrik_data_chain_length", "length"), &SkeletonModification2DFABRIK::set_fabrik_data_chain_length);433ClassDB::bind_method(D_METHOD("get_fabrik_data_chain_length"), &SkeletonModification2DFABRIK::get_fabrik_data_chain_length);434435ClassDB::bind_method(D_METHOD("set_fabrik_joint_bone2d_node", "joint_idx", "bone2d_nodepath"), &SkeletonModification2DFABRIK::set_fabrik_joint_bone2d_node);436ClassDB::bind_method(D_METHOD("get_fabrik_joint_bone2d_node", "joint_idx"), &SkeletonModification2DFABRIK::get_fabrik_joint_bone2d_node);437ClassDB::bind_method(D_METHOD("set_fabrik_joint_bone_index", "joint_idx", "bone_idx"), &SkeletonModification2DFABRIK::set_fabrik_joint_bone_index);438ClassDB::bind_method(D_METHOD("get_fabrik_joint_bone_index", "joint_idx"), &SkeletonModification2DFABRIK::get_fabrik_joint_bone_index);439ClassDB::bind_method(D_METHOD("set_fabrik_joint_magnet_position", "joint_idx", "magnet_position"), &SkeletonModification2DFABRIK::set_fabrik_joint_magnet_position);440ClassDB::bind_method(D_METHOD("get_fabrik_joint_magnet_position", "joint_idx"), &SkeletonModification2DFABRIK::get_fabrik_joint_magnet_position);441ClassDB::bind_method(D_METHOD("set_fabrik_joint_use_target_rotation", "joint_idx", "use_target_rotation"), &SkeletonModification2DFABRIK::set_fabrik_joint_use_target_rotation);442ClassDB::bind_method(D_METHOD("get_fabrik_joint_use_target_rotation", "joint_idx"), &SkeletonModification2DFABRIK::get_fabrik_joint_use_target_rotation);443444ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target_nodepath", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node2D"), "set_target_node", "get_target_node");445ADD_PROPERTY(PropertyInfo(Variant::INT, "fabrik_data_chain_length", PROPERTY_HINT_RANGE, "0, 100, 1"), "set_fabrik_data_chain_length", "get_fabrik_data_chain_length");446}447448SkeletonModification2DFABRIK::SkeletonModification2DFABRIK() {449stack = nullptr;450is_setup = false;451enabled = true;452editor_draw_gizmo = false;453}454455SkeletonModification2DFABRIK::~SkeletonModification2DFABRIK() {456}457458459