Symphony Of Empires
value_chase.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <chrono>
4 #include <glm/common.hpp>
5 
6 namespace Eng3D {
7  template <typename T>
8  class ValueChase {
9  float friction;
10  std::chrono::system_clock::time_point last_time;
11  public:
12  constexpr ValueChase(float _friction) noexcept
13  : friction{ _friction }
14  {
15 
16  }
17 
18  constexpr T move_towards(T current, T target) {
19  auto now = std::chrono::system_clock::now();
20  float time = std::chrono::duration_cast<std::chrono::milliseconds>(now - last_time).count();
21  time /= 1e3;
22  time *= (1.f / friction);
23  time = glm::min(time, 1.f);
24  last_time = now;
25  T distance = target - current;
26  T step = distance * time;
27  // const float tolerance = 10.f;
28  // T new_current = glm::round((current + step) * tolerance) / tolerance;
29  return current + step;
30  }
31  };
32 }
constexpr T move_towards(T current, T target)
Definition: value_chase.hpp:18
constexpr ValueChase(float _friction) noexcept
Definition: value_chase.hpp:12