Antkeeper  0.0.1
rigid-body.cpp
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 
22 #include <algorithm>
23 
24 namespace physics {
25 
26 void rigid_body::integrate_forces(float dt) noexcept
27 {
28  // Apply forces
29  m_linear_momentum += m_applied_force * dt;
30  m_angular_momentum += m_applied_torque * dt;
31 
32  // Apply damping
33  m_linear_momentum *= std::max<float>(0.0f, 1.0f - m_linear_damping * dt);
34  m_angular_momentum *= std::max<float>(0.0f, 1.0f - m_angular_damping * dt);
35 
36  // Update velocities
37  m_linear_velocity = m_inverse_mass * m_linear_momentum;
38  m_angular_velocity = m_inverse_inertia * m_angular_momentum;
39 
40  // Clear forces
41  m_applied_force = math::fvec3::zero();
42  m_applied_torque = math::fvec3::zero();
43 }
44 
45 void rigid_body::integrate_velocities(float dt) noexcept
46 {
47  // Record previous state
48  m_previous_transform = m_current_transform;
49 
50  // Update position
51  m_current_transform.translation += m_linear_velocity * dt;
52 
53  // Update orientation
54  const math::fquat spin = math::fquat{0.0f, m_angular_velocity * 0.5f} * m_current_transform.rotation;
55  m_current_transform.rotation = math::normalize(m_current_transform.rotation + spin * dt);
56 }
57 
59 {
60  return
61  {
62  math::lerp(m_previous_transform.translation, m_current_transform.translation, alpha),
63  math::nlerp(m_previous_transform.rotation, m_current_transform.rotation, alpha),
64  math::lerp(m_previous_transform.scale, m_current_transform.scale, alpha),
65  };
66 }
67 
68 } // namespace physics
void integrate_velocities(float dt) noexcept
Integrates velocities, updating the center of mass and orientation of the rigid body.
Definition: rigid-body.cpp:45
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
void integrate_forces(float dt) noexcept
Integrates forces, updating the momentums and velocities of the rigid body.
Definition: rigid-body.cpp:26
quaternion< T > nlerp(const quaternion< T > &a, const quaternion< T > &b, T t)
Performs normalized linear interpolation between two quaternions.
Definition: quaternion.hpp:673
quaternion< T > normalize(const quaternion< T > &q)
Normalizes a quaternion.
Definition: quaternion.hpp:679
constexpr T lerp(const T &x, const T &y, S a) noexcept
Linearly interpolates between x and y.
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
vector_type translation
Translation vector.
Definition: transform.hpp:50
quaternion_type rotation
Rotation quaternion.
Definition: transform.hpp:53
static constexpr vector zero() noexcept
Returns a zero vector, where every element is equal to zero.
Definition: vector.hpp:306