SmoothedTransform.cpp 5.7 KB

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