Debian Codegraph @

scene/3d/skeleton_ik_3d.h

raw symbols SCIP
1 /**************************************************************************/
2 /* skeleton_ik_3d.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 #pragma once
32
33 #include "scene/3d/skeleton_modifier_3d.h"
34
35 class FabrikInverseKinematic {
36 struct EndEffector {
37 BoneId tip_bone;
38 Transform3D goal_transform;
39 };
40
41 struct ChainItem {
42 Vector<ChainItem> children;
43 ChainItem *parent_item = nullptr;
44
45 // Bone info
46 BoneId bone = -1;
47
48 real_t length = 0.0;
49 /// Positions relative to root bone
50 Transform3D initial_transform;
51 Vector3 current_pos;
52 // Direction from this bone to child
53 Vector3 current_ori;
54
55 ChainItem *find_child(const BoneId p_bone_id);
56 ChainItem *add_child(const BoneId p_bone_id);
57 };
58
59 struct ChainTip {
60 ChainItem *chain_item = nullptr;
61 const EndEffector *end_effector = nullptr;
62
63 ChainTip() {}
64
65 ChainTip(ChainItem *p_chain_item, const EndEffector *p_end_effector) :
66 chain_item(p_chain_item),
67 end_effector(p_end_effector) {}
68 };
69
70 struct Chain {
71 ChainItem chain_root;
72 ChainItem *middle_chain_item = nullptr;
73 Vector<ChainTip> tips;
74 Vector3 magnet_position;
75 };
76
77 public:
78 struct Task {
79 RID self;
80 Skeleton3D *skeleton = nullptr;
81
82 Chain chain;
83
84 // Settings
85 real_t min_distance = 0.01;
86 int max_iterations = 10;
87
88 // Bone data
89 BoneId root_bone = -1;
90 Vector<EndEffector> end_effectors;
91
92 Transform3D goal_global_transform;
93 };
94
95 private:
96 /// Init a chain that starts from the root to tip
97 static bool build_chain(Task *p_task, bool p_force_simple_chain = true);
98
99 static void solve_simple(Task *p_task, bool p_solve_magnet, Vector3 p_origin_pos);
100 /// Special solvers that solve only chains with one end effector
101 static void solve_simple_backwards(const Chain &r_chain, bool p_solve_magnet);
102 static void solve_simple_forwards(Chain &r_chain, bool p_solve_magnet, Vector3 p_origin_pos);
103
104 public:
105 static Task *create_simple_task(Skeleton3D *p_sk, BoneId root_bone, BoneId tip_bone, const Transform3D &goal_transform);
106 static void free_task(Task *p_task);
107 // The goal of chain should be always in local space
108 static void set_goal(Task *p_task, const Transform3D &p_goal);
109 static void make_goal(Task *p_task, const Transform3D &p_inverse_transf);
110 static void solve(Task *p_task, bool override_tip_basis, bool p_use_magnet, const Vector3 &p_magnet_position);
111
112 static void _update_chain(const Skeleton3D *p_skeleton, ChainItem *p_chain_item);
113 };
114
115 class SkeletonIK3D : public SkeletonModifier3D {
116 GDCLASS(SkeletonIK3D, SkeletonModifier3D);
117
118 bool internal_active = false;
119
120 StringName root_bone;
121 StringName tip_bone;
122 Transform3D target;
123 NodePath target_node_path_override;
124 bool override_tip_basis = true;
125 bool use_magnet = false;
126 Vector3 magnet_position;
127
128 real_t min_distance = 0.01;
129 int max_iterations = 10;
130
131 Variant target_node_override_ref;
132 FabrikInverseKinematic::Task *task = nullptr;
133
134 #ifndef DISABLE_DEPRECATED
135 void _set_interpolation(real_t p_interpolation);
136 real_t _get_interpolation() const;
137 #endif // DISABLE_DEPRECATED
138
139 protected:
140 void _validate_property(PropertyInfo &p_property) const;
141
142 static void _bind_methods();
143 virtual void _notification(int p_what);
144
145 virtual void _process_modification(double p_delta) override;
146
147 public:
148 SkeletonIK3D();
149 virtual ~SkeletonIK3D();
150
151 void set_root_bone(const StringName &p_root_bone);
152 StringName get_root_bone() const;
153
154 void set_tip_bone(const StringName &p_tip_bone);
155 StringName get_tip_bone() const;
156
157 void set_target_transform(const Transform3D &p_target);
158 const Transform3D &get_target_transform() const;
159
160 void set_target_node(const NodePath &p_node);
161 NodePath get_target_node();
162
163 void set_override_tip_basis(bool p_override);
164 bool is_override_tip_basis() const;
165
166 void set_use_magnet(bool p_use);
167 bool is_using_magnet() const;
168
169 void set_magnet_position(const Vector3 &p_local_position);
170 const Vector3 &get_magnet_position() const;
171
172 void set_min_distance(real_t p_min_distance);
173 real_t get_min_distance() const { return min_distance; }
174
175 void set_max_iterations(int p_iterations);
176 int get_max_iterations() const { return max_iterations; }
177
178 Skeleton3D *get_parent_skeleton() const;
179
180 bool is_running();
181
182 void start(bool p_one_time = false);
183 void stop();
184
185 private:
186 Transform3D _get_target_transform();
187 void reload_chain();
188 void reload_goal();
189 void _solve_chain();
190 };