CellModules
MassSpringDamper.hpp
Go to the documentation of this file.
1#ifndef MASSSPRINGDAMPER_HPP
2#define MASSSPRINGDAMPER_HPP
3
11#include <mecacell/mecacell.h>
13
20template <typename cell_t>
22
23private:
24 std::pair<cell_t*, cell_t*> connection;
25 float_t stiffness;
26 float_t dampCoef;
27 float_t restLength;
28 float_t length;
38 direction = p1 - p0;
40 if (length > 0) direction /= length;
41 }
42
49 double sp1 = connection.first->getBody().getVelocity().dot(direction);
50 double sp2 = -connection.second->getBody().getVelocity().dot(direction);
51 return -(sp1 + sp2);
52 }
53
54public:
58 inline MassSpringDamper() = default;
59
69 inline MassSpringDamper(cell_t* c1, cell_t* c2, const float_t &K, const float_t &C, const float_t &L)
70 : connection(c1, c2), stiffness(K), dampCoef(C), restLength(L), length(L) {}
71
77 inline void setRestLength(float_t L) { restLength = L; }
78
84 inline float_t getRestLength() const { return restLength; }
85
91 inline float_t getLength() const { return length; }
92
98 inline std::pair<cell_t *, cell_t *> getConnection() const { return connection; }
99
104 // BASIC SPRING
105 updateLengthDirection(connection.first->getBody().getPosition(),
106 connection.second->getBody().getPosition());
107
108 float_t x = length - restLength; // Actual compression or elongation
109 bool compression = x < 0;
110 double v = computeSpeedFromCells();
111
112 float_t f = (-stiffness * x - dampCoef * v) / 2.0;
113 connection.first->getBody().receiveForce(f, -direction, compression);
114 connection.second->getBody().receiveForce(f, direction, compression);
115 }
116
122 inline std::string toString() {
123 return std::to_string(connection.first->id) + " <-> " + std::to_string(connection.second->id);
124 }
125};
126
127#endif // MASSSPRINGDAMPER_HPP
CGAL::Exact_predicates_inexact_constructions_kernel K
Class for managing mass-spring-damper physics.
void updateLengthDirection(const MecaCell::Vec &p0, const MecaCell::Vec &p1)
Updates the current length and direction.
double computeSpeedFromCells()
Computes the total speed from the connected cells.
MassSpringDamper(cell_t *c1, cell_t *c2, const float_t &K, const float_t &C, const float_t &L)
Constructor with parameters.
float_t getRestLength() const
Gets the rest length of the spring.
std::string toString()
Gets the connection as a string.
MassSpringDamper()=default
Default constructor.
MecaCell::Vec direction
std::pair< cell_t *, cell_t * > connection
std::pair< cell_t *, cell_t * > getConnection() const
Gets the connection between the cells.
void setRestLength(float_t L)
Sets the rest length of the spring.
void computeForces()
Computes the forces for the connected cells.
float_t getLength() const
Gets the current length of the spring.
general purpose 3D vector/point class.
Definition: vector3D.h:20
double length() const
compute the length of the vector
Definition: vector3D.h:257