2017-10-03 16:49:32 +00:00
|
|
|
/**************************************************************************/
|
2020-03-27 07:44:44 +00:00
|
|
|
/* skeleton_ik_3d.h */
|
2017-10-03 16:49:32 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
2022-07-23 21:41:51 +00:00
|
|
|
#ifndef SKELETON_IK_3D_H
|
|
|
|
#define SKELETON_IK_3D_H
|
2017-10-03 16:49:32 +00:00
|
|
|
|
2024-01-28 00:11:43 +00:00
|
|
|
#include "scene/3d/skeleton_modifier_3d.h"
|
2017-10-03 16:49:32 +00:00
|
|
|
|
|
|
|
class FabrikInverseKinematic {
|
|
|
|
struct EndEffector {
|
|
|
|
BoneId tip_bone;
|
2020-10-17 05:08:21 +00:00
|
|
|
Transform3D goal_transform;
|
2017-10-03 16:49:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ChainItem {
|
2019-05-19 10:34:40 +00:00
|
|
|
Vector<ChainItem> children;
|
2020-05-12 15:01:17 +00:00
|
|
|
ChainItem *parent_item = nullptr;
|
2017-10-03 16:49:32 +00:00
|
|
|
|
|
|
|
// Bone info
|
2020-05-12 15:01:17 +00:00
|
|
|
BoneId bone = -1;
|
2017-10-03 16:49:32 +00:00
|
|
|
|
2021-02-07 21:29:31 +00:00
|
|
|
real_t length = 0.0;
|
2017-10-03 16:49:32 +00:00
|
|
|
/// Positions relative to root bone
|
2020-10-17 05:08:21 +00:00
|
|
|
Transform3D initial_transform;
|
2017-10-03 16:49:32 +00:00
|
|
|
Vector3 current_pos;
|
|
|
|
// Direction from this bone to child
|
|
|
|
Vector3 current_ori;
|
|
|
|
|
|
|
|
ChainItem *find_child(const BoneId p_bone_id);
|
|
|
|
ChainItem *add_child(const BoneId p_bone_id);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ChainTip {
|
2020-05-12 15:01:17 +00:00
|
|
|
ChainItem *chain_item = nullptr;
|
|
|
|
const EndEffector *end_effector = nullptr;
|
2017-10-03 16:49:32 +00:00
|
|
|
|
2020-05-12 15:01:17 +00:00
|
|
|
ChainTip() {}
|
2017-10-03 16:49:32 +00:00
|
|
|
|
|
|
|
ChainTip(ChainItem *p_chain_item, const EndEffector *p_end_effector) :
|
|
|
|
chain_item(p_chain_item),
|
|
|
|
end_effector(p_end_effector) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Chain {
|
|
|
|
ChainItem chain_root;
|
2021-02-07 21:29:31 +00:00
|
|
|
ChainItem *middle_chain_item = nullptr;
|
2017-10-03 16:49:32 +00:00
|
|
|
Vector<ChainTip> tips;
|
|
|
|
Vector3 magnet_position;
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2019-11-05 11:01:00 +00:00
|
|
|
struct Task {
|
2017-10-03 16:49:32 +00:00
|
|
|
RID self;
|
2020-05-12 15:01:17 +00:00
|
|
|
Skeleton3D *skeleton = nullptr;
|
2017-10-03 16:49:32 +00:00
|
|
|
|
|
|
|
Chain chain;
|
|
|
|
|
|
|
|
// Settings
|
2020-05-12 15:01:17 +00:00
|
|
|
real_t min_distance = 0.01;
|
|
|
|
int max_iterations = 10;
|
2017-10-03 16:49:32 +00:00
|
|
|
|
|
|
|
// Bone data
|
2020-05-12 15:01:17 +00:00
|
|
|
BoneId root_bone = -1;
|
2017-10-03 16:49:32 +00:00
|
|
|
Vector<EndEffector> end_effectors;
|
|
|
|
|
2020-10-17 05:08:21 +00:00
|
|
|
Transform3D goal_global_transform;
|
2017-10-03 16:49:32 +00:00
|
|
|
|
2020-05-12 15:01:17 +00:00
|
|
|
Task() {}
|
2017-10-03 16:49:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// Init a chain that starts from the root to tip
|
2018-08-26 10:41:20 +00:00
|
|
|
static bool build_chain(Task *p_task, bool p_force_simple_chain = true);
|
2017-10-03 16:49:32 +00:00
|
|
|
|
2021-05-26 01:30:40 +00:00
|
|
|
static void solve_simple(Task *p_task, bool p_solve_magnet, Vector3 p_origin_pos);
|
2017-10-03 16:49:32 +00:00
|
|
|
/// Special solvers that solve only chains with one end effector
|
2022-04-28 09:35:39 +00:00
|
|
|
static void solve_simple_backwards(const Chain &r_chain, bool p_solve_magnet);
|
2021-05-26 01:30:40 +00:00
|
|
|
static void solve_simple_forwards(Chain &r_chain, bool p_solve_magnet, Vector3 p_origin_pos);
|
2017-10-03 16:49:32 +00:00
|
|
|
|
|
|
|
public:
|
2020-10-17 05:08:21 +00:00
|
|
|
static Task *create_simple_task(Skeleton3D *p_sk, BoneId root_bone, BoneId tip_bone, const Transform3D &goal_transform);
|
2017-10-03 16:49:32 +00:00
|
|
|
static void free_task(Task *p_task);
|
|
|
|
// The goal of chain should be always in local space
|
2020-10-17 05:08:21 +00:00
|
|
|
static void set_goal(Task *p_task, const Transform3D &p_goal);
|
2024-01-28 00:11:43 +00:00
|
|
|
static void make_goal(Task *p_task, const Transform3D &p_inverse_transf);
|
|
|
|
static void solve(Task *p_task, bool override_tip_basis, bool p_use_magnet, const Vector3 &p_magnet_position);
|
2021-03-28 16:00:18 +00:00
|
|
|
|
|
|
|
static void _update_chain(const Skeleton3D *p_skeleton, ChainItem *p_chain_item);
|
2017-10-03 16:49:32 +00:00
|
|
|
};
|
|
|
|
|
2024-01-28 00:11:43 +00:00
|
|
|
class SkeletonIK3D : public SkeletonModifier3D {
|
|
|
|
GDCLASS(SkeletonIK3D, SkeletonModifier3D);
|
|
|
|
|
|
|
|
bool internal_active = false;
|
2017-10-03 16:49:32 +00:00
|
|
|
|
|
|
|
StringName root_bone;
|
|
|
|
StringName tip_bone;
|
2020-10-17 05:08:21 +00:00
|
|
|
Transform3D target;
|
2017-10-03 16:49:32 +00:00
|
|
|
NodePath target_node_path_override;
|
2020-05-12 15:01:17 +00:00
|
|
|
bool override_tip_basis = true;
|
|
|
|
bool use_magnet = false;
|
2017-10-03 16:49:32 +00:00
|
|
|
Vector3 magnet_position;
|
|
|
|
|
2020-05-12 15:01:17 +00:00
|
|
|
real_t min_distance = 0.01;
|
|
|
|
int max_iterations = 10;
|
2017-10-03 16:49:32 +00:00
|
|
|
|
2023-05-25 06:28:08 +00:00
|
|
|
Variant target_node_override_ref = Variant();
|
2020-05-12 15:01:17 +00:00
|
|
|
FabrikInverseKinematic::Task *task = nullptr;
|
2017-10-03 16:49:32 +00:00
|
|
|
|
2024-04-17 04:44:44 +00:00
|
|
|
#ifndef DISABLE_DEPRECATED
|
|
|
|
void _set_interpolation(real_t p_interpolation);
|
|
|
|
real_t _get_interpolation() const;
|
|
|
|
#endif // DISABLE_DEPRECATED
|
|
|
|
|
2017-10-03 16:49:32 +00:00
|
|
|
protected:
|
2022-08-12 20:57:11 +00:00
|
|
|
void _validate_property(PropertyInfo &p_property) const;
|
2017-10-03 16:49:32 +00:00
|
|
|
|
|
|
|
static void _bind_methods();
|
2019-06-26 13:08:25 +00:00
|
|
|
virtual void _notification(int p_what);
|
2017-10-03 16:49:32 +00:00
|
|
|
|
2024-01-28 00:11:43 +00:00
|
|
|
virtual void _process_modification() override;
|
|
|
|
|
2017-10-03 16:49:32 +00:00
|
|
|
public:
|
2020-03-26 21:49:16 +00:00
|
|
|
SkeletonIK3D();
|
|
|
|
virtual ~SkeletonIK3D();
|
2017-10-03 16:49:32 +00:00
|
|
|
|
|
|
|
void set_root_bone(const StringName &p_root_bone);
|
|
|
|
StringName get_root_bone() const;
|
|
|
|
|
|
|
|
void set_tip_bone(const StringName &p_tip_bone);
|
|
|
|
StringName get_tip_bone() const;
|
|
|
|
|
2020-10-17 05:08:21 +00:00
|
|
|
void set_target_transform(const Transform3D &p_target);
|
|
|
|
const Transform3D &get_target_transform() const;
|
2017-10-03 16:49:32 +00:00
|
|
|
|
|
|
|
void set_target_node(const NodePath &p_node);
|
|
|
|
NodePath get_target_node();
|
|
|
|
|
2018-08-26 11:50:44 +00:00
|
|
|
void set_override_tip_basis(bool p_override);
|
|
|
|
bool is_override_tip_basis() const;
|
|
|
|
|
2017-10-03 16:49:32 +00:00
|
|
|
void set_use_magnet(bool p_use);
|
|
|
|
bool is_using_magnet() const;
|
|
|
|
|
2019-06-26 13:08:25 +00:00
|
|
|
void set_magnet_position(const Vector3 &p_local_position);
|
2017-10-03 16:49:32 +00:00
|
|
|
const Vector3 &get_magnet_position() const;
|
|
|
|
|
|
|
|
void set_min_distance(real_t p_min_distance);
|
|
|
|
real_t get_min_distance() const { return min_distance; }
|
|
|
|
|
|
|
|
void set_max_iterations(int p_iterations);
|
|
|
|
int get_max_iterations() const { return max_iterations; }
|
|
|
|
|
2023-05-25 06:28:08 +00:00
|
|
|
Skeleton3D *get_parent_skeleton() const;
|
2017-10-03 16:49:32 +00:00
|
|
|
|
|
|
|
bool is_running();
|
|
|
|
|
|
|
|
void start(bool p_one_time = false);
|
|
|
|
void stop();
|
|
|
|
|
|
|
|
private:
|
2020-10-17 05:08:21 +00:00
|
|
|
Transform3D _get_target_transform();
|
2017-10-03 16:49:32 +00:00
|
|
|
void reload_chain();
|
|
|
|
void reload_goal();
|
|
|
|
void _solve_chain();
|
|
|
|
};
|
|
|
|
|
2022-07-23 21:41:51 +00:00
|
|
|
#endif // SKELETON_IK_3D_H
|