SmoothedTransform.cpp 5.3 KB

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