SmoothedTransform.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // Copyright (c) 2008-2015 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 "../Precompiled.h"
  23. #include "../Core/Context.h"
  24. #include "../Scene/Scene.h"
  25. #include "../Scene/SceneEvents.h"
  26. #include "../Scene/SmoothedTransform.h"
  27. #include "../DebugNew.h"
  28. namespace Urho3D
  29. {
  30. SmoothedTransform::SmoothedTransform(Context* context) :
  31. Component(context),
  32. targetPosition_(Vector3::ZERO),
  33. targetRotation_(Quaternion::IDENTITY),
  34. smoothingMask_(SMOOTH_NONE),
  35. subscribed_(false)
  36. {
  37. }
  38. SmoothedTransform::~SmoothedTransform()
  39. {
  40. }
  41. void SmoothedTransform::RegisterObject(Context* context)
  42. {
  43. context->RegisterFactory<SmoothedTransform>();
  44. }
  45. void SmoothedTransform::Update(float constant, float squaredSnapThreshold)
  46. {
  47. if (smoothingMask_ && node_)
  48. {
  49. Vector3 position = node_->GetPosition();
  50. Quaternion rotation = node_->GetRotation();
  51. if (smoothingMask_ & SMOOTH_POSITION)
  52. {
  53. // If position snaps, snap everything to the end
  54. float delta = (position - targetPosition_).LengthSquared();
  55. if (delta > squaredSnapThreshold)
  56. constant = 1.0f;
  57. if (delta < M_EPSILON || constant >= 1.0f)
  58. {
  59. position = targetPosition_;
  60. smoothingMask_ &= ~SMOOTH_POSITION;
  61. }
  62. else
  63. position = position.Lerp(targetPosition_, constant);
  64. node_->SetPosition(position);
  65. }
  66. if (smoothingMask_ & SMOOTH_ROTATION)
  67. {
  68. float delta = (rotation - targetRotation_).LengthSquared();
  69. if (delta < M_EPSILON || constant >= 1.0f)
  70. {
  71. rotation = targetRotation_;
  72. smoothingMask_ &= ~SMOOTH_ROTATION;
  73. }
  74. else
  75. rotation = rotation.Slerp(targetRotation_, constant);
  76. node_->SetRotation(rotation);
  77. }
  78. }
  79. // If smoothing has completed, unsubscribe from the update event
  80. if (!smoothingMask_)
  81. {
  82. UnsubscribeFromEvent(GetScene(), E_UPDATESMOOTHING);
  83. subscribed_ = false;
  84. }
  85. }
  86. void SmoothedTransform::SetTargetPosition(const Vector3& position)
  87. {
  88. targetPosition_ = position;
  89. smoothingMask_ |= SMOOTH_POSITION;
  90. // Subscribe to smoothing update if not yet subscribed
  91. if (!subscribed_)
  92. {
  93. SubscribeToEvent(GetScene(), E_UPDATESMOOTHING, URHO3D_HANDLER(SmoothedTransform, HandleUpdateSmoothing));
  94. subscribed_ = true;
  95. }
  96. SendEvent(E_TARGETPOSITION);
  97. }
  98. void SmoothedTransform::SetTargetRotation(const Quaternion& rotation)
  99. {
  100. targetRotation_ = rotation;
  101. smoothingMask_ |= SMOOTH_ROTATION;
  102. if (!subscribed_)
  103. {
  104. SubscribeToEvent(GetScene(), E_UPDATESMOOTHING, URHO3D_HANDLER(SmoothedTransform, HandleUpdateSmoothing));
  105. subscribed_ = true;
  106. }
  107. SendEvent(E_TARGETROTATION);
  108. }
  109. void SmoothedTransform::SetTargetWorldPosition(const Vector3& position)
  110. {
  111. if (node_ && node_->GetParent())
  112. SetTargetPosition(node_->GetParent()->GetWorldTransform().Inverse() * position);
  113. else
  114. SetTargetPosition(position);
  115. }
  116. void SmoothedTransform::SetTargetWorldRotation(const Quaternion& rotation)
  117. {
  118. if (node_ && node_->GetParent())
  119. SetTargetRotation(node_->GetParent()->GetWorldRotation().Inverse() * rotation);
  120. else
  121. SetTargetRotation(rotation);
  122. }
  123. Vector3 SmoothedTransform::GetTargetWorldPosition() const
  124. {
  125. if (node_ && node_->GetParent())
  126. return node_->GetParent()->GetWorldTransform() * targetPosition_;
  127. else
  128. return targetPosition_;
  129. }
  130. Quaternion SmoothedTransform::GetTargetWorldRotation() const
  131. {
  132. if (node_ && node_->GetParent())
  133. return node_->GetParent()->GetWorldRotation() * targetRotation_;
  134. else
  135. return targetRotation_;
  136. }
  137. void SmoothedTransform::OnNodeSet(Node* node)
  138. {
  139. if (node)
  140. {
  141. // Copy initial target transform
  142. targetPosition_ = node->GetPosition();
  143. targetRotation_ = node->GetRotation();
  144. }
  145. }
  146. void SmoothedTransform::HandleUpdateSmoothing(StringHash eventType, VariantMap& eventData)
  147. {
  148. using namespace UpdateSmoothing;
  149. float constant = eventData[P_CONSTANT].GetFloat();
  150. float squaredSnapThreshold = eventData[P_SQUAREDSNAPTHRESHOLD].GetFloat();
  151. Update(constant, squaredSnapThreshold);
  152. }
  153. }