Antkeeper  0.0.1
spring.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 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 
20 #ifndef ANTKEEPER_PHYSICS_SPRING_HPP
21 #define ANTKEEPER_PHYSICS_SPRING_HPP
22 
23 #include <engine/math/numbers.hpp>
25 
26 namespace physics {
27 
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
43 {
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);
48 
49  x = (x * f + v * dt + xt * ww_dtdt) * inv_det;
50  v = (v + (xt - x) * ww_dt) * inv_det;
51 }
52 
59 template <class T, class S>
61 {
62 public:
64  using value_type = T;
65 
67  using scalar_type = S;
68 
74  inline constexpr void solve(scalar_type dt) noexcept
75  {
76  solve_spring(m_value, m_velocity, m_target_value, m_damping_ratio, m_angular_frequency, dt);
77  }
78 
84  inline constexpr void set_value(const value_type& value) noexcept
85  {
86  m_value = value;
87  }
88 
94  inline constexpr void set_target_value(const value_type& value) noexcept
95  {
96  m_target_value = value;
97  }
98 
104  inline constexpr void set_velocity(const value_type& velocity) noexcept
105  {
106  m_velocity = velocity;
107  }
108 
114  inline constexpr void set_damping_ratio(scalar_type ratio) noexcept
115  {
116  m_damping_ratio = ratio;
117  }
118 
124  inline constexpr void set_angular_frequency(scalar_type angular_frequency) noexcept
125  {
126  m_angular_frequency = angular_frequency;
127  }
128 
134  inline constexpr void set_period(scalar_type period) noexcept
135  {
136  m_angular_frequency = s_to_rads(period);
137  }
138 
144  inline constexpr void set_frequency(scalar_type frequency) noexcept
145  {
146  m_angular_frequency = hz_to_rads(frequency);
147  }
148 
150  [[nodiscard]] inline constexpr const value_type& get_value() const noexcept
151  {
152  return m_value;
153  }
154 
156  [[nodiscard]] inline constexpr const value_type& get_target_value() const noexcept
157  {
158  return m_target_value;
159  }
160 
162  [[nodiscard]] inline constexpr const value_type& get_velocity() const noexcept
163  {
164  return m_velocity;
165  }
166 
168  [[nodiscard]] inline constexpr scalar_type get_damping_ratio() const noexcept
169  {
170  return m_damping_ratio;
171  }
172 
174  [[nodiscard]] inline constexpr scalar_type get_angular_frequency() const noexcept
175  {
176  return m_angular_frequency;
177  }
178 
180  [[nodiscard]] inline constexpr scalar_type get_period() const noexcept
181  {
182  return rads_to_s(m_angular_frequency);
183  }
184 
186  [[nodiscard]] inline constexpr scalar_type get_frequency() const noexcept
187  {
188  return rads_to_hz(m_angular_frequency);
189  }
190 
191 private:
192  value_type m_value{};
193  value_type m_target_value{};
194  value_type m_velocity{};
195  scalar_type m_damping_ratio{1};
196  scalar_type m_angular_frequency{math::two_pi<scalar_type>};
197 };
198 
199 } // namespace physics
200 
201 #endif // ANTKEEPER_PHYSICS_SPRING_HPP
Numeric spring.
Definition: spring.hpp:61
constexpr void set_damping_ratio(scalar_type ratio) noexcept
Sets the damping ratio of the spring.
Definition: spring.hpp:114
constexpr scalar_type get_frequency() const noexcept
Returns the oscillation frequency of the spring, in hertz.
Definition: spring.hpp:186
constexpr scalar_type get_angular_frequency() const noexcept
Returns the angular frequency of the spring oscillation, in radians per second.
Definition: spring.hpp:174
constexpr void set_period(scalar_type period) noexcept
Sets the oscillation period of the spring.
Definition: spring.hpp:134
S scalar_type
Scalar type.
Definition: spring.hpp:67
constexpr const value_type & get_value() const noexcept
Returns the current value of the spring.
Definition: spring.hpp:150
constexpr scalar_type get_damping_ratio() const noexcept
Returns the damping ratio of the spring.
Definition: spring.hpp:168
constexpr void set_target_value(const value_type &value) noexcept
Sets the target value of the spring.
Definition: spring.hpp:94
constexpr void set_value(const value_type &value) noexcept
Sets the current value of the spring.
Definition: spring.hpp:84
constexpr const value_type & get_target_value() const noexcept
Returns the target value of the spring.
Definition: spring.hpp:156
constexpr void solve(scalar_type dt) noexcept
Solves the spring using the implicit Euler method.
Definition: spring.hpp:74
constexpr scalar_type get_period() const noexcept
Returns the oscillation period of the spring, in seconds.
Definition: spring.hpp:180
T value_type
Value type.
Definition: spring.hpp:64
constexpr const value_type & get_velocity() const noexcept
Returns the velocity of the spring.
Definition: spring.hpp:162
constexpr void set_velocity(const value_type &velocity) noexcept
Sets the velocity of the spring.
Definition: spring.hpp:104
constexpr void set_angular_frequency(scalar_type angular_frequency) noexcept
Sets the angular frequency of the spring oscillation.
Definition: spring.hpp:124
constexpr void set_frequency(scalar_type frequency) noexcept
Sets the oscillation frequency of the spring.
Definition: spring.hpp:144
T period(T a, T gm)
Calculates the period of an elliptical orbit according to Kepler's third law.
Definition: elements.hpp:130
Physics.
Definition: constants.hpp:23
constexpr T rads_to_hz(T w) noexcept
Converts angular frequency to frequency.
Definition: frequency.hpp:52
constexpr T hz_to_rads(T f) noexcept
Converts frequency to angular frequency.
Definition: frequency.hpp:37
constexpr T rads_to_s(T w) noexcept
Converts angular frequency to oscillation period.
Definition: frequency.hpp:82
constexpr T s_to_rads(T t) noexcept
Converts oscillation period to angular frequency.
Definition: frequency.hpp:67
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.
Definition: spring.hpp:42