SmoothedTransform.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #pragma once
  24. #include "Component.h"
  25. /// No ongoing smoothing.
  26. static const unsigned SMOOTH_NONE = 0;
  27. /// Ongoing position smoothing.
  28. static const unsigned SMOOTH_POSITION = 1;
  29. /// Ongoing rotation smoothing.
  30. static const unsigned SMOOTH_ROTATION = 2;
  31. /// Transform smoothing component for network updates.
  32. class SmoothedTransform : public Component
  33. {
  34. OBJECT(SmoothedTransform);
  35. public:
  36. /// Construct.
  37. SmoothedTransform(Context* context);
  38. /// Destruct.
  39. ~SmoothedTransform();
  40. /// Register object factory.
  41. static void RegisterObject(Context* context);
  42. /// Update smoothing.
  43. void Update(float constant, float squaredSnapThreshold);
  44. /// %Set target position relative to parent node.
  45. void SetTargetPosition(const Vector3& position);
  46. /// %Set target rotation relative to parent node.
  47. void SetTargetRotation(const Quaternion& rotation);
  48. /// %Set target position in world space.
  49. void SetTargetWorldPosition(const Vector3& position);
  50. /// %Set target rotation in world space.
  51. void SetTargetWorldRotation(const Quaternion& rotation);
  52. /// Return target position relative to parent node.
  53. const Vector3& GetTargetPosition() const { return targetPosition_; }
  54. /// Return target rotation relative to parent node.
  55. const Quaternion& GetTargetRotation() const { return targetRotation_; }
  56. /// Return target position in world space.
  57. Vector3 GetTargetWorldPosition() const;
  58. /// Return target rotation in world space.
  59. Quaternion GetTargetWorldRotation() const;
  60. /// Return whether smoothing is in progress.
  61. bool IsActive() const { return smoothingMask_ != 0; }
  62. protected:
  63. /// Handle scene node being assigned at creation.
  64. virtual void OnNodeSet(Node* node);
  65. private:
  66. /// Handle smoothing update event.
  67. void HandleUpdateSmoothing(StringHash eventType, VariantMap& eventData);
  68. /// Target position.
  69. Vector3 targetPosition_;
  70. /// Target rotation.
  71. Quaternion targetRotation_;
  72. /// Smoothing constant.
  73. float smoothingConstant_;
  74. /// Position snap threshold.
  75. float snapThreshold_;
  76. /// Active smoothing operations bitmask.
  77. unsigned char smoothingMask_;
  78. };