skeleton_modification_2d_fabrik.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #ifndef SKELETON_MODIFICATION_2D_FABRIK_H
  31. #define SKELETON_MODIFICATION_2D_FABRIK_H
  32. #include "scene/2d/skeleton_2d.h"
  33. #include "scene/resources/2d/skeleton/skeleton_modification_2d.h"
  34. ///////////////////////////////////////
  35. // SkeletonModification2DFABRIK
  36. ///////////////////////////////////////
  37. class SkeletonModification2DFABRIK : public SkeletonModification2D {
  38. GDCLASS(SkeletonModification2DFABRIK, SkeletonModification2D);
  39. private:
  40. struct FABRIK_Joint_Data2D {
  41. int bone_idx = -1;
  42. NodePath bone2d_node;
  43. ObjectID bone2d_node_cache;
  44. Vector2 magnet_position = Vector2(0, 0);
  45. bool use_target_rotation = false;
  46. bool editor_draw_gizmo = true;
  47. };
  48. Vector<FABRIK_Joint_Data2D> fabrik_data_chain;
  49. // Unlike in 3D, we need a vector of Transform2D objects to perform FABRIK.
  50. // This is because FABRIK (unlike CCDIK) needs to operate on transforms that are NOT
  51. // affected by each other, making the transforms stored in Bone2D unusable, as well as those in Skeleton2D.
  52. // For this reason, this modification stores a vector of Transform2Ds used for the calculations, which are then applied at the end.
  53. Vector<Transform2D> fabrik_transform_chain;
  54. NodePath target_node;
  55. ObjectID target_node_cache;
  56. void update_target_cache();
  57. float chain_tolarance = 0.01;
  58. int chain_max_iterations = 10;
  59. int chain_iterations = 0;
  60. Transform2D target_global_pose;
  61. Transform2D origin_global_pose;
  62. void fabrik_joint_update_bone2d_cache(int p_joint_idx);
  63. void chain_backwards();
  64. void chain_forwards();
  65. protected:
  66. static void _bind_methods();
  67. bool _set(const StringName &p_path, const Variant &p_value);
  68. bool _get(const StringName &p_path, Variant &r_ret) const;
  69. void _get_property_list(List<PropertyInfo> *p_list) const;
  70. public:
  71. void _execute(float p_delta) override;
  72. void _setup_modification(SkeletonModificationStack2D *p_stack) override;
  73. void set_target_node(const NodePath &p_target_node);
  74. NodePath get_target_node() const;
  75. int get_fabrik_data_chain_length();
  76. void set_fabrik_data_chain_length(int p_new_length);
  77. void set_fabrik_joint_bone2d_node(int p_joint_idx, const NodePath &p_target_node);
  78. NodePath get_fabrik_joint_bone2d_node(int p_joint_idx) const;
  79. void set_fabrik_joint_bone_index(int p_joint_idx, int p_bone_idx);
  80. int get_fabrik_joint_bone_index(int p_joint_idx) const;
  81. void set_fabrik_joint_magnet_position(int p_joint_idx, Vector2 p_magnet_position);
  82. Vector2 get_fabrik_joint_magnet_position(int p_joint_idx) const;
  83. void set_fabrik_joint_use_target_rotation(int p_joint_idx, bool p_use_target_rotation);
  84. bool get_fabrik_joint_use_target_rotation(int p_joint_idx) const;
  85. SkeletonModification2DFABRIK();
  86. ~SkeletonModification2DFABRIK();
  87. };
  88. #endif // SKELETON_MODIFICATION_2D_FABRIK_H