skeleton_ik.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*************************************************************************/
  2. /* skeleton_ik.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #ifndef SKELETON_IK_H
  31. #define SKELETON_IK_H
  32. #ifndef _3D_DISABLED
  33. /**
  34. * @author AndreaCatania
  35. */
  36. #include "core/math/transform.h"
  37. #include "scene/3d/skeleton.h"
  38. class FabrikInverseKinematic {
  39. struct EndEffector {
  40. BoneId tip_bone;
  41. Transform goal_transform;
  42. };
  43. struct ChainItem {
  44. Vector<ChainItem> children;
  45. ChainItem *parent_item;
  46. // Bone info
  47. BoneId bone;
  48. real_t length;
  49. /// Positions relative to root bone
  50. Transform initial_transform;
  51. Vector3 current_pos;
  52. // Direction from this bone to child
  53. Vector3 current_ori;
  54. ChainItem() :
  55. parent_item(nullptr),
  56. bone(-1),
  57. length(0) {}
  58. ChainItem *find_child(const BoneId p_bone_id);
  59. ChainItem *add_child(const BoneId p_bone_id);
  60. };
  61. struct ChainTip {
  62. ChainItem *chain_item;
  63. const EndEffector *end_effector;
  64. ChainTip() :
  65. chain_item(nullptr),
  66. end_effector(nullptr) {}
  67. ChainTip(ChainItem *p_chain_item, const EndEffector *p_end_effector) :
  68. chain_item(p_chain_item),
  69. end_effector(p_end_effector) {}
  70. };
  71. struct Chain {
  72. ChainItem chain_root;
  73. ChainItem *middle_chain_item;
  74. Vector<ChainTip> tips;
  75. Vector3 magnet_position;
  76. };
  77. public:
  78. struct Task : public RID_Data {
  79. RID self;
  80. Skeleton *skeleton;
  81. Chain chain;
  82. // Settings
  83. real_t min_distance;
  84. int max_iterations;
  85. // Bone data
  86. BoneId root_bone;
  87. Vector<EndEffector> end_effectors;
  88. Transform goal_global_transform;
  89. Task() :
  90. skeleton(nullptr),
  91. min_distance(0.01),
  92. max_iterations(10),
  93. root_bone(-1) {}
  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. static void solve_simple(Task *p_task, bool p_solve_magnet, Vector3 p_origin_pos);
  99. /// Special solvers that solve only chains with one end effector
  100. static void solve_simple_backwards(Chain &r_chain, bool p_solve_magnet);
  101. static void solve_simple_forwards(Chain &r_chain, bool p_solve_magnet, Vector3 p_origin_pos);
  102. public:
  103. static Task *create_simple_task(Skeleton *p_sk, BoneId root_bone, BoneId tip_bone, const Transform &goal_transform);
  104. static void free_task(Task *p_task);
  105. // The goal of chain should be always in local space
  106. static void set_goal(Task *p_task, const Transform &p_goal);
  107. static void make_goal(Task *p_task, const Transform &p_inverse_transf, real_t blending_delta);
  108. static void solve(Task *p_task, real_t blending_delta, bool override_tip_basis, bool p_use_magnet, const Vector3 &p_magnet_position);
  109. static void _update_chain(const Skeleton *p_skeleton, ChainItem *p_chain_item);
  110. };
  111. class SkeletonIK : public Node {
  112. GDCLASS(SkeletonIK, Node);
  113. StringName root_bone;
  114. StringName tip_bone;
  115. real_t interpolation;
  116. Transform target;
  117. NodePath target_node_path_override;
  118. bool override_tip_basis;
  119. bool use_magnet;
  120. Vector3 magnet_position;
  121. real_t min_distance;
  122. int max_iterations;
  123. Skeleton *skeleton;
  124. Spatial *target_node_override;
  125. FabrikInverseKinematic::Task *task;
  126. protected:
  127. virtual void
  128. _validate_property(PropertyInfo &property) const;
  129. static void _bind_methods();
  130. virtual void _notification(int p_what);
  131. public:
  132. SkeletonIK();
  133. virtual ~SkeletonIK();
  134. void set_root_bone(const StringName &p_root_bone);
  135. StringName get_root_bone() const;
  136. void set_tip_bone(const StringName &p_tip_bone);
  137. StringName get_tip_bone() const;
  138. void set_interpolation(real_t p_interpolation);
  139. real_t get_interpolation() const;
  140. void set_target_transform(const Transform &p_target);
  141. const Transform &get_target_transform() const;
  142. void set_target_node(const NodePath &p_node);
  143. NodePath get_target_node();
  144. void set_override_tip_basis(bool p_override);
  145. bool is_override_tip_basis() const;
  146. void set_use_magnet(bool p_use);
  147. bool is_using_magnet() const;
  148. void set_magnet_position(const Vector3 &p_local_position);
  149. const Vector3 &get_magnet_position() const;
  150. void set_min_distance(real_t p_min_distance);
  151. real_t get_min_distance() const { return min_distance; }
  152. void set_max_iterations(int p_iterations);
  153. int get_max_iterations() const { return max_iterations; }
  154. Skeleton *get_parent_skeleton() const { return skeleton; }
  155. bool is_running();
  156. void start(bool p_one_time = false);
  157. void stop();
  158. private:
  159. Transform _get_target_transform();
  160. void reload_chain();
  161. void reload_goal();
  162. void _solve_chain();
  163. };
  164. #endif // _3D_DISABLED
  165. #endif // SKELETON_IK_H