SmoothedTransform.cpp 5.5 KB

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