💿🐜 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.

136 lines
3.6 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. #include "math/constants.hpp"
  22. /**
  23. * Contains the variables required for numeric springing.
  24. *
  25. * @tparam T Value type.
  26. * @tparam S Scalar type.
  27. *
  28. * @see spring()
  29. * @see solve_numeric_spring()
  30. */
  31. template <typename T, typename S>
  32. struct numeric_spring
  33. {
  34. T x0; ///< Start value
  35. T x1; ///< End value
  36. T v; ///< Velocity
  37. S z; ///< Damping ratio, which can be undamped (z = 0), underdamped (z < 1), critically damped (z = 1), or overdamped (z > 1).
  38. S w; ///< Angular frequency of the oscillation, in radians per second (2pi = 1Hz).
  39. };
  40. /**
  41. * Solves a number spring using the implicit Euler method.
  42. *
  43. * @tparam T Value type.
  44. * @tparam S Scalar type.
  45. *
  46. * @param[in,out] x0 Start value, which will be oscillated by this function.
  47. * @param[in,out] v Velocity, which will be modified by this function.
  48. * @param[in] x1 End value.
  49. * @param[in] z Damping ratio, which can be undamped (z = 0), underdamped (z < 1), critically damped (z = 1), or overdamped (z > 1).
  50. * @param[in] w Angular frequency of the oscillation, in radians per second (2pi = 1Hz).
  51. * @param[in] dt Delta time, in seconds.
  52. */
  53. template <typename T, typename S>
  54. void spring(T& x0, T& v, const T& x1, S z, S w, S dt);
  55. /**
  56. * Solves a number spring using the implicit Euler method.
  57. *
  58. * @param[in,out] ns Numeric spring to be sovled.
  59. * @param dt Delta time, in seconds.
  60. *
  61. * @see spring()
  62. */
  63. template <typename T, typename S>
  64. void solve_numeric_spring(numeric_spring<T, S>& ns, S dt);
  65. /**
  66. * Converts a frequency from hertz to radians per second.
  67. *
  68. * @param hz Frequency in hertz.
  69. * @return Frequency in radians per second.
  70. */
  71. template <typename T>
  72. T hz_to_rads(T hz);
  73. /**
  74. * Converts a frequency from radians per second to hertz.
  75. *
  76. * @param rads Frequency in radians per second.
  77. * @return Frequency in hertz.
  78. */
  79. template <typename T>
  80. T rads_to_hz(T rads);
  81. /**
  82. * Converts a period from seconds to radians per second.
  83. *
  84. * @param t Period, in seconds.
  85. * @return Angular frequency, in radians per second.
  86. */
  87. template <typename T>
  88. T period_to_rads(T t);
  89. template <typename T, typename S>
  90. void spring(T& x0, T& v, const T& x1, S z, S w, S dt)
  91. {
  92. const S ww_dt = w * w * dt;
  93. const S ww_dtdt = ww_dt * dt;
  94. const S f = z * w * dt * S{2} + S{1};
  95. const T det_x = x0 * f + v * dt + x1 * ww_dtdt;
  96. const T det_v = v + (x1 - x0) * ww_dt;
  97. const S inv_det = S{1} / (f + ww_dtdt);
  98. x0 = det_x * inv_det;
  99. v = det_v * inv_det;
  100. }
  101. template <typename T, typename S>
  102. void solve_numeric_spring(numeric_spring<T, S>& ns, S dt)
  103. {
  104. spring(ns.x0, ns.v, ns.x1, ns.z, ns.w, dt);
  105. }
  106. template <typename T>
  107. inline T hz_to_rads(T hz)
  108. {
  109. return hz * math::two_pi<T>;
  110. }
  111. template <typename T>
  112. inline T rads_to_hz(T rads)
  113. {
  114. return rads / math::two_pi<T>;
  115. }
  116. template <typename T>
  117. inline T period_to_rads(T t)
  118. {
  119. return math::two_pi<T> / t;
  120. }
  121. #endif // ANTKEEPER_SPRING_HPP