SmoothedTransform.cpp 5.7 KB

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