Antkeeper  0.0.1
rigid-body.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_RIGID_BODY_HPP
21 #define ANTKEEPER_PHYSICS_RIGID_BODY_HPP
22 
23 #include <engine/math/vector.hpp>
27 #include <memory>
28 
29 namespace physics {
30 
35 {
36 public:
42  inline constexpr void set_transform(const math::transform<float>& transform) noexcept
43  {
44  m_current_transform = transform;
45  }
46 
52  inline constexpr void set_position(const math::fvec3& position) noexcept
53  {
54  m_current_transform.translation = position;
55  }
56 
62  inline constexpr void set_orientation(const math::fquat& orientation) noexcept
63  {
64  m_current_transform.rotation = orientation;
65  }
66 
73  inline constexpr void set_scale(const math::fvec3& scale) noexcept
74  {
75  m_current_transform.scale = scale;
76  }
77  inline constexpr void set_scale(float scale) noexcept
78  {
79  m_current_transform.scale = {scale, scale, scale};
80  }
82 
88  inline constexpr void set_previous_transform(const math::transform<float>& transform) noexcept
89  {
90  m_previous_transform = transform;
91  }
92 
98  inline constexpr void set_previous_position(const math::fvec3& position) noexcept
99  {
100  m_previous_transform.translation = position;
101  }
102 
108  inline constexpr void set_previous_orientation(const math::fquat& orientation) noexcept
109  {
110  m_previous_transform.rotation = orientation;
111  }
112 
119  inline constexpr void set_previous_scale(const math::fvec3& scale) noexcept
120  {
121  m_previous_transform.scale = scale;
122  }
123  inline constexpr void set_previous_scale(float scale) noexcept
124  {
125  m_previous_transform.scale = {scale, scale, scale};
126  }
128 
134  inline constexpr void set_center_of_mass(const math::fvec3& point) noexcept
135  {
136  m_center_of_mass = point;
137  }
138 
144  inline constexpr void set_mass(float mass) noexcept
145  {
146  m_mass = mass;
147  m_inverse_mass = (mass) ? 1.0f / mass : 0.0f;
148  }
149 
155  inline constexpr void set_inertia(float inertia) noexcept
156  {
157  m_inertia = inertia;
158  m_inverse_inertia = (inertia) ? 1.0f / inertia : 0.0f;
159  }
160 
166  inline void set_collider(std::shared_ptr<collider> collider) noexcept
167  {
168  m_collider = collider;
169  }
170 
176  inline constexpr void set_linear_damping(float damping) noexcept
177  {
178  m_linear_damping = damping;
179  }
180 
186  inline constexpr void set_angular_damping(float damping) noexcept
187  {
188  m_angular_damping = damping;
189  }
190 
196  inline constexpr void set_linear_momentum(const math::fvec3& momentum) noexcept
197  {
198  m_linear_momentum = momentum;
199  m_linear_velocity = m_inverse_mass * m_linear_momentum;
200  }
201 
207  inline constexpr void set_angular_momentum(const math::fvec3& momentum) noexcept
208  {
209  m_angular_momentum = momentum;
210  m_angular_velocity = m_inverse_inertia * m_angular_momentum;
211  }
212 
218  inline constexpr void set_linear_velocity(const math::fvec3& velocity) noexcept
219  {
220  m_linear_velocity = velocity;
221  m_linear_momentum = m_mass * m_linear_velocity;
222  }
223 
229  inline constexpr void set_angular_velocity(const math::fvec3& velocity) noexcept
230  {
231  m_angular_velocity = velocity;
232  m_angular_momentum = m_inertia * m_angular_velocity;
233  }
234 
236  [[nodiscard]] inline constexpr const math::transform<float>& get_transform() const noexcept
237  {
238  return m_current_transform;
239  }
240 
242  [[nodiscard]] inline constexpr const math::fvec3& get_position() const noexcept
243  {
244  return m_current_transform.translation;
245  }
246 
248  [[nodiscard]] inline constexpr const math::fquat& get_orientation() const noexcept
249  {
250  return m_current_transform.rotation;
251  }
252 
254  [[nodiscard]] inline constexpr const math::fvec3& get_scale() const noexcept
255  {
256  return m_current_transform.scale;
257  }
258 
260  [[nodiscard]] inline constexpr const math::transform<float>& get_previous_transform() const noexcept
261  {
262  return m_previous_transform;
263  }
264 
266  [[nodiscard]] inline constexpr const math::fvec3& get_previous_position() const noexcept
267  {
268  return m_previous_transform.translation;
269  }
270 
272  [[nodiscard]] inline constexpr const math::fquat& get_previous_orientation() const noexcept
273  {
274  return m_previous_transform.rotation;
275  }
276 
278  [[nodiscard]] inline constexpr const math::fvec3& get_previous_scale() const noexcept
279  {
280  return m_previous_transform.scale;
281  }
282 
284  [[nodiscard]] inline constexpr const math::fvec3& get_center_of_mass() const noexcept
285  {
286  return m_center_of_mass;
287  }
288 
290  [[nodiscard]] inline constexpr float get_mass() const noexcept
291  {
292  return m_mass;
293  }
294 
296  [[nodiscard]] inline constexpr float get_inverse_mass() const noexcept
297  {
298  return m_inverse_mass;
299  }
300 
302  [[nodiscard]] inline constexpr float get_inertia() const noexcept
303  {
304  return m_inertia;
305  }
306 
308  [[nodiscard]] inline constexpr float get_inverse_inertia() const noexcept
309  {
310  return m_inverse_inertia;
311  }
312 
314  [[nodiscard]] inline constexpr float get_linear_damping() const noexcept
315  {
316  return m_linear_damping;
317  }
318 
320  [[nodiscard]] inline constexpr float get_angular_damping() const noexcept
321  {
322  return m_angular_damping;
323  }
324 
326  [[nodiscard]] inline constexpr const std::shared_ptr<collider>& get_collider() const noexcept
327  {
328  return m_collider;
329  }
330 
332  [[nodiscard]] inline constexpr const math::fvec3& get_linear_momentum() const noexcept
333  {
334  return m_linear_momentum;
335  }
336 
338  [[nodiscard]] inline constexpr const math::fvec3& get_angular_momentum() const noexcept
339  {
340  return m_angular_momentum;
341  }
342 
344  [[nodiscard]] inline constexpr const math::fvec3& get_linear_velocity() const noexcept
345  {
346  return m_linear_velocity;
347  }
348 
350  [[nodiscard]] inline constexpr const math::fvec3& get_angular_velocity() const noexcept
351  {
352  return m_angular_velocity;
353  }
354 
356  [[nodiscard]] inline constexpr const math::fvec3& get_applied_force() const noexcept
357  {
358  return m_applied_force;
359  }
360 
362  [[nodiscard]] inline constexpr const math::fvec3& get_applied_torque() const noexcept
363  {
364  return m_applied_torque;
365  }
366 
374  [[nodiscard]] inline constexpr math::fvec3 get_point_velocity(const math::fvec3& radius) const noexcept
375  {
376  return m_linear_velocity + math::cross(m_angular_velocity, radius);
377  }
378 
382  [[nodiscard]] inline constexpr bool is_static() const noexcept
383  {
384  return (m_mass == 0.0f);
385  }
386 
393  inline constexpr void apply_force(const math::fvec3& force, const math::fvec3& radius) noexcept
394  {
395  m_applied_force += force;
396  m_applied_torque += math::cross(radius, force);
397  }
398 
404  inline constexpr void apply_central_force(const math::fvec3& force) noexcept
405  {
406  m_applied_force += force;
407  }
408 
414  inline constexpr void apply_torque(const math::fvec3& torque) noexcept
415  {
416  m_applied_torque += torque;
417  }
418 
425  inline constexpr void apply_impulse(const math::fvec3& impulse, const math::fvec3& radius) noexcept
426  {
427  m_linear_momentum += impulse;
428  m_angular_momentum += math::cross(radius, impulse);
429 
430  // Update velocities
431  m_linear_velocity = m_inverse_mass * m_linear_momentum;
432  m_angular_velocity = m_inverse_inertia * m_angular_momentum;
433  }
434 
440  inline constexpr void apply_central_impulse(const math::fvec3& impulse) noexcept
441  {
442  m_linear_momentum += impulse;
443 
444  // Update linear velocity
445  m_linear_velocity = m_inverse_mass * m_linear_momentum;
446  }
447 
453  inline constexpr void apply_torque_impulse(const math::fvec3& torque) noexcept
454  {
455  m_angular_momentum += torque;
456 
457  // Update angular velocity
458  m_angular_velocity = m_inverse_inertia * m_angular_momentum;
459  }
460 
462  inline constexpr void clear_applied_forces() noexcept
463  {
464  m_applied_force = {};
465  m_applied_torque = {};
466  }
467 
473  void integrate_forces(float dt) noexcept;
474 
480  void integrate_velocities(float dt) noexcept;
481 
487  inline void integrate(float dt) noexcept
488  {
489  integrate_forces(dt);
491  }
492 
500  [[nodiscard]] math::transform<float> interpolate(float alpha) const;
501 
502 private:
505 
508 
510  math::fvec3 m_center_of_mass{};
511 
513  float m_mass{1.0f};
514 
516  float m_inverse_mass{1.0f};
517 
519  float m_inertia{1.0f};
520 
522  float m_inverse_inertia{1.0f};
523 
525  float m_linear_damping{};
526 
528  float m_angular_damping{};
529 
531  std::shared_ptr<collider> m_collider;
532 
534  math::fvec3 m_linear_momentum{};
535 
537  math::fvec3 m_angular_momentum{};
538 
540  math::fvec3 m_linear_velocity{};
541 
543  math::fvec3 m_angular_velocity{};
544 
546  math::fvec3 m_applied_force{};
547 
549  math::fvec3 m_applied_torque{};
550 };
551 
552 } // namespace physics
553 
554 #endif // ANTKEEPER_PHYSICS_RIGID_BODY_HPP
Abstract base class for collision objects.
Definition: collider.hpp:34
void integrate_velocities(float dt) noexcept
Integrates velocities, updating the center of mass and orientation of the rigid body.
Definition: rigid-body.cpp:45
constexpr void set_previous_scale(float scale) noexcept
Sets the previous scale of the rigid body.
Definition: rigid-body.hpp:123
constexpr void set_orientation(const math::fquat &orientation) noexcept
Sets the current orientation of the rigid body.
Definition: rigid-body.hpp:62
constexpr const math::fvec3 & get_linear_velocity() const noexcept
Returns the linear velocity of the rigid body, in m/s.
Definition: rigid-body.hpp:344
constexpr const math::fvec3 & get_applied_torque() const noexcept
Returns the total pre-integrated torque, in N⋅m.
Definition: rigid-body.hpp:362
constexpr float get_angular_damping() const noexcept
Returns the angular damping factor of the rigid body.
Definition: rigid-body.hpp:320
constexpr const std::shared_ptr< collider > & get_collider() const noexcept
Returns the collider of the rigid body.
Definition: rigid-body.hpp:326
constexpr const math::fquat & get_orientation() const noexcept
Returns the current orientation of the rigid body.
Definition: rigid-body.hpp:248
constexpr void set_angular_velocity(const math::fvec3 &velocity) noexcept
Sets the angular velocity of the rigid body.
Definition: rigid-body.hpp:229
constexpr void set_center_of_mass(const math::fvec3 &point) noexcept
Sets the center of mass of the rigid body.
Definition: rigid-body.hpp:134
constexpr void set_inertia(float inertia) noexcept
Sets the moment of inertia of the rigid body.
Definition: rigid-body.hpp:155
constexpr float get_linear_damping() const noexcept
Returns the linear damping factor of the rigid body.
Definition: rigid-body.hpp:314
constexpr const math::fvec3 & get_previous_scale() const noexcept
Returns the previous scale of the rigid body.
Definition: rigid-body.hpp:278
constexpr void clear_applied_forces() noexcept
Clears all pre-integrated forces.
Definition: rigid-body.hpp:462
constexpr const math::fvec3 & get_center_of_mass() const noexcept
Returns the center of mass of the rigid body.
Definition: rigid-body.hpp:284
constexpr void set_mass(float mass) noexcept
Sets mass of the rigid body.
Definition: rigid-body.hpp:144
constexpr void apply_torque(const math::fvec3 &torque) noexcept
Applies a torque to the rigid body.
Definition: rigid-body.hpp:414
constexpr void set_previous_orientation(const math::fquat &orientation) noexcept
Sets the previous orientation of the rigid body.
Definition: rigid-body.hpp:108
constexpr void apply_central_impulse(const math::fvec3 &impulse) noexcept
Applies an impulse at the center of mass of the rigid body.
Definition: rigid-body.hpp:440
constexpr const math::fvec3 & get_previous_position() const noexcept
Returns the previous position of the rigid body.
Definition: rigid-body.hpp:266
constexpr void apply_central_force(const math::fvec3 &force) noexcept
Applies a force at the center of mass of the rigid body.
Definition: rigid-body.hpp:404
constexpr const math::transform< float > & get_previous_transform() const noexcept
Returns the transformation representing the previous state of the rigid body.
Definition: rigid-body.hpp:260
math::transform< float > interpolate(float alpha) const
Returns a transformation representing a state of the rigid body between its current and previous stat...
Definition: rigid-body.cpp:58
constexpr const math::fvec3 & get_position() const noexcept
Returns the current position of the rigid body.
Definition: rigid-body.hpp:242
constexpr const math::fquat & get_previous_orientation() const noexcept
Returns the previous orientation of the rigid body.
Definition: rigid-body.hpp:272
constexpr const math::fvec3 & get_angular_velocity() const noexcept
Returns the angular velocity of the rigid body, in rad/s.
Definition: rigid-body.hpp:350
constexpr bool is_static() const noexcept
Returns true if the rigid body is static, false otherwise.
Definition: rigid-body.hpp:382
constexpr float get_inverse_inertia() const noexcept
Returns the inverse moment of inertia of the rigid body, in kg⋅m^2^-1.
Definition: rigid-body.hpp:308
constexpr void set_linear_damping(float damping) noexcept
Sets the linear damping factor of the rigid body.
Definition: rigid-body.hpp:176
constexpr void set_position(const math::fvec3 &position) noexcept
Sets the current position of the rigid body.
Definition: rigid-body.hpp:52
void integrate(float dt) noexcept
Integrates forces and velocities.
Definition: rigid-body.hpp:487
constexpr void apply_force(const math::fvec3 &force, const math::fvec3 &radius) noexcept
Applies a force at a point on the rigid body.
Definition: rigid-body.hpp:393
constexpr math::fvec3 get_point_velocity(const math::fvec3 &radius) const noexcept
Calculates the total velocity at a point on the rigid body.
Definition: rigid-body.hpp:374
constexpr void set_previous_scale(const math::fvec3 &scale) noexcept
Sets the previous scale of the rigid body.
Definition: rigid-body.hpp:119
constexpr const math::fvec3 & get_linear_momentum() const noexcept
Returns the linear momentum of the rigid body, in kg⋅m/s.
Definition: rigid-body.hpp:332
constexpr void set_transform(const math::transform< float > &transform) noexcept
Sets the transformation representing the current state of the rigid body.
Definition: rigid-body.hpp:42
void set_collider(std::shared_ptr< collider > collider) noexcept
Sets the collider of the rigid body.
Definition: rigid-body.hpp:166
void integrate_forces(float dt) noexcept
Integrates forces, updating the momentums and velocities of the rigid body.
Definition: rigid-body.cpp:26
constexpr const math::fvec3 & get_angular_momentum() const noexcept
Returns the angular momentum of the rigid body, in kg⋅m^2⋅s^-1.
Definition: rigid-body.hpp:338
constexpr void apply_impulse(const math::fvec3 &impulse, const math::fvec3 &radius) noexcept
Applies an impulse at a point on the rigid body.
Definition: rigid-body.hpp:425
constexpr float get_inverse_mass() const noexcept
Returns the inverse mass of the rigid body, in kg^-1.
Definition: rigid-body.hpp:296
constexpr float get_mass() const noexcept
Returns the mass of the rigid body, in kg.
Definition: rigid-body.hpp:290
constexpr void set_angular_damping(float damping) noexcept
Sets the angular damping factor of the rigid body.
Definition: rigid-body.hpp:186
constexpr void apply_torque_impulse(const math::fvec3 &torque) noexcept
Applies a torque impulse to the rigid body.
Definition: rigid-body.hpp:453
constexpr const math::fvec3 & get_applied_force() const noexcept
Returns the total pre-integrated force, in N.
Definition: rigid-body.hpp:356
constexpr void set_scale(float scale) noexcept
Sets the current scale of the rigid body.
Definition: rigid-body.hpp:77
constexpr void set_previous_position(const math::fvec3 &position) noexcept
Sets the previous position of the rigid body.
Definition: rigid-body.hpp:98
constexpr float get_inertia() const noexcept
Returns the moment of inertia of the rigid body, in kg⋅m^2.
Definition: rigid-body.hpp:302
constexpr void set_angular_momentum(const math::fvec3 &momentum) noexcept
Sets the angular momentum of the rigid body.
Definition: rigid-body.hpp:207
constexpr void set_previous_transform(const math::transform< float > &transform) noexcept
Sets the transformation representing the previous state of the rigid body.
Definition: rigid-body.hpp:88
constexpr void set_scale(const math::fvec3 &scale) noexcept
Sets the current scale of the rigid body.
Definition: rigid-body.hpp:73
constexpr const math::fvec3 & get_scale() const noexcept
Returns the current scale of the rigid body.
Definition: rigid-body.hpp:254
constexpr void set_linear_momentum(const math::fvec3 &momentum) noexcept
Sets the linear momentum of the rigid body.
Definition: rigid-body.hpp:196
constexpr void set_linear_velocity(const math::fvec3 &velocity) noexcept
Sets the linear velocity of the rigid body.
Definition: rigid-body.hpp:218
constexpr const math::transform< float > & get_transform() const noexcept
Returns the transformation representing the current state of the rigid body.
Definition: rigid-body.hpp:236
constexpr mat4< T > scale(const vec3< T > &v)
Constructs a scale matrix.
constexpr vector< T, 3 > cross(const vector< T, 3 > &x, const vector< T, 3 > &y) noexcept
Calculates the cross product of two vectors.
Definition: vector.hpp:1095
Physics.
Definition: constants.hpp:23
Quaternion composed of a real scalar part and imaginary vector part.
Definition: quaternion.hpp:39
vector_type scale
Scale vector.
Definition: transform.hpp:56
static constexpr transform identity() noexcept
Returns an identity transform.
Definition: transform.hpp:107
vector_type translation
Translation vector.
Definition: transform.hpp:50
quaternion_type rotation
Rotation quaternion.
Definition: transform.hpp:53
n-dimensional vector.
Definition: vector.hpp:44