easing.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #pragma once
  2. #include <cmath>
  3. #include <utility>
  4. float lerp(float a, float b, float r);
  5. float linearRemap(float val, float fromMin, float fromMax, float toMin, float toMax);
  6. struct EasingEngine
  7. {
  8. float timer = 0;
  9. float deltaTime = 0;
  10. float runDeltaTime = 0;
  11. float result_ = 0;
  12. bool hasRestarted = 0;
  13. EasingEngine &update(float deltaTime)
  14. {
  15. this->deltaTime += deltaTime;
  16. this->runDeltaTime = this->deltaTime;
  17. this->result_ = 0;
  18. hasRestarted = 0;
  19. return *this;
  20. }
  21. EasingEngine &constant(float length, float val = 0)
  22. {
  23. if (runDeltaTime == 0) { return *this; }
  24. if (runDeltaTime > length)
  25. {
  26. runDeltaTime -= length;
  27. result_ += val;
  28. }
  29. else
  30. {
  31. //float ratio = runDeltaTime / length;
  32. result_ += val;
  33. runDeltaTime = 0;
  34. }
  35. return *this;
  36. }
  37. EasingEngine &goTowards(float length, float val = 0)
  38. {
  39. if (runDeltaTime == 0) { return *this; }
  40. if (runDeltaTime > length)
  41. {
  42. runDeltaTime -= length;
  43. result_ = val;
  44. }
  45. else
  46. {
  47. float ratio = runDeltaTime / length;
  48. result_ = lerp(result_, val, ratio);
  49. runDeltaTime = 0;
  50. }
  51. return *this;
  52. }
  53. EasingEngine &linear(float length, float start = 0, float end = 1)
  54. {
  55. if (runDeltaTime == 0) { return *this; }
  56. if (runDeltaTime > length)
  57. {
  58. runDeltaTime -= length;
  59. result_ += end;
  60. }
  61. else
  62. {
  63. float ratio = runDeltaTime / length;
  64. result_ += linearRemap(ratio, 0, 1, start, end);
  65. runDeltaTime = 0;
  66. }
  67. return *this;
  68. }
  69. EasingEngine &sin(float length, float speed = 1, float start = 0, float end = 1)
  70. {
  71. if (runDeltaTime == 0) { return *this; }
  72. if (runDeltaTime > length)
  73. {
  74. runDeltaTime -= length;
  75. result_ += linearRemap((std::sinf(length * speed) + 1.f)/2.f, 0, 1, start, end);
  76. }
  77. else
  78. {
  79. result_ += linearRemap((std::sinf(runDeltaTime * speed) + 1.f)/2.f, 0, 1, start, end);
  80. runDeltaTime = 0;
  81. }
  82. return *this;
  83. }
  84. EasingEngine &clamp(float min = 0, float max = 1)
  85. {
  86. result_ = std::min(std::max(result_, min), max);
  87. return *this;
  88. }
  89. //goes up and down using sin
  90. EasingEngine &burst(float length, float start = 0, float end = 1, float power = 1)
  91. {
  92. if (runDeltaTime == 0) { return *this; }
  93. if (runDeltaTime > length)
  94. {
  95. runDeltaTime -= length;
  96. result_ += start;
  97. }
  98. else
  99. {
  100. float ratio = runDeltaTime / length;
  101. result_ += linearRemap(std::powf(std::sinf(ratio*3.1415926), power)
  102. , 0, 1, start, end);
  103. runDeltaTime = 0;
  104. }
  105. return *this;
  106. }
  107. float result()
  108. {
  109. if (runDeltaTime != 0) { deltaTime = 0; hasRestarted = true; } //reset animation
  110. return result_;
  111. }
  112. };