SmoothedTransform.cpp 5.5 KB

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