IKEffector.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. struct ik_effector_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. /// Constructs a new IK effector.
  35. IKEffector(Context* context);
  36. /// Destructs he IK effector.
  37. virtual ~IKEffector();
  38. /// Registers this class as an object factory.
  39. static void RegisterObject(Context* context);
  40. /// Retrieves the node that is being used as a target. Can be NULL.
  41. Node* GetTargetNode() const;
  42. /*!
  43. * @brief The position of the target node provides the target position of
  44. * the effector node.
  45. *
  46. * The IK chain is solved such that the node to which this component is
  47. * attached to will try to move to the position of the target node.
  48. * @param targetNode A scene node that acts as a target. Specifying NULL
  49. * will erase the target and cause the solver to ignore this chain.
  50. * @note You will get very strange behaviour if you specify a target node
  51. * that is part the IK chain being solved for (circular dependency). Don't
  52. * do that.
  53. */
  54. void SetTargetNode(Node* targetNode);
  55. /*!
  56. * @brief Retrieves the name of the target node. The node doesn't
  57. * necessarily have to exist in the scene graph.
  58. */
  59. const String& GetTargetName() const;
  60. /*!
  61. * @brief Sets the name of the target node. The node doesn't necessarily
  62. * have to exist in the scene graph. When a node is created that matches
  63. * this name, it is selected as the target.
  64. * @note This clears the existing target node.
  65. */
  66. void SetTargetName(const String& nodeName);
  67. /// Returns the current target position in world space.
  68. const Vector3& GetTargetPosition() const;
  69. /// Sets the current target position. If the effector has a target node then this will have no effect.
  70. void SetTargetPosition(const Vector3& targetPosition);
  71. /// Gets the current target rotation in world space.
  72. const Quaternion& GetTargetRotation() const;
  73. /// Sets the current target rotation. If the effector has a target node then this will have no effect.
  74. void SetTargetRotation(const Quaternion& targetRotation);
  75. /// Required for the editor, get the target rotation in euler angles
  76. Vector3 GetTargetRotationEuler() const;
  77. /// Required for the editor, sets the target rotation in euler angles
  78. void SetTargetRotationEuler(const Vector3& targetRotation);
  79. /// Returns the number of segments that will be affected by this effector. 0 Means all nodes between this effector and the next IKSolver.
  80. unsigned GetChainLength() const;
  81. /// Sets the number of segments that will be affected. 0 Means all nodes between this effector and the next IKSolver.
  82. void SetChainLength(unsigned chainLength);
  83. /// How strongly the effector affects the solution.
  84. float GetWeight() const;
  85. /*!
  86. * @brief Sets how much influence the effector has on the solution.
  87. *
  88. * You can use this value to smoothly transition between a solved pose and
  89. * an initial pose For instance, lifting a foot off of the ground or
  90. * letting go of an object.
  91. */
  92. void SetWeight(float weight);
  93. /// How strongly the target node's rotation influences the solution
  94. float GetRotationWeight() const;
  95. /*!
  96. * @brief Sets how much influence the target rotation should have on the
  97. * solution. A value of 1 means to match the target rotation exactly, if
  98. * possible. A value of 0 means to not match it at all.
  99. * @note The solver must have target rotation enabled for this to have
  100. * any effect. See IKSolver::EnableTargetRotation().
  101. */
  102. void SetRotationWeight(float weight);
  103. /// Retrieves the rotation decay factor. See SetRotationDecay() for info.
  104. float GetRotationDecay() const;
  105. /*!
  106. * @brief A factor with which to control the target rotation influence of
  107. * the next segments down the chain.
  108. *
  109. * For example, if this is set to 0.5 and the rotation weight is set to
  110. * 1.0, then the first segment will match the target rotation exactly, the
  111. * next segment will match it only 50%, the next segment 25%, the next
  112. * 12.5%, etc. This parameter makes long chains look more natural when
  113. * matching a target rotation.
  114. */
  115. void SetRotationDecay(float decay);
  116. /// Whether or not to nlerp instead of lerp when transitioning with the weight parameter
  117. bool WeightedNlerpEnabled() const;
  118. /*!
  119. * @brief If you set the effector weight (see SetWeight()) to a value in
  120. * between 0 and 1, the default behaviour is to linearly interpolate the
  121. * effector's target position. If the solved tree and the initial tree
  122. * are far apart, this can look very strange, especially if you are
  123. * controlling limbs on a character that are designed to rotation. Enabling
  124. * this causes a rotational based interpolation (nlerp) around the chain's
  125. * base node and makes transitions look much more natural.
  126. */
  127. void EnableWeightedNlerp(bool enable);
  128. bool InheritParentRotationEnabled() const;
  129. void EnableInheritParentRotation(bool enable);
  130. void DrawDebugGeometry(bool depthTest);
  131. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  132. private:
  133. friend class IKSolver;
  134. /// Intended to be used only by IKSolver
  135. void SetIKSolver(IKSolver* solver);
  136. /// Intended to be used only by IKSolver
  137. void SetIKEffector(ik_effector_t* effector);
  138. /// Intended to be used by IKSolver. Copies the positions/rotations of the target node into the effector
  139. void UpdateTargetNodePosition();
  140. WeakPtr<Node> targetNode_;
  141. WeakPtr<IKSolver> solver_;
  142. ik_effector_t* ikEffector_;
  143. String targetName_;
  144. Vector3 targetPosition_;
  145. Quaternion targetRotation_;
  146. unsigned chainLength_;
  147. float weight_;
  148. float rotationWeight_;
  149. float rotationDecay_;
  150. bool weightedNlerp_;
  151. bool inheritParentRotation_;
  152. };
  153. } // namespace Urho3D