FollowingCameraComponent.h 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include "FollowingCameraConfiguration.h"
  10. #include <AzCore/Component/Component.h>
  11. #include <AzCore/Component/TickBus.h>
  12. #include <AzFramework/Components/TransformComponent.h>
  13. #include <AzFramework/Input/Events/InputChannelEventListener.h>
  14. namespace ROS2
  15. {
  16. //! The component used for cameras that follow moving objects
  17. //! It allows to switch between different cameras attached to entities, and to control the active camera using keyboard.
  18. class FollowingCameraComponent
  19. : public AZ::Component
  20. , public AZ::TickBus::Handler
  21. , public AzFramework::InputChannelEventListener
  22. {
  23. public:
  24. FollowingCameraComponent() = default;
  25. FollowingCameraComponent(const FollowingCameraConfiguration& configuration);
  26. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  27. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  28. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  29. static void Reflect(AZ::ReflectContext* reflection);
  30. AZ_COMPONENT(FollowingCameraComponent, "{6a21768a-f327-11ed-a05b-0242ac120003}", AZ::Component);
  31. // AZ::Component
  32. void Activate() override;
  33. void Deactivate() override;
  34. private:
  35. // AZ::TickBus overrides ..
  36. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  37. // AzFramework::InputChannelEventListener overrides ...
  38. bool OnInputChannelEventFiltered(const AzFramework::InputChannel& inputChannel) override;
  39. void OnKeyboardEvent(const AzFramework::InputChannel& inputChannel);
  40. //! Remove the tilt from the transform.
  41. //! The tilt is removed by projecting the transform basis of rotation matrix to the ground plane.
  42. //! @param transform The transform to remove the tilt.
  43. //! @return The transform without tilt.
  44. AZ::Transform RemoveTiltFromTransform(AZ::Transform transform);
  45. //! Compute weighted average of the vectors in the buffer.
  46. //! @param buffer The buffer to compute the average.
  47. //! @return The average vector.
  48. AZ::Vector3 AverageVector(const AZStd::deque<AZStd::pair<AZ::Vector3, float>>& buffer) const;
  49. //! Compute weighted average of translation in the buffer.
  50. //! @return The average translation.
  51. AZ::Vector3 SmoothTranslation() const;
  52. //! Compute weighted average of rotation in the buffer.
  53. //! @return The average rotation.
  54. AZ::Quaternion SmoothRotation() const;
  55. //! Cache the transform in smoothing buffer.
  56. //! @param transform The transform to cache.
  57. //! @param deltaTime The time between the last frame and the current frame.
  58. void CacheTransform(const AZ::Transform& transform, float deltaTime);
  59. //! The smoothing buffer for translation, the first element is the translation, the second element is the weight.
  60. AZStd::deque<AZStd::pair<AZ::Vector3, float>> m_lastTranslationsBuffer;
  61. //! The smoothing buffer for rotation, the first element is the tangential vector, the second element is the weight.
  62. AZStd::deque<AZStd::pair<AZ::Quaternion, float>> m_lastRotationsBuffer;
  63. float m_rotationOffset = 0.0f; //!< The rotation change from the input.
  64. float m_opticalAxisTranslation = 0.0f; //!< The zoom change from the input.
  65. AZ::EntityId m_currentView; //!< Current used view point.
  66. bool m_disableCameraMove = false; //!< Disable camera move (used when fly camera is selected).
  67. FollowingCameraConfiguration m_configuration; //!< The configuration of the following camera.
  68. };
  69. } // namespace ROS2