IKEffector.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. #include "../Core/Context.h"
  23. #include "../Graphics/DebugRenderer.h"
  24. #include "../IK/IKConverters.h"
  25. #include "../IK/IKEffector.h"
  26. #include "../IK/IKEvents.h"
  27. #include "../IK/IKSolver.h"
  28. #include "../Scene/SceneEvents.h"
  29. #include <ik/effector.h>
  30. namespace Urho3D
  31. {
  32. extern const char* IK_CATEGORY;
  33. // ----------------------------------------------------------------------------
  34. IKEffector::IKEffector(Context* context) :
  35. Component(context),
  36. ikEffector_(NULL),
  37. chainLength_(0),
  38. weight_(1.0f),
  39. rotationWeight_(1.0),
  40. rotationDecay_(0.25),
  41. weightedNlerp_(false),
  42. inheritParentRotation_(false)
  43. {
  44. }
  45. // ----------------------------------------------------------------------------
  46. IKEffector::~IKEffector()
  47. {
  48. }
  49. // ----------------------------------------------------------------------------
  50. void IKEffector::RegisterObject(Context* context)
  51. {
  52. context->RegisterFactory<IKEffector>(IK_CATEGORY);
  53. URHO3D_ACCESSOR_ATTRIBUTE("Target Node", GetTargetName, SetTargetName, String, String::EMPTY, AM_DEFAULT);
  54. URHO3D_ACCESSOR_ATTRIBUTE("Chain Length", GetChainLength, SetChainLength, unsigned, 0, AM_DEFAULT);
  55. URHO3D_ACCESSOR_ATTRIBUTE("Target Position", GetTargetPosition, SetTargetPosition, Vector3, Vector3::ZERO, AM_DEFAULT);
  56. URHO3D_MIXED_ACCESSOR_ATTRIBUTE("Target Rotation", GetTargetRotationEuler, SetTargetRotationEuler, Vector3, Vector3::ZERO, AM_DEFAULT);
  57. URHO3D_ACCESSOR_ATTRIBUTE("Weight", GetWeight, SetWeight, float, 1.0, AM_DEFAULT);
  58. URHO3D_ACCESSOR_ATTRIBUTE("Rotation Weight", GetRotationWeight, SetRotationWeight, float, 1.0, AM_DEFAULT);
  59. URHO3D_ACCESSOR_ATTRIBUTE("Rotation Decay", GetRotationDecay, SetRotationDecay, float, 0.25, AM_DEFAULT);
  60. URHO3D_ACCESSOR_ATTRIBUTE("Nlerp Weight", WeightedNlerpEnabled, EnableWeightedNlerp, bool, false, AM_DEFAULT);
  61. URHO3D_ACCESSOR_ATTRIBUTE("Inherit Parent Rotation", InheritParentRotationEnabled, EnableInheritParentRotation, bool, false, AM_DEFAULT);
  62. }
  63. // ----------------------------------------------------------------------------
  64. Node* IKEffector::GetTargetNode() const
  65. {
  66. return targetNode_;
  67. }
  68. // ----------------------------------------------------------------------------
  69. void IKEffector::SetTargetNode(Node* targetNode)
  70. {
  71. using namespace IKEffectorTargetChanged;
  72. VariantMap& eventData = GetEventDataMap();
  73. eventData[P_EFFECTORNODE] = node_; // The effector node that has changed targets
  74. eventData[P_TARGETNODE] = targetNode_; // The new target node. NOTE: Can be NULL (means no target)
  75. SendEvent(E_IKEFFECTORTARGETCHANGED, eventData);
  76. // Finally change the target node
  77. targetNode_ = targetNode;
  78. targetName_ = targetNode != NULL ? targetNode->GetName() : "";
  79. }
  80. // ----------------------------------------------------------------------------
  81. const String& IKEffector::GetTargetName() const
  82. {
  83. return targetName_;
  84. }
  85. // ----------------------------------------------------------------------------
  86. void IKEffector::SetTargetName(const String& nodeName)
  87. {
  88. targetName_ = nodeName;
  89. targetNode_ = NULL;
  90. }
  91. // ----------------------------------------------------------------------------
  92. const Vector3& IKEffector::GetTargetPosition() const
  93. {
  94. return targetPosition_;
  95. }
  96. // ----------------------------------------------------------------------------
  97. void IKEffector::SetTargetPosition(const Vector3& targetPosition)
  98. {
  99. targetPosition_ = targetPosition;
  100. if (ikEffector_ != NULL)
  101. ikEffector_->target_position = Vec3Urho2IK(targetPosition);
  102. }
  103. // ----------------------------------------------------------------------------
  104. const Quaternion& IKEffector::GetTargetRotation() const
  105. {
  106. return targetRotation_;
  107. }
  108. // ----------------------------------------------------------------------------
  109. void IKEffector::SetTargetRotation(const Quaternion& targetRotation)
  110. {
  111. targetRotation_ = targetRotation;
  112. if (ikEffector_)
  113. ikEffector_->target_rotation = QuatUrho2IK(targetRotation);
  114. }
  115. // ----------------------------------------------------------------------------
  116. Vector3 IKEffector::GetTargetRotationEuler() const
  117. {
  118. return targetRotation_.EulerAngles();
  119. }
  120. // ----------------------------------------------------------------------------
  121. void IKEffector::SetTargetRotationEuler(const Vector3& targetRotation)
  122. {
  123. SetTargetRotation(Quaternion(targetRotation.x_, targetRotation.y_, targetRotation.z_));
  124. }
  125. // ----------------------------------------------------------------------------
  126. unsigned int IKEffector::GetChainLength() const
  127. {
  128. return chainLength_;
  129. }
  130. // ----------------------------------------------------------------------------
  131. void IKEffector::SetChainLength(unsigned chainLength)
  132. {
  133. chainLength_ = chainLength;
  134. if (ikEffector_ != NULL)
  135. {
  136. ikEffector_->chain_length = chainLength;
  137. solver_->MarkSolverTreeDirty();
  138. }
  139. }
  140. // ----------------------------------------------------------------------------
  141. float IKEffector::GetWeight() const
  142. {
  143. return weight_;
  144. }
  145. // ----------------------------------------------------------------------------
  146. void IKEffector::SetWeight(float weight)
  147. {
  148. weight_ = Clamp(weight, 0.0f, 1.0f);
  149. if (ikEffector_ != NULL)
  150. ikEffector_->weight = weight_;
  151. }
  152. // ----------------------------------------------------------------------------
  153. float IKEffector::GetRotationWeight() const
  154. {
  155. return rotationWeight_;
  156. }
  157. // ----------------------------------------------------------------------------
  158. void IKEffector::SetRotationWeight(float weight)
  159. {
  160. rotationWeight_ = Clamp(weight, 0.0f, 1.0f);
  161. if (ikEffector_ != NULL)
  162. ikEffector_->rotation_weight = rotationWeight_;
  163. }
  164. // ----------------------------------------------------------------------------
  165. float IKEffector::GetRotationDecay() const
  166. {
  167. return rotationDecay_;
  168. }
  169. // ----------------------------------------------------------------------------
  170. void IKEffector::SetRotationDecay(float decay)
  171. {
  172. rotationDecay_ = Clamp(decay, 0.0f, 1.0f);
  173. if (ikEffector_ != NULL)
  174. ikEffector_->rotation_decay = rotationDecay_;
  175. }
  176. // ----------------------------------------------------------------------------
  177. bool IKEffector::WeightedNlerpEnabled() const
  178. {
  179. return weightedNlerp_;
  180. }
  181. // ----------------------------------------------------------------------------
  182. void IKEffector::EnableWeightedNlerp(bool enable)
  183. {
  184. weightedNlerp_ = enable;
  185. if (ikEffector_ != NULL)
  186. {
  187. ikEffector_->flags &= ~EFFECTOR_WEIGHT_NLERP;
  188. if (enable)
  189. ikEffector_->flags |= EFFECTOR_WEIGHT_NLERP;
  190. }
  191. }
  192. // ----------------------------------------------------------------------------
  193. bool IKEffector::InheritParentRotationEnabled() const
  194. {
  195. return inheritParentRotation_;
  196. }
  197. // ----------------------------------------------------------------------------
  198. void IKEffector::EnableInheritParentRotation(bool enable)
  199. {
  200. inheritParentRotation_ = enable;
  201. if(ikEffector_ != NULL)
  202. {
  203. ikEffector_->flags &= ~EFFECTOR_INHERIT_PARENT_ROTATION;
  204. if (enable)
  205. ikEffector_->flags |= EFFECTOR_INHERIT_PARENT_ROTATION;
  206. }
  207. }
  208. // ----------------------------------------------------------------------------
  209. void IKEffector::UpdateTargetNodePosition()
  210. {
  211. if (targetNode_ == NULL)
  212. {
  213. SetTargetNode(node_->GetScene()->GetChild(targetName_, true));
  214. if (targetNode_ == NULL)
  215. return;
  216. }
  217. SetTargetPosition(targetNode_->GetWorldPosition());
  218. SetTargetRotation(targetNode_->GetWorldRotation());
  219. }
  220. // ----------------------------------------------------------------------------
  221. void IKEffector::DrawDebugGeometry(bool depthTest)
  222. {
  223. DebugRenderer* debug = GetScene()->GetComponent<DebugRenderer>();
  224. if (debug)
  225. DrawDebugGeometry(debug, depthTest);
  226. }
  227. // ----------------------------------------------------------------------------
  228. void IKEffector::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  229. {
  230. Node* terminationNode;
  231. if (solver_ == NULL)
  232. terminationNode = GetScene();
  233. else
  234. terminationNode = solver_->GetNode();
  235. // Calculate average length of all segments so we can determine the radius
  236. // of the debug spheres to draw
  237. int chainLength = chainLength_ == 0 ? -1 : chainLength_;
  238. Node* a = node_;
  239. float averageLength = 0.0f;
  240. unsigned numberOfSegments = 0;
  241. while (a && a != terminationNode->GetParent() && chainLength-- != 0)
  242. {
  243. averageLength += a->GetPosition().Length();
  244. ++numberOfSegments;
  245. a = a->GetParent();
  246. }
  247. averageLength /= numberOfSegments;
  248. // connect all chained nodes together with lines
  249. chainLength = chainLength_ == 0 ? -1 : chainLength_;
  250. a = node_;
  251. Node* b = a->GetParent();
  252. debug->AddSphere(
  253. Sphere(a->GetWorldPosition(), averageLength * 0.1f),
  254. Color::YELLOW,
  255. depthTest
  256. );
  257. while (b && b != terminationNode->GetParent() && chainLength-- != 0)
  258. {
  259. debug->AddLine(
  260. a->GetWorldPosition(),
  261. b->GetWorldPosition(),
  262. Color::WHITE,
  263. depthTest
  264. );
  265. debug->AddSphere(
  266. Sphere(b->GetWorldPosition(), averageLength * 0.1f),
  267. Color::YELLOW,
  268. depthTest
  269. );
  270. a = b;
  271. b = b->GetParent();
  272. }
  273. Vector3 direction = targetRotation_ * Vector3::FORWARD;
  274. direction = direction * averageLength + targetPosition_;
  275. debug->AddSphere(Sphere(targetPosition_, averageLength * 0.2f), Color(255, 128, 0), depthTest);
  276. debug->AddLine(targetPosition_, direction, Color(255, 128, 0), depthTest);
  277. }
  278. // ----------------------------------------------------------------------------
  279. void IKEffector::SetIKEffector(ik_effector_t* effector)
  280. {
  281. ikEffector_ = effector;
  282. if (effector)
  283. {
  284. effector->target_position = Vec3Urho2IK(targetPosition_);
  285. effector->target_rotation = QuatUrho2IK(targetRotation_);
  286. effector->weight = weight_;
  287. effector->rotation_weight = rotationWeight_;
  288. effector->rotation_decay = rotationDecay_;
  289. effector->chain_length = chainLength_;
  290. EnableWeightedNlerp(weightedNlerp_);
  291. }
  292. }
  293. // ----------------------------------------------------------------------------
  294. void IKEffector::SetIKSolver(IKSolver* solver)
  295. {
  296. solver_ = solver;
  297. }
  298. } // namespace Urho3D