Antkeeper  0.0.1
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
spring-constraint.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_CONSTRAINT_HPP
21 #define ANTKEEPER_PHYSICS_SPRING_CONSTRAINT_HPP
22 
25 #include <engine/math/vector.hpp>
26 
27 namespace physics {
28 
33 {
34 public:
35  void solve(float dt) override;
36 
43  inline constexpr void attach_a(rigid_body& body_a, const math::fvec3& point_a) noexcept
44  {
45  m_body_a = &body_a;
46  m_point_a = point_a;
47  }
48 
55  inline constexpr void attach_b(rigid_body& body_b, const math::fvec3& point_b) noexcept
56  {
57  m_body_b = &body_b;
58  m_point_b = point_b;
59  }
60 
64  inline constexpr void detach_a() noexcept
65  {
66  m_body_a = nullptr;
67  }
68 
72  inline constexpr void detach_b() noexcept
73  {
74  m_body_b = nullptr;
75  }
76 
80  inline constexpr void detach() noexcept
81  {
82  detach_a();
83  detach_b();
84  }
85 
91  inline constexpr void set_resting_length(float length) noexcept
92  {
93  m_resting_length = length;
94  }
95 
101  inline constexpr void set_stiffness(float stiffness) noexcept
102  {
103  m_stiffness = stiffness;
104  }
105 
111  inline constexpr void set_damping(float damping) noexcept
112  {
113  m_damping = damping;
114  }
115 
117  [[nodiscard]] inline constexpr rigid_body* get_body_a() const noexcept
118  {
119  return m_body_a;
120  }
121 
123  [[nodiscard]] inline constexpr rigid_body* get_body_b() const noexcept
124  {
125  return m_body_b;
126  }
127 
129  [[nodiscard]] inline constexpr const math::fvec3& get_point_a() const noexcept
130  {
131  return m_point_a;
132  }
133 
135  [[nodiscard]] inline constexpr const math::fvec3& get_point_b() const noexcept
136  {
137  return m_point_b;
138  }
139 
141  [[nodiscard]] inline constexpr float get_resting_length() const noexcept
142  {
143  return m_resting_length;
144  }
145 
147  [[nodiscard]] inline constexpr float get_stiffness() const noexcept
148  {
149  return m_stiffness;
150  }
151 
153  [[nodiscard]] inline constexpr float get_damping() const noexcept
154  {
155  return m_damping;
156  }
157 
158 private:
160  rigid_body* m_body_a{};
161 
163  rigid_body* m_body_b{};
164 
166  math::fvec3 m_point_a{};
167 
169  math::fvec3 m_point_b{};
170 
172  float m_resting_length{};
173 
175  float m_stiffness{1.0f};
176 
178  float m_damping{1.0f};
179 };
180 
181 } // namespace physics
182 
183 #endif // ANTKEEPER_PHYSICS_SPRING_CONSTRAINT_HPP
Abstract base class for rigid body constraints.
Definition: constraint.hpp:29
constexpr void set_damping(float damping) noexcept
Sets the damping constant of the spring.
constexpr void attach_a(rigid_body &body_a, const math::fvec3 &point_a) noexcept
Attaches the spring to body a.
void solve(float dt) override
Solves the constraint.
constexpr const math::fvec3 & get_point_b() const noexcept
Returns the point at which the spring is attached to body b, in body-space.
constexpr void set_resting_length(float length) noexcept
Sets the resting length of the spring.
constexpr void attach_b(rigid_body &body_b, const math::fvec3 &point_b) noexcept
Attaches the spring to body b.
constexpr float get_stiffness() const noexcept
Returns the stiffness constant of the spring.
constexpr rigid_body * get_body_a() const noexcept
Returns the body to which the spring is attached at point a.
constexpr void detach_a() noexcept
Detaches the spring from body a.
constexpr rigid_body * get_body_b() const noexcept
Returns the body to which the spring is attached at point b.
constexpr const math::fvec3 & get_point_a() const noexcept
Returns the point at which the spring is attached to body a, in body-space.
constexpr float get_damping() const noexcept
Returns the damping constant of the spring.
constexpr void set_stiffness(float stiffness) noexcept
Sets the stiffness constant of the spring.
constexpr void detach() noexcept
Detaches the spring from bodies a and b.
constexpr float get_resting_length() const noexcept
Returns the resting length of the spring, in meters.
constexpr void detach_b() noexcept
Detaches the spring from body b.
T length(const quaternion< T > &q)
Calculates the length of a quaternion.
Definition: quaternion.hpp:602
Physics.
Definition: constants.hpp:23
n-dimensional vector.
Definition: vector.hpp:44