SmoothedTransform.cpp 5.5 KB

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