21/21
11. Simulation
Introduction to mass-spring system
You objective will be to model a so-called physical mass-spring system linking different particles. Mass-springs system are typically used to model deformable elastic objects.
Let us consider two elements (called particles) at position \(p\) and \(q\) with respective mass \(m\) linked by an elastic spring.
The spring applies the force \(F\) on the element at position \(p\) \[F = K (L_0-\|p-q\|)\frac{p-q}{\|p-q\|}, \] where \(L_0\) is the rest-length of the spring, \(K\) is the spring stiffness. Similarly, an equal and opposite force \(-F\) applies on the particle at position \(q\).
In addition of spring force, the object is also subject to gravity force, and fluid friction (called damping) modeled as \(F_d = -\mu v\), where \(v\) is the speed of the particle.
The complete motion equation for the particle at position \(p\) can be written as the following
\[ \left\{ \begin{array}{ll} m v'(t) &= F(t) + F_d(t) + m\,g \\ p'(t) &= v(t) \end{array} \right. \]
Discretizing the equation in time using the so-called Forward Euler method leads to
\[ \left\{ \begin{array}{ll} v(t + \Delta t) &= (1-\mu/m\,\Delta t)\, v(t) + \Delta t \,(F(t)/m + g) \\ p(t + \Delta t) &= p(t) + \Delta t \,v(t) \end{array} \right. \]
where \(\Delta t\) is a small time step.
Application
The following code implements a single spring between a two particles (one of them is fixed).
[ source ]

-
Execute and observe the behavior of the code, in particular the numerical integration.
-
Add a second spring to make a chain of three particles.
-
Create a chain of arbitrary number of springs.