TrajectoryHistory.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 <AzCore/Math/Vector3.h>
  10. #include <AzCore/Math/Color.h>
  11. #include <AzFramework/Entity/EntityDebugDisplayBus.h>
  12. #include <EMotionFX/Source/Pose.h>
  13. #include <EMotionFX/Source/KeyTrackLinearDynamic.h>
  14. namespace EMotionFX::MotionMatching
  15. {
  16. struct EMFX_API Sample
  17. {
  18. AZ_TYPE_INFO(Sample, "{6B67C064-08AF-431A-B236-82D3565D46A2}");
  19. AZ::Vector3 m_position = AZ::Vector3::CreateZero();
  20. AZ::Vector3 m_facingDirection = AZ::Vector3::CreateZero();
  21. };
  22. //! Used to store the trajectory history for the root motion (motion extraction node).
  23. //! The trajectory history is independent of the trajectory feature and captures a sample with every engine tick.
  24. //! The recorded history needs to record and track at least the time the trajectory feature/query requires.
  25. class EMFX_API TrajectoryHistory
  26. {
  27. public:
  28. void Init(const Pose& pose, size_t jointIndex, const AZ::Vector3& facingAxisDir, float numSecondsToTrack);
  29. void Clear();
  30. void Update(float timeDelta);
  31. void AddSample(const Pose& pose);
  32. using Sample = EMotionFX::MotionMatching::Sample;
  33. //! time in range [0, m_numSecondsToTrack]
  34. Sample Evaluate(float time) const;
  35. //! time in range [0, 1] where 0 is the current character position and 1 the oldest keyframe in the trajectory history
  36. Sample EvaluateNormalized(float normalizedTime) const;
  37. float GetNumSecondsToTrack() const { return m_numSecondsToTrack; }
  38. float GetCurrentTime() const { return m_currentTime; }
  39. size_t GetJointIndex() const { return m_jointIndex; }
  40. void DebugDraw(AzFramework::DebugDisplayRequests& debugDisplay, const AZ::Color& color, float timeStart = 0.0f) const;
  41. void DebugDrawSampled(AzFramework::DebugDisplayRequests& debugDisplay, size_t numSamples, const AZ::Color& color) const;
  42. private:
  43. void PrefillSamples(const Pose& pose, float timeDelta);
  44. KeyTrackLinearDynamic<Sample> m_keytrack;
  45. float m_numSecondsToTrack = 0.0f;
  46. size_t m_jointIndex = 0;
  47. float m_currentTime = 0.0f;
  48. AZ::Vector3 m_facingAxisDir; //! Facing direction of the character asset. (e.g. 0,1,0 when it is looking towards Y-axis)
  49. static constexpr float m_debugMarkerSize = 0.02f;
  50. };
  51. } // namespace EMotionFX::MotionMatching