Antkeeper  0.0.1
spring-constraint.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 
21 
22 namespace physics {
23 
25 {
26  if (!m_body_a || !m_body_b)
27  {
28  return;
29  }
30 
31  // Get radius vectors from centers of mass to spring attachment points
32  const math::fvec3 radius_a = m_body_a->get_orientation() * m_point_a;
33  const math::fvec3 radius_b = m_body_b->get_orientation() * m_point_b;
34 
35  // Get world-space spring attachment points
36  const math::fvec3 point_a = m_body_a->get_position() + radius_a;
37  const math::fvec3 point_b = m_body_b->get_position() + radius_b;
38 
39  // Calculate relative velocity between the attachment points
40  const math::fvec3 velocity = m_body_b->get_point_velocity(radius_b) - m_body_a->get_point_velocity(radius_a);
41 
42  // Calculate spring force
43  // F = -k * (|x| - d) * (x / |x|) - bv
44  const math::fvec3 difference = point_b - point_a;
46  const math::fvec3 force = -m_stiffness * (distance - m_resting_length) * (difference / distance) - velocity * m_damping;
47 
48  // Apply spring force to bodies at attachment points
49  m_body_a->apply_force(-force, radius_a);
50  m_body_b->apply_force(force, radius_b);
51 }
52 
53 } // namespace physics
constexpr const math::fquat & get_orientation() const noexcept
Returns the current orientation of the rigid body.
Definition: rigid-body.hpp:248
constexpr const math::fvec3 & get_position() const noexcept
Returns the current position of the rigid body.
Definition: rigid-body.hpp:242
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
void solve(float dt) override
Solves the constraint.
constexpr int difference(T x, T y) noexcept
Returns the number of differing bits between two values, known as Hamming distance.
Definition: bit-math.hpp:280
T distance(const vector< T, N > &p0, const vector< T, N > &p1)
Calculates the distance between two points.
Definition: vector.hpp:1106
vector< T, N > sqrt(const vector< T, N > &x)
Takes the square root of each element.
constexpr T dot(const quaternion< T > &a, const quaternion< T > &b) noexcept
Calculates the dot product of two quaternions.
Definition: quaternion.hpp:572
Physics.
Definition: constants.hpp:23
n-dimensional vector.
Definition: vector.hpp:44