20 #ifndef ANTKEEPER_PHYSICS_SPRING_HPP
21 #define ANTKEEPER_PHYSICS_SPRING_HPP
41 template <
class T,
class S>
42 constexpr
void solve_spring(T& x, T& v,
const T& xt, S z, S w, S dt) noexcept
44 const auto ww_dt = w * w * dt;
45 const auto ww_dtdt = ww_dt * dt;
46 const auto f = z * w * dt * S{2} + S{1};
47 const auto inv_det = S{1} / (f + ww_dtdt);
49 x = (x * f + v * dt + xt * ww_dtdt) * inv_det;
50 v = (v + (xt - x) * ww_dt) * inv_det;
59 template <
class T,
class S>
76 solve_spring(m_value, m_velocity, m_target_value, m_damping_ratio, m_angular_frequency, dt);
96 m_target_value = value;
106 m_velocity = velocity;
116 m_damping_ratio = ratio;
126 m_angular_frequency = angular_frequency;
158 return m_target_value;
170 return m_damping_ratio;
176 return m_angular_frequency;
196 scalar_type m_angular_frequency{math::two_pi<scalar_type>};
constexpr void set_damping_ratio(scalar_type ratio) noexcept
Sets the damping ratio of the spring.
constexpr scalar_type get_frequency() const noexcept
Returns the oscillation frequency of the spring, in hertz.
constexpr scalar_type get_angular_frequency() const noexcept
Returns the angular frequency of the spring oscillation, in radians per second.
constexpr void set_period(scalar_type period) noexcept
Sets the oscillation period of the spring.
S scalar_type
Scalar type.
constexpr const value_type & get_value() const noexcept
Returns the current value of the spring.
constexpr scalar_type get_damping_ratio() const noexcept
Returns the damping ratio of the spring.
constexpr void set_target_value(const value_type &value) noexcept
Sets the target value of the spring.
constexpr void set_value(const value_type &value) noexcept
Sets the current value of the spring.
constexpr const value_type & get_target_value() const noexcept
Returns the target value of the spring.
constexpr void solve(scalar_type dt) noexcept
Solves the spring using the implicit Euler method.
constexpr scalar_type get_period() const noexcept
Returns the oscillation period of the spring, in seconds.
constexpr const value_type & get_velocity() const noexcept
Returns the velocity of the spring.
constexpr void set_velocity(const value_type &velocity) noexcept
Sets the velocity of the spring.
constexpr void set_angular_frequency(scalar_type angular_frequency) noexcept
Sets the angular frequency of the spring oscillation.
constexpr void set_frequency(scalar_type frequency) noexcept
Sets the oscillation frequency of the spring.
T period(T a, T gm)
Calculates the period of an elliptical orbit according to Kepler's third law.
constexpr T rads_to_hz(T w) noexcept
Converts angular frequency to frequency.
constexpr T hz_to_rads(T f) noexcept
Converts frequency to angular frequency.
constexpr T rads_to_s(T w) noexcept
Converts angular frequency to oscillation period.
constexpr T s_to_rads(T t) noexcept
Converts oscillation period to angular frequency.
constexpr void solve_spring(T &x, T &v, const T &xt, S z, S w, S dt) noexcept
Solves a numeric spring using the implicit Euler method.