💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.2 KiB

  1. /*
  2. * Copyright (C) 2023 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_ANIMATION_CCD_IK_SOLVER_HPP
  20. #define ANTKEEPER_ANIMATION_CCD_IK_SOLVER_HPP
  21. #include <engine/animation/ik/ik-solver.hpp>
  22. #include <engine/animation/bone.hpp>
  23. #include <vector>
  24. class ik_rig;
  25. /**
  26. * Cyclic Coordinate Descent (CCD) IK solver.
  27. */
  28. class ccd_ik_solver: public ik_solver
  29. {
  30. public:
  31. /**
  32. * Constructs a CCD IK solver.
  33. *
  34. * @param ik_rig IK rig with which to associate this IK solver.
  35. * @param root_bone_index Index of the first bone in the bone chain.
  36. * @param effector_bone_index Index of the last bone in the bone chain.
  37. * @param chain_length Number of bones in the IK chain.
  38. */
  39. ccd_ik_solver(ik_rig& ik_rig, bone_index_type root_bone_index, bone_index_type effector_bone_index);
  40. /// @name Solving
  41. /// @{
  42. void solve() override;
  43. /**
  44. * Sets the maximum number of solving iterations.
  45. *
  46. * @param iterations Maximum number of solving iterations.
  47. */
  48. inline void set_max_iterations(std::size_t iterations) noexcept
  49. {
  50. m_max_iterations = iterations;
  51. }
  52. /// Returns the maximum number of solving iterations.
  53. [[nodiscard]] inline std::size_t get_max_iterations() const noexcept
  54. {
  55. return m_max_iterations;
  56. }
  57. /// @}
  58. /// @name Effector
  59. /// @{
  60. /**
  61. * Sets the position of the end effector.
  62. *
  63. * @param position Position of the end effector, relative to the tip bone.
  64. */
  65. inline void set_effector_position(const math::fvec3& position) noexcept
  66. {
  67. m_effector_position = position;
  68. }
  69. /// Returns the position of the end effector, relative to the tip bone.
  70. [[nodiscard]] inline const math::fvec3& get_effector_position() const
  71. {
  72. return m_effector_position;
  73. }
  74. /// @}
  75. /// @name Goal
  76. /// @{
  77. /**
  78. * Sets the center of the IK goal.
  79. *
  80. * @param center IK goal center, in world-space.
  81. */
  82. inline void set_goal_center(const math::fvec3& center) noexcept
  83. {
  84. m_goal_center = center;
  85. }
  86. /**
  87. * Sets the radius of the IK goal.
  88. *
  89. * @param radius IK goal radius.
  90. */
  91. inline void set_goal_radius(float radius) noexcept
  92. {
  93. m_sqr_goal_radius = radius * radius;
  94. }
  95. /// Returns the center of the IK goal, in world-space.
  96. [[nodiscard]] inline const math::fvec3& get_goal_center() const
  97. {
  98. return m_goal_center;
  99. }
  100. /// @}
  101. private:
  102. ik_rig* m_ik_rig{nullptr};
  103. std::size_t m_max_iterations{10};
  104. std::vector<bone_index_type> m_bone_indices;
  105. math::fvec3 m_effector_position{0.0f, 0.0f, 0.0f};
  106. math::fvec3 m_goal_center{0.0f, 0.0f, 0.0f};
  107. float m_sqr_goal_radius{1e-5f};
  108. };
  109. #endif // ANTKEEPER_ANIMATION_CCD_IK_SOLVER_HPP