SmoothedTransform.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "Scene.h"
  26. #include "SceneEvents.h"
  27. #include "SmoothedTransform.h"
  28. #include "DebugNew.h"
  29. static const float DEFAULT_SMOOTHING_CONSTANT = 50.0f;
  30. static const float DEFAULT_SNAP_THRESHOLD = 5.0f;
  31. OBJECTTYPESTATIC(SmoothedTransform);
  32. SmoothedTransform::SmoothedTransform(Context* context) :
  33. Component(context),
  34. targetPosition_(Vector3::ZERO),
  35. targetRotation_(Quaternion::IDENTITY),
  36. smoothingConstant_(DEFAULT_SMOOTHING_CONSTANT),
  37. snapThreshold_(DEFAULT_SNAP_THRESHOLD),
  38. smoothingMask_(SMOOTH_NONE),
  39. subscribed_(false)
  40. {
  41. }
  42. SmoothedTransform::~SmoothedTransform()
  43. {
  44. }
  45. void SmoothedTransform::RegisterObject(Context* context)
  46. {
  47. context->RegisterFactory<SmoothedTransform>();
  48. }
  49. void SmoothedTransform::Update(float constant, float squaredSnapThreshold)
  50. {
  51. if (smoothingMask_ && node_)
  52. {
  53. Vector3 position = node_->GetPosition();
  54. Quaternion rotation = node_->GetRotation();
  55. if (smoothingMask_ & SMOOTH_POSITION)
  56. {
  57. // If position snaps, snap everything to the end
  58. float delta = (position - targetPosition_).LengthSquared();
  59. if (delta > squaredSnapThreshold)
  60. constant = 1.0f;
  61. if (delta < M_EPSILON || constant >= 1.0f)
  62. {
  63. position = targetPosition_;
  64. smoothingMask_ &= ~SMOOTH_POSITION;
  65. }
  66. else
  67. position = position.Lerp(targetPosition_, constant);
  68. node_->SetPosition(position);
  69. }
  70. if (smoothingMask_ & SMOOTH_ROTATION)
  71. {
  72. float delta = (rotation - targetRotation_).LengthSquared();
  73. if (delta < M_EPSILON || constant >= 1.0f)
  74. {
  75. rotation = targetRotation_;
  76. smoothingMask_ &= ~SMOOTH_ROTATION;
  77. }
  78. else
  79. rotation = rotation.Slerp(targetRotation_, constant);
  80. node_->SetRotation(rotation);
  81. }
  82. }
  83. // If smoothing has completed, unsubscribe from the update event
  84. if (!smoothingMask_)
  85. {
  86. UnsubscribeFromEvent(GetScene(), E_UPDATESMOOTHING);
  87. subscribed_ = false;
  88. }
  89. }
  90. void SmoothedTransform::SetTargetPosition(const Vector3& position)
  91. {
  92. targetPosition_ = position;
  93. smoothingMask_ |= SMOOTH_POSITION;
  94. // Subscribe to smoothing update if not yet subscribed
  95. if (!subscribed_)
  96. {
  97. SubscribeToEvent(GetScene(), E_UPDATESMOOTHING, HANDLER(SmoothedTransform, HandleUpdateSmoothing));
  98. subscribed_ = true;
  99. }
  100. SendEvent(E_TARGETPOSITION);
  101. }
  102. void SmoothedTransform::SetTargetRotation(const Quaternion& rotation)
  103. {
  104. targetRotation_ = rotation;
  105. smoothingMask_ |= SMOOTH_ROTATION;
  106. if (!subscribed_)
  107. {
  108. SubscribeToEvent(GetScene(), E_UPDATESMOOTHING, HANDLER(SmoothedTransform, HandleUpdateSmoothing));
  109. subscribed_ = true;
  110. }
  111. SendEvent(E_TARGETROTATION);
  112. }
  113. void SmoothedTransform::SetTargetWorldPosition(const Vector3& position)
  114. {
  115. if (node_ && node_->GetParent())
  116. SetTargetPosition(node_->GetParent()->GetWorldTransform().Inverse() * position);
  117. else
  118. SetTargetPosition(position);
  119. }
  120. void SmoothedTransform::SetTargetWorldRotation(const Quaternion& rotation)
  121. {
  122. if (node_ && node_->GetParent())
  123. SetTargetRotation(node_->GetParent()->GetWorldRotation().Inverse() * rotation);
  124. else
  125. SetTargetRotation(rotation);
  126. }
  127. Vector3 SmoothedTransform::GetTargetWorldPosition() const
  128. {
  129. if (node_ && node_->GetParent())
  130. return node_->GetParent()->GetWorldTransform() * targetPosition_;
  131. else
  132. return targetPosition_;
  133. }
  134. Quaternion SmoothedTransform::GetTargetWorldRotation() const
  135. {
  136. if (node_ && node_->GetParent())
  137. return node_->GetParent()->GetWorldRotation() * targetRotation_;
  138. else
  139. return targetRotation_;
  140. }
  141. void SmoothedTransform::OnNodeSet(Node* node)
  142. {
  143. if (node)
  144. {
  145. // Copy initial target transform
  146. targetPosition_ = node->GetPosition();
  147. targetRotation_ = node->GetRotation();
  148. }
  149. }
  150. void SmoothedTransform::HandleUpdateSmoothing(StringHash eventType, VariantMap& eventData)
  151. {
  152. using namespace UpdateSmoothing;
  153. float constant = eventData[P_CONSTANT].GetFloat();
  154. float squaredSnapThreshold = eventData[P_SQUAREDSNAPTHRESHOLD].GetFloat();
  155. Update(constant, squaredSnapThreshold);
  156. }