skeleton_ik_3d.h 6.3 KB

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