|
Phyth v0.1.0
A dimensional safe C++ header-only mechanics and electromagnetics simulation library.
|
Spring constraint between two particles. More...
#include <Spring.hpp>

Static Private Member Functions | |
| static void | validateRestLength (const Quantity< Meter > &length) |
| static void | validateStiffness (const Quantity< NewtonPerMeter > &stiffness) |
| static void | validateDamping (const Quantity< KilogramPerSecond > &damping) |
Private Attributes | |
| std::shared_ptr< Particle > | p1_ |
| std::shared_ptr< Particle > | p2_ |
| Quantity< NewtonPerMeter > | stiffness_ |
| Quantity< KilogramPerSecond > | damping_ |
| Quantity< Meter > | rest_length_ |
Spring constraint between two particles.
Spring applies a Hooke's law force between two particles: F = -k * (current_length - rest_length) * direction
Additionally, a damping force proportional to the relative velocity along the spring axis can be added: F_damp = -c * (v1 - v2) · direction
The spring is bi-directional: equal and opposite forces are applied to both particles. If one particle is fixed, the other still moves correctly.
Example: auto p1 = std::make_shared<Particle>(1_kg, Vector3{0_m, 0_m, 0_m}); auto p2 = std::make_shared<Particle>(1_kg, Vector3{1_m, 0_m, 0_m});
Spring spring(p1, p2, 10.0_N/m, 0.5_kgps);
// In simulation loop: spring.Constrain();
auto energy = spring.GetElasticEnergy(); // 1/2 * k * elongation^2
Definition at line 37 of file Spring.hpp.
|
inline |
Construct a spring between two particles.
| p1 | First particle (force applied in direction p2->p1) |
| p2 | Second particle (force applied in direction p1->p2) |
| stiffness | Spring constant k (N/m) |
| damping | Damping coefficient c (kg/s), default 0 |
The rest length is automatically set to the initial distance between the two particles.
| std::invalid_argument | If rest length <= 0, stiffness < 0, or damping < 0 |
Definition at line 53 of file Spring.hpp.
|
inline |
Apply spring and damping forces to both particles (Force Constrain)
The spring force is: F = k * (current_length - rest_length) * direction
If damping > 0, an additional damping force is applied: F_damp = c * (v1 - v2) · direction
Both forces are applied in opposite directions to p1 and p2.
Definition at line 76 of file Spring.hpp.
Definition at line 116 of file Spring.hpp.
|
inline |
Definition at line 113 of file Spring.hpp.
Compute the elastic potential energy stored in the spring.
Definition at line 161 of file Spring.hpp.
Definition at line 121 of file Spring.hpp.
Compute energy per unit length.
Note: If current_length is zero, this will produce a division by zero. Caller should ensure the particles are separated.
Definition at line 174 of file Spring.hpp.
Definition at line 101 of file Spring.hpp.
Definition at line 104 of file Spring.hpp.
Definition at line 107 of file Spring.hpp.
|
inline |
Definition at line 110 of file Spring.hpp.
|
inline |
Set a new damping coefficient.
| damping | New damping coefficient (must be non-negative) |
| std::invalid_argument | If damping < 0 |
Definition at line 151 of file Spring.hpp.
Set a new rest length.
| rest_length | New rest length (must be positive finite) |
| std::invalid_argument | If rest_length <= 0 |
Definition at line 129 of file Spring.hpp.
|
inline |
Set a new stiffness.
| stiffness | New stiffness (must be non-negative) |
| std::invalid_argument | If stiffness < 0 |
Definition at line 140 of file Spring.hpp.
|
inlinestaticprivate |
Definition at line 198 of file Spring.hpp.
|
inlinestaticprivate |
Definition at line 186 of file Spring.hpp.
|
inlinestaticprivate |
Definition at line 192 of file Spring.hpp.
|
private |
Definition at line 183 of file Spring.hpp.
|
private |
Definition at line 179 of file Spring.hpp.
|
private |
Definition at line 180 of file Spring.hpp.
Definition at line 184 of file Spring.hpp.
|
private |
Definition at line 182 of file Spring.hpp.