IKEffector.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //
  2. // Copyright (c) 2008-2016 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. typedef struct ik_node_t ik_node_t;
  26. namespace Atomic
  27. {
  28. class Context;
  29. class IKSolver;
  30. class ATOMIC_API IKEffector : public Component
  31. {
  32. ATOMIC_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. IKEffector(Context* context);
  55. /// Destructs he IK effector.
  56. virtual ~IKEffector();
  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. bool GetFeature(Feature feature) const;
  61. /// Enable or disable a certain feature (see IKEffector::Feature)
  62. void SetFeature(Feature feature, bool enable);
  63. /// Retrieves the node that is being used as a target. Can be NULL.
  64. Node* GetTargetNode() const;
  65. /*!
  66. * @brief The position of the target node provides the target position of
  67. * the effector node.
  68. *
  69. * The IK chain is solved such that the node to which this component is
  70. * attached to will try to move to the position of the target node.
  71. * @param targetNode A scene node that acts as a target. Specifying NULL
  72. * will erase the target and cause the solver to ignore this chain.
  73. * @note You will get very strange behaviour if you specify a target node
  74. * that is part the IK chain being solved for (circular dependency). Don't
  75. * do that.
  76. */
  77. void SetTargetNode(Node* targetNode);
  78. /*!
  79. * @brief Retrieves the name of the target node. The node doesn't
  80. * necessarily have to exist in the scene graph.
  81. */
  82. const String& GetTargetName() const;
  83. /*!
  84. * @brief Sets the name of the target node. The node doesn't necessarily
  85. * have to exist in the scene graph. When a node is created that matches
  86. * this name, it is selected as the target.
  87. * @note This clears the existing target node.
  88. */
  89. void SetTargetName(const String& nodeName);
  90. /// Returns the current target position in world space.
  91. const Vector3& GetTargetPosition() const;
  92. /// Sets the current target position. If the effector has a target node then this will have no effect.
  93. void SetTargetPosition(const Vector3& targetPosition);
  94. /// Gets the current target rotation in world space.
  95. const Quaternion& GetTargetRotation() const;
  96. /// Sets the current target rotation. If the effector has a target node then this will have no effect.
  97. void SetTargetRotation(const Quaternion& targetRotation);
  98. /// Required for the editor, get the target rotation in euler angles
  99. Vector3 GetTargetRotationEuler() const;
  100. /// Required for the editor, sets the target rotation in euler angles
  101. void SetTargetRotationEuler(const Vector3& targetRotation);
  102. /// Returns the number of segments that will be affected by this effector. 0 Means all nodes between this effector and the next IKSolver.
  103. unsigned GetChainLength() const;
  104. /// Sets the number of segments that will be affected. 0 Means all nodes between this effector and the next IKSolver.
  105. void SetChainLength(unsigned chainLength);
  106. /// How strongly the effector affects the solution.
  107. float GetWeight() const;
  108. /*!
  109. * @brief Sets how much influence the effector has on the solution.
  110. *
  111. * You can use this value to smoothly transition between a solved pose and
  112. * an initial pose For instance, lifting a foot off of the ground or
  113. * letting go of an object.
  114. */
  115. void SetWeight(float weight);
  116. /// How strongly the target node's rotation influences the solution
  117. float GetRotationWeight() const;
  118. /*!
  119. * @brief Sets how much influence the target rotation should have on the
  120. * solution. A value of 1 means to match the target rotation exactly, if
  121. * possible. A value of 0 means to not match it at all.
  122. * @note The solver must have target rotation enabled for this to have
  123. * any effect. See IKSolver::Feature::TARGET_ROTATIONS.
  124. */
  125. void SetRotationWeight(float weight);
  126. /// Retrieves the rotation decay factor. See SetRotationDecay() for info.
  127. float GetRotationDecay() const;
  128. /*!
  129. * @brief A factor with which to control the target rotation influence of
  130. * the next segments down the chain.
  131. *
  132. * For example, if this is set to 0.5 and the rotation weight is set to
  133. * 1.0, then the first segment will match the target rotation exactly, the
  134. * next segment will match it only 50%, the next segment 25%, the next
  135. * 12.5%, etc. This parameter makes long chains look more natural when
  136. * matching a target rotation.
  137. */
  138. void SetRotationDecay(float decay);
  139. void DrawDebugGeometry(bool depthTest);
  140. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  141. private:
  142. friend class IKSolver;
  143. /// Intended to be used only by IKSolver
  144. void SetIKSolver(IKSolver* solver);
  145. /// Intended to be used only by IKSolver
  146. void SetIKEffectorNode(ik_node_t* effector);
  147. /// Intended to be used by IKSolver. Copies the positions/rotations of the target node into the effector
  148. void UpdateTargetNodePosition();
  149. public:
  150. /// Need these wrapper functions flags of GetFeature/SetFeature can be correctly exposed to the editor and to AngelScript and lua
  151. bool GetWEIGHT_NLERP() const;
  152. bool GetINHERIT_PARENT_ROTATION() const;
  153. void SetWEIGHT_NLERP(bool enable);
  154. void SetINHERIT_PARENT_ROTATION(bool enable);
  155. private:
  156. WeakPtr<Node> targetNode_;
  157. WeakPtr<IKSolver> solver_;
  158. ik_node_t* ikEffectorNode_;
  159. String targetName_;
  160. Vector3 targetPosition_;
  161. Quaternion targetRotation_;
  162. unsigned chainLength_;
  163. float weight_;
  164. float rotationWeight_;
  165. float rotationDecay_;
  166. unsigned features_;
  167. };
  168. } // namespace Atomic