ParabolicEase.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. // ============================================================================
  19. // Copyright (C) 2003, 2004 Electronic Arts
  20. //
  21. // ParabolicEase.h
  22. // Ease in and out based on a parabolic function.
  23. // Author: Robert Minsk May 12, 2003
  24. // ============================================================================
  25. #pragma once
  26. #ifndef _PARABOLICEASE_H
  27. #define _PARABOLICEASE_H
  28. // ============================================================================
  29. #include "Lib/BaseType.h"
  30. // ============================================================================
  31. /// Ease in and out based on a linear velocity.
  32. /**
  33. * This ends up being a function that is parabolic at both ends and a linear
  34. * middle section with respect to position.
  35. *
  36. * velocity(0.0) = 0.0
  37. * velocity(in) = v0
  38. * velocity(out) = v0
  39. * velocity(1.0) = 0.0
  40. *
  41. * From 0.0->in velocity is linearly increasing.
  42. * From out->1.0 velocity is linearly decreasing.
  43. *
  44. * velocity(t) = v0*t/in t = [0, in] Linear increasing segment
  45. * = v0 t = (in, out] Constant segment
  46. * = (1-t)*v0/(1-out) t = (out, 1.0] Linear decreasing segment
  47. *
  48. * We need to calculate v0. We want the total distance covered to be 1.0.
  49. *
  50. * 1 = integral(velocity(t), 0, 1)
  51. * 1 = integral(velocity(t), 0, in) +
  52. * integral(velocity(t), in, out) +
  53. * integral(velocity(t), out, 1.0)
  54. * 1 = v0*in/2 + v0*(out - in) + v0*(1 - out)/2
  55. * = v0*(out-in+1)/2
  56. * v0 = 2/(out-in+1)
  57. *
  58. * Now we can calculate the distance function.
  59. *
  60. * d(0->in) = integral(velocity(t), 0, s)
  61. * = v0*s*s/(2*in)
  62. * d(in->out) = d(0->in) + integral(velocity(t), in, s)
  63. * = (v0*in/2) + (v0*(s - in))
  64. * d(out->1) = d(0->in) + d(in->out) + integral(velocity(t), out, s)
  65. * = (v0*in/2) + (v0*(out - in)) + (s-s*s/2-out+out*out/2)*v0/(1-out)
  66. */
  67. class ParabolicEase
  68. {
  69. public:
  70. explicit ParabolicEase(Real easeInTime = 0.0f, Real easeOutTime = 0.0f)
  71. { setEaseTimes(easeInTime, easeOutTime); }
  72. /// Initialize the ease-in/ease-out function.
  73. /**
  74. * \param easeInTime/\param easeOutTime is the amount of time to
  75. * accomplish the transition. The time is normalized from 0 to 1.
  76. */
  77. void setEaseTimes(Real easeInTime, Real easeOutTime);
  78. /// Evaluate the ease-in/ease-out function at time \param param.
  79. /**
  80. * \param param is normalized from 0 to 1.
  81. */
  82. Real operator ()(Real param) const;
  83. private:
  84. Real m_in, m_out;
  85. };
  86. // ============================================================================
  87. #endif // _PARABOLICEASE_H