skeleton_ik.h 6.5 KB

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