Spline.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // Copyright (c) 2008-2014 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. #pragma once
  23. #include "Component.h"
  24. #include "Vector3.h"
  25. namespace Urho3D
  26. {
  27. /// Interpolation Mode for a Spline.
  28. enum InterpolationMode
  29. {
  30. BEZIER_CURVE = 0
  31. };
  32. /// Spline for creating smooth movement based on Speed along a set of Control Points modified by the Interpolation Mode.
  33. class URHO3D_API Spline : public Component
  34. {
  35. OBJECT(Spline)
  36. public:
  37. /// Construct an Empty Spline.
  38. Spline(Context* context);
  39. /// Register object factory.
  40. static void RegisterObject(Context* context);
  41. /// Set the Control Points from an already defined set.
  42. void SetControlPoints(const PODVector<Vector3>& controlPoints);
  43. /// Set the Interpolation Mode.
  44. void SetInterpolationMode(InterpolationMode interpolationMode) { interpolationMode_ = interpolationMode; }
  45. /// Set the movement Speed.
  46. void SetSpeed(float speed) { speed_ = speed; }
  47. /// Set the parent node's position on the Spline.
  48. void SetPosition(float factor);
  49. /// Get the Control Points.
  50. const PODVector<Vector3>& GetControlPoints() const { return controlPoints_; }
  51. /// Get the Interpolation Mode.
  52. InterpolationMode GetInterpolationMode() const { return interpolationMode_; }
  53. /// Get the movement Speed.
  54. float GetSpeed() const { return speed_; }
  55. /// Get the parent node's last position on the spline.
  56. Vector3 GetPosition() const;
  57. /// Add a Control Point to the end.
  58. void Push(const Vector3& controlPoint);
  59. /// Remove a Control Point from the end.
  60. void Pop();
  61. /// Get a point on the spline from 0.f to 1.f where 0 is the start and 1 is the end.
  62. Vector3 GetPoint(float factor) const;
  63. /// Move the parent node to the next position along the Spline based off the Speed value.
  64. void Move(float timeStep);
  65. /// Reset movement along the path.
  66. void Reset();
  67. /// Returns whether the movement along the Spline complete.
  68. bool IsFinished() const { return traveled_ >= 1.0f; }
  69. VariantVector GetControlPointsAttr() const;
  70. void SetControlPointsAttr(VariantVector value);
  71. private:
  72. /// Calculate the length of the Spline. Used for movement calculations.
  73. void CalculateLength();
  74. /// Move the parent node along the Spline in Bezier Mode.
  75. Vector3 BezierMove(const PODVector<Vector3>& controlPoints, float t) const;
  76. /// The Control Points of the Spline.
  77. PODVector<Vector3> controlPoints_;
  78. /// The Interpolation Mode of the Spline.
  79. InterpolationMode interpolationMode_;
  80. /// The Speed of movement along the Spline.
  81. float speed_;
  82. /// Amount of time that has elapsed while moving.
  83. float elapsedTime_;
  84. /// The fraction of the Spline covered.
  85. float traveled_;
  86. /// The length of the Spline.
  87. float length_;
  88. /// Whether the length needs to be recalculated. Will only be true after a push or pop.
  89. bool dirty_;
  90. };
  91. }