SplinePath.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "ForEach.h"
  25. #include "Log.h"
  26. #include "Node.h"
  27. #include "Scene.h"
  28. #include "SceneEvents.h"
  29. #include "SplinePath.h"
  30. namespace Urho3D
  31. {
  32. extern const char* interpolationModeNames[];
  33. extern const char* LOGIC_CATEGORY;
  34. SplinePath::SplinePath(Context* context) :
  35. Component(context),
  36. speed_(1.f),
  37. elapsedTime_(0.f),
  38. length_(0.f),
  39. traveled_(0.f),
  40. spline_(BEZIER_CURVE)
  41. {
  42. SubscribeToEvent(E_SCENEPOSTUPDATE, HANDLER(SplinePath, PointsUpdated));
  43. }
  44. SplinePath::~SplinePath()
  45. {
  46. UnsubscribeFromAllEvents();
  47. }
  48. void SplinePath::RegisterObject(Context* context)
  49. {
  50. context->RegisterFactory<SplinePath>(LOGIC_CATEGORY);
  51. ATTRIBUTE(SplinePath, VAR_FLOAT, "Speed", speed_, 1.f, AM_FILE);
  52. ENUM_ACCESSOR_ATTRIBUTE(SplinePath, "Interpolation Mode", GetInterpolationMode, SetInterpolationMode, InterpolationMode, interpolationModeNames, BEZIER_CURVE, AM_FILE);
  53. ATTRIBUTE(SplinePath, VAR_FLOAT, "Traveled", traveled_, 0.f, AM_FILE | AM_NOEDIT);
  54. ATTRIBUTE(SplinePath, VAR_FLOAT, "Elapsed Time", elapsedTime_, 0.f, AM_FILE | AM_NOEDIT);
  55. }
  56. void SplinePath::ApplyAttributes()
  57. {
  58. SubscribeToEvent(node_->GetScene(), E_NODEADDED, HANDLER(SplinePath, PointAdded));
  59. SubscribeToEvent(node_->GetScene(), E_NODEREMOVED, HANDLER(SplinePath, PointRemoved));
  60. foreach(SharedPtr<Node> child, node_->GetChildren())
  61. child->AddListener(this);
  62. UpdatePoints();
  63. CalculateLength();
  64. }
  65. void SplinePath::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  66. {
  67. if (debug && node_ && IsEnabledEffective())
  68. {
  69. if (spline_.GetKnots().Size() > 1)
  70. {
  71. Vector3 a = spline_.GetPoint(0.f);
  72. for (float f = 0.01f; f <= 1.0f; f = f + 0.01f)
  73. {
  74. Vector3 b = spline_.GetPoint(f);
  75. debug->AddLine(a, b, Color::GREEN);
  76. a = b;
  77. }
  78. }
  79. foreach(SharedPtr<Node> node, node_->GetChildren())
  80. debug->AddNode(node);
  81. }
  82. }
  83. void SplinePath::SetInterpolationMode(InterpolationMode interpolationMode)
  84. {
  85. spline_.SetInterpolationMode(interpolationMode);
  86. dirty_ = true;
  87. }
  88. void SplinePath::SetPosition(float factor)
  89. {
  90. float t = factor;
  91. if (t < 0.f)
  92. t = 0.0f;
  93. else if (t > 1.0f)
  94. t = 1.0f;
  95. traveled_ = t;
  96. }
  97. Vector3 SplinePath::GetPosition() const
  98. {
  99. return GetPoint(traveled_);
  100. }
  101. Vector3 SplinePath::GetPoint(float factor) const
  102. {
  103. return spline_.GetPoint(factor);
  104. }
  105. void SplinePath::Move(float timeStep)
  106. {
  107. if (traveled_ >= 1.0f || length_ <= 0.0f)
  108. return;
  109. elapsedTime_ += timeStep;
  110. // Calculate where we should be on the spline based on length, speed and time. If that is less than the set traveled_ don't move till caught up.
  111. float distanceCovered = elapsedTime_ * speed_;
  112. traveled_ = distanceCovered / length_;
  113. GetPoint(traveled_);
  114. }
  115. void SplinePath::Reset()
  116. {
  117. traveled_ = 0.f;
  118. elapsedTime_ = 0.f;
  119. }
  120. void SplinePath::OnMarkedDirty(Node* node)
  121. {
  122. if (node)
  123. dirty_ = true;
  124. }
  125. void SplinePath::OnNodeSetEnabled(Node* node)
  126. {
  127. if (node)
  128. dirty_ = true;
  129. }
  130. void SplinePath::CalculateLength()
  131. {
  132. if (spline_.GetKnots().Size() <= 0)
  133. return;
  134. length_ = 0.f;
  135. Vector3 a = spline_.GetKnot(0);
  136. for (float f = 0.000f; f <= 1.000f; f += 0.001f)
  137. {
  138. Vector3 b = spline_.GetPoint(f);
  139. length_ += Abs((a - b).Length());
  140. a = b;
  141. }
  142. }
  143. void SplinePath::PointAdded(StringHash eventType, VariantMap& eventData)
  144. {
  145. using namespace NodeAdded;
  146. Node* parent = static_cast<Node*>(eventData[P_PARENT].GetPtr());
  147. Node* child = static_cast<Node*>(eventData[P_NODE].GetPtr());
  148. if (child != NULL && parent != NULL && parent == node_)
  149. {
  150. child->AddListener(this);
  151. dirty_ = true;
  152. }
  153. }
  154. void SplinePath::PointRemoved(StringHash eventType, VariantMap& eventData)
  155. {
  156. using namespace NodeRemoved;
  157. Node* parent = static_cast<Node*>(eventData[P_PARENT].GetPtr());
  158. if (parent != NULL && parent == node_)
  159. dirty_ = true;
  160. }
  161. void SplinePath::PointsUpdated(StringHash eventType, VariantMap& eventData)
  162. {
  163. if (dirty_)
  164. {
  165. UpdatePoints();
  166. CalculateLength();
  167. dirty_ = false;
  168. }
  169. }
  170. void SplinePath::UpdatePoints()
  171. {
  172. spline_.Clear();
  173. foreach(SharedPtr<Node> node, node_->GetChildren())
  174. {
  175. if (node->IsEnabled())
  176. spline_.AddKnot(node->GetWorldPosition());
  177. }
  178. }
  179. }