1/**************************************************************************/
2/* skeleton_modification_2d_fabrik.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#ifndef SKELETON_MODIFICATION_2D_FABRIK_H
32#define SKELETON_MODIFICATION_2D_FABRIK_H
33
34#include "scene/2d/skeleton_2d.h"
35#include "scene/resources/skeleton_modification_2d.h"
36
37///////////////////////////////////////
38// SkeletonModification2DFABRIK
39///////////////////////////////////////
40
41class SkeletonModification2DFABRIK : public SkeletonModification2D {
42 GDCLASS(SkeletonModification2DFABRIK, SkeletonModification2D);
43
44private:
45 struct FABRIK_Joint_Data2D {
46 int bone_idx = -1;
47 NodePath bone2d_node;
48 ObjectID bone2d_node_cache;
49
50 Vector2 magnet_position = Vector2(0, 0);
51 bool use_target_rotation = false;
52
53 bool editor_draw_gizmo = true;
54 };
55
56 Vector<FABRIK_Joint_Data2D> fabrik_data_chain;
57
58 // Unlike in 3D, we need a vector of Transform2D objects to perform FABRIK.
59 // This is because FABRIK (unlike CCDIK) needs to operate on transforms that are NOT
60 // affected by each other, making the transforms stored in Bone2D unusable, as well as those in Skeleton2D.
61 // For this reason, this modification stores a vector of Transform2Ds used for the calculations, which are then applied at the end.
62 Vector<Transform2D> fabrik_transform_chain;
63
64 NodePath target_node;
65 ObjectID target_node_cache;
66 void update_target_cache();
67
68 float chain_tolarance = 0.01;
69 int chain_max_iterations = 10;
70 int chain_iterations = 0;
71 Transform2D target_global_pose;
72 Transform2D origin_global_pose;
73
74 void fabrik_joint_update_bone2d_cache(int p_joint_idx);
75 void chain_backwards();
76 void chain_forwards();
77
78protected:
79 static void _bind_methods();
80 bool _set(const StringName &p_path, const Variant &p_value);
81 bool _get(const StringName &p_path, Variant &r_ret) const;
82 void _get_property_list(List<PropertyInfo> *p_list) const;
83
84public:
85 void _execute(float p_delta) override;
86 void _setup_modification(SkeletonModificationStack2D *p_stack) override;
87
88 void set_target_node(const NodePath &p_target_node);
89 NodePath get_target_node() const;
90
91 int get_fabrik_data_chain_length();
92 void set_fabrik_data_chain_length(int p_new_length);
93
94 void set_fabrik_joint_bone2d_node(int p_joint_idx, const NodePath &p_target_node);
95 NodePath get_fabrik_joint_bone2d_node(int p_joint_idx) const;
96 void set_fabrik_joint_bone_index(int p_joint_idx, int p_bone_idx);
97 int get_fabrik_joint_bone_index(int p_joint_idx) const;
98
99 void set_fabrik_joint_magnet_position(int p_joint_idx, Vector2 p_magnet_position);
100 Vector2 get_fabrik_joint_magnet_position(int p_joint_idx) const;
101 void set_fabrik_joint_use_target_rotation(int p_joint_idx, bool p_use_target_rotation);
102 bool get_fabrik_joint_use_target_rotation(int p_joint_idx) const;
103
104 SkeletonModification2DFABRIK();
105 ~SkeletonModification2DFABRIK();
106};
107
108#endif // SKELETON_MODIFICATION_2D_FABRIK_H
109