SmoothedTransform.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // Copyright (c) 2008-2020 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. /// \file
  23. #pragma once
  24. #include "../Scene/Component.h"
  25. namespace Urho3D
  26. {
  27. enum SmoothingType : unsigned
  28. {
  29. /// No ongoing smoothing.
  30. SMOOTH_NONE = 0,
  31. /// Ongoing position smoothing.
  32. SMOOTH_POSITION = 1,
  33. /// Ongoing rotation smoothing.
  34. SMOOTH_ROTATION = 2,
  35. };
  36. URHO3D_FLAGSET(SmoothingType, SmoothingTypeFlags);
  37. /// Transform smoothing component for network updates.
  38. class URHO3D_API SmoothedTransform : public Component
  39. {
  40. URHO3D_OBJECT(SmoothedTransform, Component);
  41. public:
  42. /// Construct.
  43. explicit SmoothedTransform(Context* context);
  44. /// Destruct.
  45. ~SmoothedTransform() override;
  46. /// Register object factory.
  47. static void RegisterObject(Context* context);
  48. /// Update smoothing.
  49. void Update(float constant, float squaredSnapThreshold);
  50. /// Set target position in parent space.
  51. /// @property
  52. void SetTargetPosition(const Vector3& position);
  53. /// Set target rotation in parent space.
  54. /// @property
  55. void SetTargetRotation(const Quaternion& rotation);
  56. /// Set target position in world space.
  57. /// @property
  58. void SetTargetWorldPosition(const Vector3& position);
  59. /// Set target rotation in world space.
  60. /// @property
  61. void SetTargetWorldRotation(const Quaternion& rotation);
  62. /// Return target position in parent space.
  63. /// @property
  64. const Vector3& GetTargetPosition() const { return targetPosition_; }
  65. /// Return target rotation in parent space.
  66. /// @property
  67. const Quaternion& GetTargetRotation() const { return targetRotation_; }
  68. /// Return target position in world space.
  69. /// @property
  70. Vector3 GetTargetWorldPosition() const;
  71. /// Return target rotation in world space.
  72. /// @property
  73. Quaternion GetTargetWorldRotation() const;
  74. /// Return whether smoothing is in progress.
  75. /// @property
  76. bool IsInProgress() const { return smoothingMask_ != SMOOTH_NONE; }
  77. protected:
  78. /// Handle scene node being assigned at creation.
  79. void OnNodeSet(Node* node) override;
  80. private:
  81. /// Handle smoothing update event.
  82. void HandleUpdateSmoothing(StringHash eventType, VariantMap& eventData);
  83. /// Target position.
  84. Vector3 targetPosition_;
  85. /// Target rotation.
  86. Quaternion targetRotation_;
  87. /// Active smoothing operations bitmask.
  88. SmoothingTypeFlags smoothingMask_;
  89. /// Subscribed to smoothing update event flag.
  90. bool subscribed_;
  91. };
  92. }