SmoothedTransform.cpp 5.4 KB

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