💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.3 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_SPRING_HPP
  20. #define ANTKEEPER_SPRING_HPP
  21. /**
  22. * Contains the variables required for numeric springing.
  23. *
  24. * @tparam T Value type.
  25. * @tparam S Scalar type.
  26. *
  27. * @see spring()
  28. * @see solve_numeric_spring()
  29. */
  30. template <typename T, typename S>
  31. struct numeric_spring
  32. {
  33. T x0; ///< Start value
  34. T x1; ///< End value
  35. T v; ///< Velocity
  36. S z; ///< Damping ratio, which can be undamped (z = 0), underdamped (z < 1), critically damped (z = 1), or overdamped (z > 1).
  37. S w; ///< Angular frequency of the oscillation, in radians per second (2pi = 1Hz).
  38. };
  39. /**
  40. * Performs numeric, damped springing on a value and velocity.
  41. *
  42. * @tparam T Value type.
  43. * @tparam S Scalar type.
  44. *
  45. * @param[in,out] x0 Start value, which will be oscillated by this function.
  46. * @param[in,out] v Velocity, which will be modified by this function.
  47. * @param[in] x1 End value.
  48. * @param[in] z Damping ratio, which can be undamped (z = 0), underdamped (z < 1), critically damped (z = 1), or overdamped (z > 1).
  49. * @param[in] w Angular frequency of the oscillation, in radians per second (2pi = 1Hz).
  50. * @param[in] dt Delta time, in seconds.
  51. */
  52. template <typename T, typename S>
  53. void spring(T& x0, T& v, const T& x1, S z, S w, S dt);
  54. /**
  55. * Solves a numeric spring using the spring() function.
  56. *
  57. * @param[in,out] ns Numeric spring to be sovled.
  58. * @param dt Delta time, in seconds.
  59. *
  60. * @see spring()
  61. */
  62. template <typename T, typename S>
  63. void solve_numeric_spring(numeric_spring<T, S>& ns, S dt);
  64. /**
  65. * Converts a frequency from hertz to radians per second.
  66. *
  67. * @param hz Frequency in hertz.
  68. * @return Frequency in radians per second.
  69. */
  70. template <typename T>
  71. T hz_to_rads(T hz);
  72. /**
  73. * Converts a frequency from radians per second to hertz.
  74. *
  75. * @param rads Frequency in radians per second.
  76. * @return Frequency in hertz.
  77. */
  78. template <typename T>
  79. T rads_to_hz(T rads);
  80. template <typename T, typename S>
  81. void spring(T& x0, T& v, const T& x1, S z, S w, S dt)
  82. {
  83. const S w2_dt = w * w * dt;
  84. const S w2_dt2 = w2_dt * dt;
  85. const S f = z * w * dt * S(2) + S(1);
  86. const T det_x = x0 * f + v * dt + x1 * w2_dt2;
  87. const T det_v = v + (x1 - x0) * w2_dt;
  88. const S inv_det = S(1) / (f + w2_dt2);
  89. x0 = det_x * inv_det;
  90. v = det_v * inv_det;
  91. }
  92. template <typename T, typename S>
  93. void solve_numeric_spring(numeric_spring<T, S>& ns, S dt)
  94. {
  95. spring(ns.x0, ns.v, ns.x1, ns.z, ns.w, dt);
  96. }
  97. template <typename T>
  98. inline T hz_to_rads(T hz)
  99. {
  100. return hz * math::two_pi<T>;
  101. }
  102. template <typename T>
  103. inline T rads_to_hz(T rads)
  104. {
  105. return rads / math::two_pi<T>;
  106. }
  107. #endif // ANTKEEPER_SPRING_HPP