2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* skeleton_ik_3d.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2017-10-03 18:49:32 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
#include "skeleton_ik_3d.h"
|
2017-10-03 18:49:32 +02:00
|
|
|
|
2018-08-19 12:09:41 +02:00
|
|
|
#ifndef _3D_DISABLED
|
|
|
|
|
2017-10-03 18:49:32 +02:00
|
|
|
FabrikInverseKinematic::ChainItem *FabrikInverseKinematic::ChainItem::find_child(const BoneId p_bone_id) {
|
2019-05-19 12:34:40 +02:00
|
|
|
for (int i = children.size() - 1; 0 <= i; --i) {
|
|
|
|
if (p_bone_id == children[i].bone) {
|
|
|
|
return &children.write[i];
|
2017-10-03 18:49:32 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2017-10-03 18:49:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FabrikInverseKinematic::ChainItem *FabrikInverseKinematic::ChainItem::add_child(const BoneId p_bone_id) {
|
2019-05-19 12:34:40 +02:00
|
|
|
const int infant_child_id = children.size();
|
|
|
|
children.resize(infant_child_id + 1);
|
|
|
|
children.write[infant_child_id].bone = p_bone_id;
|
|
|
|
children.write[infant_child_id].parent_item = this;
|
|
|
|
return &children.write[infant_child_id];
|
2017-10-03 18:49:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Build a chain that starts from the root to tip
|
2018-08-26 12:41:20 +02:00
|
|
|
bool FabrikInverseKinematic::build_chain(Task *p_task, bool p_force_simple_chain) {
|
|
|
|
ERR_FAIL_COND_V(-1 == p_task->root_bone, false);
|
2017-10-03 18:49:32 +02:00
|
|
|
|
|
|
|
Chain &chain(p_task->chain);
|
|
|
|
|
|
|
|
chain.tips.resize(p_task->end_effectors.size());
|
|
|
|
chain.chain_root.bone = p_task->root_bone;
|
|
|
|
chain.chain_root.initial_transform = p_task->skeleton->get_bone_global_pose(chain.chain_root.bone);
|
|
|
|
chain.chain_root.current_pos = chain.chain_root.initial_transform.origin;
|
2020-04-02 01:20:12 +02:00
|
|
|
chain.middle_chain_item = nullptr;
|
2017-10-03 18:49:32 +02:00
|
|
|
|
|
|
|
// Holds all IDs that are composing a single chain in reverse order
|
|
|
|
Vector<BoneId> chain_ids;
|
|
|
|
// This is used to know the chain size
|
|
|
|
int sub_chain_size;
|
|
|
|
// Resize only one time in order to fit all joints for performance reason
|
|
|
|
chain_ids.resize(p_task->skeleton->get_bone_count());
|
|
|
|
|
|
|
|
for (int x = p_task->end_effectors.size() - 1; 0 <= x; --x) {
|
|
|
|
const EndEffector *ee(&p_task->end_effectors[x]);
|
2018-08-26 12:41:20 +02:00
|
|
|
ERR_FAIL_COND_V(p_task->root_bone >= ee->tip_bone, false);
|
|
|
|
ERR_FAIL_INDEX_V(ee->tip_bone, p_task->skeleton->get_bone_count(), false);
|
2017-10-03 18:49:32 +02:00
|
|
|
|
|
|
|
sub_chain_size = 0;
|
|
|
|
// Picks all IDs that composing a single chain in reverse order (except the root)
|
|
|
|
BoneId chain_sub_tip(ee->tip_bone);
|
|
|
|
while (chain_sub_tip > p_task->root_bone) {
|
|
|
|
chain_ids.write[sub_chain_size++] = chain_sub_tip;
|
|
|
|
chain_sub_tip = p_task->skeleton->get_bone_parent(chain_sub_tip);
|
|
|
|
}
|
|
|
|
|
2021-01-30 01:55:54 +01:00
|
|
|
BoneId middle_chain_item_id = (BoneId)(sub_chain_size * 0.5);
|
2017-10-03 18:49:32 +02:00
|
|
|
|
|
|
|
// Build chain by reading chain ids in reverse order
|
|
|
|
// For each chain item id will be created a ChainItem if doesn't exists
|
|
|
|
ChainItem *sub_chain(&chain.chain_root);
|
|
|
|
for (int i = sub_chain_size - 1; 0 <= i; --i) {
|
|
|
|
ChainItem *child_ci(sub_chain->find_child(chain_ids[i]));
|
|
|
|
if (!child_ci) {
|
|
|
|
child_ci = sub_chain->add_child(chain_ids[i]);
|
|
|
|
|
|
|
|
child_ci->initial_transform = p_task->skeleton->get_bone_global_pose(child_ci->bone);
|
|
|
|
child_ci->current_pos = child_ci->initial_transform.origin;
|
|
|
|
|
|
|
|
if (child_ci->parent_item) {
|
2021-09-29 05:51:34 +02:00
|
|
|
child_ci->length = child_ci->parent_item->current_pos.distance_to(child_ci->current_pos);
|
2017-10-03 18:49:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub_chain = child_ci;
|
|
|
|
|
|
|
|
if (middle_chain_item_id == i) {
|
|
|
|
chain.middle_chain_item = child_ci;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!middle_chain_item_id) {
|
2020-04-02 01:20:12 +02:00
|
|
|
chain.middle_chain_item = nullptr;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-10-03 18:49:32 +02:00
|
|
|
|
|
|
|
// Initialize current tip
|
|
|
|
chain.tips.write[x].chain_item = sub_chain;
|
|
|
|
chain.tips.write[x].end_effector = ee;
|
|
|
|
|
|
|
|
if (p_force_simple_chain) {
|
|
|
|
// NOTE:
|
2021-03-12 14:35:16 +01:00
|
|
|
// This is a "hack" that force to create only one tip per chain since the solver of multi tip (end effector)
|
2017-10-03 18:49:32 +02:00
|
|
|
// is not yet created.
|
|
|
|
// Remove this code when this is done
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-08-26 12:41:20 +02:00
|
|
|
return true;
|
2017-10-03 18:49:32 +02:00
|
|
|
}
|
|
|
|
|
2021-05-26 03:30:40 +02:00
|
|
|
void FabrikInverseKinematic::solve_simple(Task *p_task, bool p_solve_magnet, Vector3 p_origin_pos) {
|
2017-10-03 18:49:32 +02:00
|
|
|
real_t distance_to_goal(1e4);
|
|
|
|
real_t previous_distance_to_goal(0);
|
|
|
|
int can_solve(p_task->max_iterations);
|
|
|
|
while (distance_to_goal > p_task->min_distance && Math::abs(previous_distance_to_goal - distance_to_goal) > 0.005 && can_solve) {
|
|
|
|
previous_distance_to_goal = distance_to_goal;
|
|
|
|
--can_solve;
|
|
|
|
|
|
|
|
solve_simple_backwards(p_task->chain, p_solve_magnet);
|
2021-05-26 03:30:40 +02:00
|
|
|
solve_simple_forwards(p_task->chain, p_solve_magnet, p_origin_pos);
|
2017-10-03 18:49:32 +02:00
|
|
|
|
2021-09-29 05:51:34 +02:00
|
|
|
distance_to_goal = p_task->chain.tips[0].end_effector->goal_transform.origin.distance_to(p_task->chain.tips[0].chain_item->current_pos);
|
2017-10-03 18:49:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-28 11:35:39 +02:00
|
|
|
void FabrikInverseKinematic::solve_simple_backwards(const Chain &r_chain, bool p_solve_magnet) {
|
2017-10-03 18:49:32 +02:00
|
|
|
if (p_solve_magnet && !r_chain.middle_chain_item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 goal;
|
|
|
|
ChainItem *sub_chain_tip;
|
|
|
|
if (p_solve_magnet) {
|
|
|
|
goal = r_chain.magnet_position;
|
|
|
|
sub_chain_tip = r_chain.middle_chain_item;
|
|
|
|
} else {
|
|
|
|
goal = r_chain.tips[0].end_effector->goal_transform.origin;
|
|
|
|
sub_chain_tip = r_chain.tips[0].chain_item;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (sub_chain_tip) {
|
|
|
|
sub_chain_tip->current_pos = goal;
|
|
|
|
|
|
|
|
if (sub_chain_tip->parent_item) {
|
|
|
|
// Not yet in the chain root
|
|
|
|
// So calculate next goal location
|
|
|
|
|
|
|
|
const Vector3 look_parent((sub_chain_tip->parent_item->current_pos - sub_chain_tip->current_pos).normalized());
|
|
|
|
goal = sub_chain_tip->current_pos + (look_parent * sub_chain_tip->length);
|
|
|
|
|
|
|
|
// [TODO] Constraints goes here
|
|
|
|
}
|
|
|
|
|
|
|
|
sub_chain_tip = sub_chain_tip->parent_item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-26 03:30:40 +02:00
|
|
|
void FabrikInverseKinematic::solve_simple_forwards(Chain &r_chain, bool p_solve_magnet, Vector3 p_origin_pos) {
|
2017-10-03 18:49:32 +02:00
|
|
|
if (p_solve_magnet && !r_chain.middle_chain_item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChainItem *sub_chain_root(&r_chain.chain_root);
|
2021-05-26 03:30:40 +02:00
|
|
|
Vector3 origin = p_origin_pos;
|
2017-10-03 18:49:32 +02:00
|
|
|
|
|
|
|
while (sub_chain_root) { // Reach the tip
|
|
|
|
sub_chain_root->current_pos = origin;
|
|
|
|
|
2020-12-15 13:04:21 +01:00
|
|
|
if (!sub_chain_root->children.is_empty()) {
|
2019-05-19 12:34:40 +02:00
|
|
|
ChainItem &child(sub_chain_root->children.write[0]);
|
2017-10-03 18:49:32 +02:00
|
|
|
|
|
|
|
// Is not tip
|
|
|
|
// So calculate next origin location
|
|
|
|
|
|
|
|
// Look child
|
|
|
|
sub_chain_root->current_ori = (child.current_pos - sub_chain_root->current_pos).normalized();
|
|
|
|
origin = sub_chain_root->current_pos + (sub_chain_root->current_ori * child.length);
|
|
|
|
|
|
|
|
// [TODO] Constraints goes here
|
|
|
|
|
|
|
|
if (p_solve_magnet && sub_chain_root == r_chain.middle_chain_item) {
|
|
|
|
// In case of magnet solving this is the tip
|
2020-04-02 01:20:12 +02:00
|
|
|
sub_chain_root = nullptr;
|
2017-10-03 18:49:32 +02:00
|
|
|
} else {
|
|
|
|
sub_chain_root = &child;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Is tip
|
2020-04-02 01:20:12 +02:00
|
|
|
sub_chain_root = nullptr;
|
2017-10-03 18:49:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-17 07:08:21 +02:00
|
|
|
FabrikInverseKinematic::Task *FabrikInverseKinematic::create_simple_task(Skeleton3D *p_sk, BoneId root_bone, BoneId tip_bone, const Transform3D &goal_transform) {
|
2017-10-03 18:49:32 +02:00
|
|
|
FabrikInverseKinematic::EndEffector ee;
|
|
|
|
ee.tip_bone = tip_bone;
|
|
|
|
|
|
|
|
Task *task(memnew(Task));
|
|
|
|
task->skeleton = p_sk;
|
|
|
|
task->root_bone = root_bone;
|
|
|
|
task->end_effectors.push_back(ee);
|
|
|
|
task->goal_global_transform = goal_transform;
|
|
|
|
|
2018-08-26 12:41:20 +02:00
|
|
|
if (!build_chain(task)) {
|
|
|
|
free_task(task);
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2018-08-26 12:41:20 +02:00
|
|
|
}
|
2017-10-03 18:49:32 +02:00
|
|
|
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FabrikInverseKinematic::free_task(Task *p_task) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_task) {
|
2017-10-03 18:49:32 +02:00
|
|
|
memdelete(p_task);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-10-03 18:49:32 +02:00
|
|
|
}
|
|
|
|
|
2020-10-17 07:08:21 +02:00
|
|
|
void FabrikInverseKinematic::set_goal(Task *p_task, const Transform3D &p_goal) {
|
2017-10-03 18:49:32 +02:00
|
|
|
p_task->goal_global_transform = p_goal;
|
|
|
|
}
|
|
|
|
|
2020-10-17 07:08:21 +02:00
|
|
|
void FabrikInverseKinematic::make_goal(Task *p_task, const Transform3D &p_inverse_transf, real_t blending_delta) {
|
2017-10-03 18:49:32 +02:00
|
|
|
if (blending_delta >= 0.99f) {
|
|
|
|
// Update the end_effector (local transform) without blending
|
|
|
|
p_task->end_effectors.write[0].goal_transform = p_inverse_transf * p_task->goal_global_transform;
|
|
|
|
} else {
|
|
|
|
// End effector in local transform
|
2020-10-17 07:08:21 +02:00
|
|
|
const Transform3D end_effector_pose(p_task->skeleton->get_bone_global_pose_no_override(p_task->end_effectors[0].tip_bone));
|
2017-10-03 18:49:32 +02:00
|
|
|
|
|
|
|
// Update the end_effector (local transform) by blending with current pose
|
|
|
|
p_task->end_effectors.write[0].goal_transform = end_effector_pose.interpolate_with(p_inverse_transf * p_task->goal_global_transform, blending_delta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-26 13:50:44 +02:00
|
|
|
void FabrikInverseKinematic::solve(Task *p_task, real_t blending_delta, bool override_tip_basis, bool p_use_magnet, const Vector3 &p_magnet_position) {
|
2017-10-03 18:49:32 +02:00
|
|
|
if (blending_delta <= 0.01f) {
|
2021-03-28 18:00:18 +02:00
|
|
|
// Before skipping, make sure we undo the global pose overrides
|
|
|
|
ChainItem *ci(&p_task->chain.chain_root);
|
|
|
|
while (ci) {
|
|
|
|
p_task->skeleton->set_bone_global_pose_override(ci->bone, ci->initial_transform, 0.0, false);
|
|
|
|
|
|
|
|
if (!ci->children.is_empty()) {
|
|
|
|
ci = &ci->children.write[0];
|
|
|
|
} else {
|
|
|
|
ci = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-03 18:49:32 +02:00
|
|
|
return; // Skip solving
|
|
|
|
}
|
|
|
|
|
2021-04-24 19:07:17 +02:00
|
|
|
// Update the initial root transform so its synced with any animation changes
|
2021-03-28 18:00:18 +02:00
|
|
|
_update_chain(p_task->skeleton, &p_task->chain.chain_root);
|
2021-03-18 22:52:54 +01:00
|
|
|
|
2020-10-17 07:08:21 +02:00
|
|
|
p_task->skeleton->set_bone_global_pose_override(p_task->chain.chain_root.bone, Transform3D(), 0.0, false);
|
2021-05-26 03:30:40 +02:00
|
|
|
Vector3 origin_pos = p_task->skeleton->get_bone_global_pose(p_task->chain.chain_root.bone).origin;
|
|
|
|
|
2020-07-27 21:27:58 +02:00
|
|
|
make_goal(p_task, p_task->skeleton->get_global_transform().affine_inverse(), blending_delta);
|
2017-10-03 18:49:32 +02:00
|
|
|
|
|
|
|
if (p_use_magnet && p_task->chain.middle_chain_item) {
|
2020-03-16 10:07:33 +01:00
|
|
|
p_task->chain.magnet_position = p_task->chain.middle_chain_item->initial_transform.origin.lerp(p_magnet_position, blending_delta);
|
2021-05-26 03:30:40 +02:00
|
|
|
solve_simple(p_task, true, origin_pos);
|
2017-10-03 18:49:32 +02:00
|
|
|
}
|
2021-05-26 03:30:40 +02:00
|
|
|
solve_simple(p_task, false, origin_pos);
|
2017-10-03 18:49:32 +02:00
|
|
|
|
|
|
|
// Assign new bone position.
|
|
|
|
ChainItem *ci(&p_task->chain.chain_root);
|
|
|
|
while (ci) {
|
2020-10-17 07:08:21 +02:00
|
|
|
Transform3D new_bone_pose(ci->initial_transform);
|
2017-10-03 18:49:32 +02:00
|
|
|
new_bone_pose.origin = ci->current_pos;
|
|
|
|
|
2021-04-24 19:07:17 +02:00
|
|
|
if (!ci->children.is_empty()) {
|
2023-05-25 11:58:17 +02:00
|
|
|
Vector3 forward_vector = (ci->children[0].initial_transform.origin - ci->initial_transform.origin).normalized();
|
2021-10-04 16:22:51 +02:00
|
|
|
// Rotate the bone towards the next bone in the chain:
|
|
|
|
new_bone_pose.basis.rotate_to_align(forward_vector, new_bone_pose.origin.direction_to(ci->children[0].current_pos));
|
2021-03-28 18:00:18 +02:00
|
|
|
|
2021-04-24 19:07:17 +02:00
|
|
|
} else {
|
|
|
|
// Set target orientation to tip
|
|
|
|
if (override_tip_basis) {
|
|
|
|
new_bone_pose.basis = p_task->chain.tips[0].end_effector->goal_transform.basis;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2021-04-24 19:07:17 +02:00
|
|
|
new_bone_pose.basis = new_bone_pose.basis * p_task->chain.tips[0].end_effector->goal_transform.basis;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-10-03 18:49:32 +02:00
|
|
|
}
|
|
|
|
|
2020-07-27 21:27:58 +02:00
|
|
|
// IK should not affect scale, so undo any scaling
|
|
|
|
new_bone_pose.basis.orthonormalize();
|
|
|
|
new_bone_pose.basis.scale(p_task->skeleton->get_bone_global_pose(ci->bone).basis.get_scale());
|
|
|
|
|
2020-01-16 20:22:15 +01:00
|
|
|
p_task->skeleton->set_bone_global_pose_override(ci->bone, new_bone_pose, 1.0, true);
|
2017-10-03 18:49:32 +02:00
|
|
|
|
2020-12-15 13:04:21 +01:00
|
|
|
if (!ci->children.is_empty()) {
|
2019-05-19 12:34:40 +02:00
|
|
|
ci = &ci->children.write[0];
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2020-04-02 01:20:12 +02:00
|
|
|
ci = nullptr;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-10-03 18:49:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-28 18:00:18 +02:00
|
|
|
void FabrikInverseKinematic::_update_chain(const Skeleton3D *p_sk, ChainItem *p_chain_item) {
|
|
|
|
if (!p_chain_item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-24 19:07:17 +02:00
|
|
|
p_chain_item->initial_transform = p_sk->get_bone_global_pose_no_override(p_chain_item->bone);
|
2021-03-28 18:00:18 +02:00
|
|
|
p_chain_item->current_pos = p_chain_item->initial_transform.origin;
|
|
|
|
|
|
|
|
ChainItem *items = p_chain_item->children.ptrw();
|
|
|
|
for (int i = 0; i < p_chain_item->children.size(); i += 1) {
|
|
|
|
_update_chain(p_sk, items + i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-12 22:57:11 +02:00
|
|
|
void SkeletonIK3D::_validate_property(PropertyInfo &p_property) const {
|
|
|
|
if (p_property.name == "root_bone" || p_property.name == "tip_bone") {
|
2023-05-25 08:28:08 +02:00
|
|
|
Skeleton3D *skeleton = get_parent_skeleton();
|
2017-10-03 18:49:32 +02:00
|
|
|
if (skeleton) {
|
2019-04-10 07:50:09 +02:00
|
|
|
String names("--,");
|
2017-10-03 18:49:32 +02:00
|
|
|
for (int i = 0; i < skeleton->get_bone_count(); i++) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (i > 0) {
|
2017-10-03 18:49:32 +02:00
|
|
|
names += ",";
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-10-03 18:49:32 +02:00
|
|
|
names += skeleton->get_bone_name(i);
|
|
|
|
}
|
|
|
|
|
2022-08-12 22:57:11 +02:00
|
|
|
p_property.hint = PROPERTY_HINT_ENUM;
|
|
|
|
p_property.hint_string = names;
|
2017-10-03 18:49:32 +02:00
|
|
|
} else {
|
2022-08-12 22:57:11 +02:00
|
|
|
p_property.hint = PROPERTY_HINT_NONE;
|
|
|
|
p_property.hint_string = "";
|
2017-10-03 18:49:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void SkeletonIK3D::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_root_bone", "root_bone"), &SkeletonIK3D::set_root_bone);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_root_bone"), &SkeletonIK3D::get_root_bone);
|
2017-10-03 18:49:32 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_tip_bone", "tip_bone"), &SkeletonIK3D::set_tip_bone);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_tip_bone"), &SkeletonIK3D::get_tip_bone);
|
2017-10-03 18:49:32 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_interpolation", "interpolation"), &SkeletonIK3D::set_interpolation);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_interpolation"), &SkeletonIK3D::get_interpolation);
|
2017-10-03 18:49:32 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_target_transform", "target"), &SkeletonIK3D::set_target_transform);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_target_transform"), &SkeletonIK3D::get_target_transform);
|
2017-10-03 18:49:32 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_target_node", "node"), &SkeletonIK3D::set_target_node);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_target_node"), &SkeletonIK3D::get_target_node);
|
2017-10-03 18:49:32 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_override_tip_basis", "override"), &SkeletonIK3D::set_override_tip_basis);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_override_tip_basis"), &SkeletonIK3D::is_override_tip_basis);
|
2018-08-26 13:50:44 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_use_magnet", "use"), &SkeletonIK3D::set_use_magnet);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_using_magnet"), &SkeletonIK3D::is_using_magnet);
|
2017-10-03 18:49:32 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_magnet_position", "local_position"), &SkeletonIK3D::set_magnet_position);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_magnet_position"), &SkeletonIK3D::get_magnet_position);
|
2017-10-03 18:49:32 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_parent_skeleton"), &SkeletonIK3D::get_parent_skeleton);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_running"), &SkeletonIK3D::is_running);
|
2017-10-03 18:49:32 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_min_distance", "min_distance"), &SkeletonIK3D::set_min_distance);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_min_distance"), &SkeletonIK3D::get_min_distance);
|
2017-10-03 18:49:32 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_max_iterations", "iterations"), &SkeletonIK3D::set_max_iterations);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_max_iterations"), &SkeletonIK3D::get_max_iterations);
|
2017-10-03 18:49:32 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("start", "one_time"), &SkeletonIK3D::start, DEFVAL(false));
|
|
|
|
ClassDB::bind_method(D_METHOD("stop"), &SkeletonIK3D::stop);
|
2017-10-03 18:49:32 +02:00
|
|
|
|
2020-02-20 22:58:05 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "root_bone"), "set_root_bone", "get_root_bone");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "tip_bone"), "set_tip_bone", "get_tip_bone");
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "interpolation", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_interpolation", "get_interpolation");
|
2021-12-03 01:09:19 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM3D, "target", PROPERTY_HINT_NONE, "suffix:m"), "set_target_transform", "get_target_transform");
|
2018-08-26 13:50:44 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "override_tip_basis"), "set_override_tip_basis", "is_override_tip_basis");
|
2017-10-03 18:49:32 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_magnet"), "set_use_magnet", "is_using_magnet");
|
2021-12-03 01:09:19 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "magnet", PROPERTY_HINT_NONE, "suffix:m"), "set_magnet_position", "get_magnet_position");
|
2017-10-03 18:49:32 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target_node"), "set_target_node", "get_target_node");
|
2022-05-20 07:24:41 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "min_distance", PROPERTY_HINT_NONE, "suffix:m"), "set_min_distance", "get_min_distance");
|
2017-10-03 18:49:32 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "max_iterations"), "set_max_iterations", "get_max_iterations");
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void SkeletonIK3D::_notification(int p_what) {
|
2017-10-03 18:49:32 +02:00
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2023-05-25 08:28:08 +02:00
|
|
|
skeleton_ref = Object::cast_to<Skeleton3D>(get_parent());
|
2019-07-02 16:23:54 +02:00
|
|
|
set_process_priority(1);
|
2017-10-03 18:49:32 +02:00
|
|
|
reload_chain();
|
|
|
|
} break;
|
2022-02-15 18:06:48 +01:00
|
|
|
|
2017-10-03 18:49:32 +02:00
|
|
|
case NOTIFICATION_INTERNAL_PROCESS: {
|
2023-05-25 08:28:08 +02:00
|
|
|
if (target_node_override_ref) {
|
2017-10-03 18:49:32 +02:00
|
|
|
reload_goal();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-10-03 18:49:32 +02:00
|
|
|
_solve_chain();
|
|
|
|
} break;
|
2022-02-15 18:06:48 +01:00
|
|
|
|
2017-10-03 18:49:32 +02:00
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
|
|
|
reload_chain();
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 17:01:17 +02:00
|
|
|
SkeletonIK3D::SkeletonIK3D() {
|
2017-10-03 18:49:32 +02:00
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
SkeletonIK3D::~SkeletonIK3D() {
|
2017-10-03 18:49:32 +02:00
|
|
|
FabrikInverseKinematic::free_task(task);
|
2020-04-02 01:20:12 +02:00
|
|
|
task = nullptr;
|
2017-10-03 18:49:32 +02:00
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void SkeletonIK3D::set_root_bone(const StringName &p_root_bone) {
|
2017-10-03 18:49:32 +02:00
|
|
|
root_bone = p_root_bone;
|
|
|
|
reload_chain();
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
StringName SkeletonIK3D::get_root_bone() const {
|
2017-10-03 18:49:32 +02:00
|
|
|
return root_bone;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void SkeletonIK3D::set_tip_bone(const StringName &p_tip_bone) {
|
2017-10-03 18:49:32 +02:00
|
|
|
tip_bone = p_tip_bone;
|
|
|
|
reload_chain();
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
StringName SkeletonIK3D::get_tip_bone() const {
|
2017-10-03 18:49:32 +02:00
|
|
|
return tip_bone;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void SkeletonIK3D::set_interpolation(real_t p_interpolation) {
|
2017-10-03 18:49:32 +02:00
|
|
|
interpolation = p_interpolation;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
real_t SkeletonIK3D::get_interpolation() const {
|
2017-10-03 18:49:32 +02:00
|
|
|
return interpolation;
|
|
|
|
}
|
|
|
|
|
2020-10-17 07:08:21 +02:00
|
|
|
void SkeletonIK3D::set_target_transform(const Transform3D &p_target) {
|
2017-10-03 18:49:32 +02:00
|
|
|
target = p_target;
|
|
|
|
reload_goal();
|
|
|
|
}
|
|
|
|
|
2020-10-17 07:08:21 +02:00
|
|
|
const Transform3D &SkeletonIK3D::get_target_transform() const {
|
2017-10-03 18:49:32 +02:00
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void SkeletonIK3D::set_target_node(const NodePath &p_node) {
|
2017-10-03 18:49:32 +02:00
|
|
|
target_node_path_override = p_node;
|
2023-05-25 08:28:08 +02:00
|
|
|
target_node_override_ref = Variant();
|
2017-10-03 18:49:32 +02:00
|
|
|
reload_goal();
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
NodePath SkeletonIK3D::get_target_node() {
|
2017-10-03 18:49:32 +02:00
|
|
|
return target_node_path_override;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void SkeletonIK3D::set_override_tip_basis(bool p_override) {
|
2018-08-26 13:50:44 +02:00
|
|
|
override_tip_basis = p_override;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
bool SkeletonIK3D::is_override_tip_basis() const {
|
2018-08-26 13:50:44 +02:00
|
|
|
return override_tip_basis;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void SkeletonIK3D::set_use_magnet(bool p_use) {
|
2017-10-03 18:49:32 +02:00
|
|
|
use_magnet = p_use;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
bool SkeletonIK3D::is_using_magnet() const {
|
2017-10-03 18:49:32 +02:00
|
|
|
return use_magnet;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void SkeletonIK3D::set_magnet_position(const Vector3 &p_local_position) {
|
2017-10-03 18:49:32 +02:00
|
|
|
magnet_position = p_local_position;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
const Vector3 &SkeletonIK3D::get_magnet_position() const {
|
2017-10-03 18:49:32 +02:00
|
|
|
return magnet_position;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void SkeletonIK3D::set_min_distance(real_t p_min_distance) {
|
2017-10-03 18:49:32 +02:00
|
|
|
min_distance = p_min_distance;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void SkeletonIK3D::set_max_iterations(int p_iterations) {
|
2017-10-03 18:49:32 +02:00
|
|
|
max_iterations = p_iterations;
|
|
|
|
}
|
|
|
|
|
2023-05-25 08:28:08 +02:00
|
|
|
Skeleton3D *SkeletonIK3D::get_parent_skeleton() const {
|
|
|
|
return cast_to<Skeleton3D>(skeleton_ref.get_validated_object());
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
bool SkeletonIK3D::is_running() {
|
2017-10-03 18:49:32 +02:00
|
|
|
return is_processing_internal();
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void SkeletonIK3D::start(bool p_one_time) {
|
2017-10-03 18:49:32 +02:00
|
|
|
if (p_one_time) {
|
|
|
|
set_process_internal(false);
|
2020-07-27 18:52:51 +02:00
|
|
|
|
2023-05-25 08:28:08 +02:00
|
|
|
if (target_node_override_ref) {
|
2020-07-27 18:52:51 +02:00
|
|
|
reload_goal();
|
|
|
|
}
|
|
|
|
|
2017-10-03 18:49:32 +02:00
|
|
|
_solve_chain();
|
|
|
|
} else {
|
|
|
|
set_process_internal(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void SkeletonIK3D::stop() {
|
2017-10-03 18:49:32 +02:00
|
|
|
set_process_internal(false);
|
2023-05-25 08:28:08 +02:00
|
|
|
Skeleton3D *skeleton = get_parent_skeleton();
|
2021-04-15 21:46:30 +02:00
|
|
|
if (skeleton) {
|
|
|
|
skeleton->clear_bones_global_pose_override();
|
|
|
|
}
|
2017-10-03 18:49:32 +02:00
|
|
|
}
|
|
|
|
|
2020-10-17 07:08:21 +02:00
|
|
|
Transform3D SkeletonIK3D::_get_target_transform() {
|
2023-05-25 08:28:08 +02:00
|
|
|
if (!target_node_override_ref && !target_node_path_override.is_empty()) {
|
|
|
|
target_node_override_ref = Object::cast_to<Node3D>(get_node(target_node_path_override));
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-10-03 18:49:32 +02:00
|
|
|
|
2023-05-25 08:28:08 +02:00
|
|
|
Node3D *target_node_override = cast_to<Node3D>(target_node_override_ref.get_validated_object());
|
2021-09-14 11:46:39 +02:00
|
|
|
if (target_node_override && target_node_override->is_inside_tree()) {
|
2017-10-03 18:49:32 +02:00
|
|
|
return target_node_override->get_global_transform();
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2017-10-03 18:49:32 +02:00
|
|
|
return target;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-10-03 18:49:32 +02:00
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void SkeletonIK3D::reload_chain() {
|
2017-10-03 18:49:32 +02:00
|
|
|
FabrikInverseKinematic::free_task(task);
|
2020-04-02 01:20:12 +02:00
|
|
|
task = nullptr;
|
2017-10-03 18:49:32 +02:00
|
|
|
|
2023-05-25 08:28:08 +02:00
|
|
|
Skeleton3D *skeleton = get_parent_skeleton();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!skeleton) {
|
2017-10-03 18:49:32 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-10-03 18:49:32 +02:00
|
|
|
|
|
|
|
task = FabrikInverseKinematic::create_simple_task(skeleton, skeleton->find_bone(root_bone), skeleton->find_bone(tip_bone), _get_target_transform());
|
2018-08-26 12:41:20 +02:00
|
|
|
if (task) {
|
|
|
|
task->max_iterations = max_iterations;
|
|
|
|
task->min_distance = min_distance;
|
|
|
|
}
|
2017-10-03 18:49:32 +02:00
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void SkeletonIK3D::reload_goal() {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!task) {
|
2017-10-03 18:49:32 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-10-03 18:49:32 +02:00
|
|
|
|
|
|
|
FabrikInverseKinematic::set_goal(task, _get_target_transform());
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void SkeletonIK3D::_solve_chain() {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!task) {
|
2017-10-03 18:49:32 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-08-26 13:50:44 +02:00
|
|
|
FabrikInverseKinematic::solve(task, interpolation, override_tip_basis, use_magnet, magnet_position);
|
2017-10-03 18:49:32 +02:00
|
|
|
}
|
2018-08-19 12:09:41 +02:00
|
|
|
|
|
|
|
#endif // _3D_DISABLED
|