IKEffector.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Scene/Component.h"
  24. #include "../Scene/Scene.h"
  25. using ik_node_t = struct ik_node_t;
  26. namespace Urho3D
  27. {
  28. class Context;
  29. class IKSolver;
  30. class URHO3D_API IKEffector : public Component
  31. {
  32. URHO3D_OBJECT(IKEffector, Component);
  33. public:
  34. enum Feature
  35. {
  36. /*!
  37. * If you set the effector weight (see SetWeight()) to a value in
  38. * between 0 and 1, the default behaviour is to linearly interpolate
  39. * the effector's target position. If the solved tree and the initial
  40. * tree are far apart, this can look very strange, especially if you
  41. * are controlling limbs on a character that are designed to rotation.
  42. * Enabling this causes a rotational based interpolation (nlerp) around
  43. * the chain's base node and makes transitions look much more natural.
  44. */
  45. WEIGHT_NLERP = 0x01,
  46. /*!
  47. * By default the end effector node will retain its global orientation,
  48. * even after solving. By enabling this feature, the node will instead
  49. * "rotate with" its parent node.
  50. */
  51. INHERIT_PARENT_ROTATION = 0x02
  52. };
  53. /// Constructs a new IK effector.
  54. explicit IKEffector(Context* context);
  55. /// Destructs he IK effector.
  56. ~IKEffector() override;
  57. /// Registers this class as an object factory.
  58. static void RegisterObject(Context* context);
  59. /// Test if a certain feature is enabled (see IKEffector::Feature).
  60. /// @nobind
  61. bool GetFeature(Feature feature) const;
  62. /// Enable or disable a certain feature (see IKEffector::Feature).
  63. /// @nobind
  64. void SetFeature(Feature feature, bool enable);
  65. /// Retrieves the node that is being used as a target. Can be NULL.
  66. /// @property
  67. Node* GetTargetNode() const;
  68. /*!
  69. * @property
  70. * @brief The position of the target node provides the target position of
  71. * the effector node.
  72. *
  73. * The IK chain is solved such that the node to which this component is
  74. * attached to will try to move to the position of the target node.
  75. * @param targetNode A scene node that acts as a target. Specifying NULL
  76. * will erase the target and cause the solver to ignore this chain.
  77. * @note You will get very strange behaviour if you specify a target node
  78. * that is part the IK chain being solved for (circular dependency). Don't
  79. * do that.
  80. */
  81. void SetTargetNode(Node* targetNode);
  82. /*!
  83. * @property
  84. * @brief Retrieves the name of the target node. The node doesn't
  85. * necessarily have to exist in the scene graph.
  86. */
  87. const String& GetTargetName() const;
  88. /*!
  89. * @property
  90. * @brief Sets the name of the target node. The node doesn't necessarily
  91. * have to exist in the scene graph. When a node is created that matches
  92. * this name, it is selected as the target.
  93. * @note This clears the existing target node.
  94. */
  95. void SetTargetName(const String& nodeName);
  96. /// Returns the current target position in world space.
  97. /// @property
  98. const Vector3& GetTargetPosition() const;
  99. /// Sets the current target position. If the effector has a target node then this will have no effect.
  100. /// @property
  101. void SetTargetPosition(const Vector3& targetPosition);
  102. /// Gets the current target rotation in world space.
  103. /// @property
  104. const Quaternion& GetTargetRotation() const;
  105. /// Sets the current target rotation. If the effector has a target node then this will have no effect.
  106. /// @property
  107. void SetTargetRotation(const Quaternion& targetRotation);
  108. /// Required for the editor, get the target rotation in euler angles.
  109. Vector3 GetTargetRotationEuler() const;
  110. /// Required for the editor, sets the target rotation in euler angles.
  111. void SetTargetRotationEuler(const Vector3& targetRotation);
  112. /// Returns the number of segments that will be affected by this effector. 0 Means all nodes between this effector and the next IKSolver.
  113. /// @property
  114. unsigned GetChainLength() const;
  115. /// Sets the number of segments that will be affected. 0 Means all nodes between this effector and the next IKSolver.
  116. /// @property
  117. void SetChainLength(unsigned chainLength);
  118. /// How strongly the effector affects the solution.
  119. /// @property
  120. float GetWeight() const;
  121. /*!
  122. * @property
  123. * @brief Sets how much influence the effector has on the solution.
  124. *
  125. * You can use this value to smoothly transition between a solved pose and
  126. * an initial pose For instance, lifting a foot off of the ground or
  127. * letting go of an object.
  128. */
  129. void SetWeight(float weight);
  130. /// How strongly the target node's rotation influences the solution.
  131. /// @property
  132. float GetRotationWeight() const;
  133. /*!
  134. * @property
  135. * @brief Sets how much influence the target rotation should have on the
  136. * solution. A value of 1 means to match the target rotation exactly, if
  137. * possible. A value of 0 means to not match it at all.
  138. * @note The solver must have target rotation enabled for this to have
  139. * any effect. See IKSolver::Feature::TARGET_ROTATIONS.
  140. */
  141. void SetRotationWeight(float weight);
  142. /// Retrieves the rotation decay factor. See SetRotationDecay() for info.
  143. /// @property
  144. float GetRotationDecay() const;
  145. /*!
  146. * @property
  147. * @brief A factor with which to control the target rotation influence of
  148. * the next segments down the chain.
  149. *
  150. * For example, if this is set to 0.5 and the rotation weight is set to
  151. * 1.0, then the first segment will match the target rotation exactly, the
  152. * next segment will match it only 50%, the next segment 25%, the next
  153. * 12.5%, etc. This parameter makes long chains look more natural when
  154. * matching a target rotation.
  155. */
  156. void SetRotationDecay(float decay);
  157. void DrawDebugGeometry(bool depthTest);
  158. void DrawDebugGeometry(DebugRenderer* debug, bool depthTest) override;
  159. private:
  160. friend class IKSolver;
  161. /// Intended to be used only by IKSolver.
  162. void SetIKSolver(IKSolver* solver);
  163. /// Intended to be used only by IKSolver.
  164. void SetIKEffectorNode(ik_node_t* effectorNode);
  165. /// Intended to be used by IKSolver. Copies the positions/rotations of the target node into the effector.
  166. void UpdateTargetNodePosition();
  167. public:
  168. /// Need these wrapper functions flags of GetFeature/SetFeature can be correctly exposed to the editor and to AngelScript and lua.
  169. bool GetWEIGHT_NLERP() const;
  170. bool GetINHERIT_PARENT_ROTATION() const;
  171. void SetWEIGHT_NLERP(bool enable);
  172. void SetINHERIT_PARENT_ROTATION(bool enable);
  173. private:
  174. WeakPtr<Node> targetNode_;
  175. WeakPtr<IKSolver> solver_;
  176. ik_node_t* ikEffectorNode_;
  177. String targetName_;
  178. Vector3 targetPosition_;
  179. Quaternion targetRotation_;
  180. unsigned chainLength_;
  181. float weight_;
  182. float rotationWeight_;
  183. float rotationDecay_;
  184. unsigned features_;
  185. };
  186. } // namespace Urho3D