this simulation shows a roller coaster with a ball which is also attached to a spring.
, please see that page for more information on the physics and how the simulation is done. the only difference here is that the spring force is added to the differential equation.
the width and height of the spring is given by:
s_{x} = q_{x} − p_{x}
s_{y} = q_{y} − p_{y}
the length of the spring is then
√(s_{x}^{2} + s_{y}^{2}). the magnitude of the spring force is
c (√(s_{x}^{2} + s_{y}^{2}) − r). the direction of the spring force is towards
q, the fixed end of the spring. the component of the spring force that is parallel to the track is

f = cos(θ) c (√(s_{x}^{2} + s_{y}^{2}) − r)
 (1) 
it is this paralleltothetrack component of the spring force that will accelerate the ball along the track. consider a couple of cases to convince yourself of this: if the spring is perpendicular to the track, then
θ ≈ 90° and
cos(θ) ≈ 0 so the spring doesn't accelerate the ball. conversely, if
θ ≈ 0 then the spring will accelerate the ball in the positive track direction. or, if
θ ≈ 180° then the spring accelerates the ball in the negative track direction.
we can find
cos(θ) by using the formula for the angle between two vectors
a, b
where the numerator is the dot product, and the denominator is the product of the magnitudes. define our two vectors by
 a = (1, k) the slope vector, pointing in the direction of increasing p
 b = (s_{x}, s_{y}) the spring force vector
this leads to
cos θ =

s_{x} + k s_{y}

√(1 + k^{2}) √(s_{x}^{2} + s_{y}^{2})

put this expression into equation (1) and combine with the gravity and friction forces developed for the
simple roller coaster to get
a =

−g k

− ^{b}⁄_{m} v +

c (√(s_{x}^{2} + s_{y}^{2}) − r) (s_{x} + k s_{y})

√(1 + k^{2})

m √(1 + k^{2}) √(s_{x}^{2} + s_{y}^{2})

 (2) 
where
b = damping constant and
v = velocity. keep in mind that the slope
k and spring stretch
s_{x}, s_{y} are functions of the position
p.
besides this change to the force equation, the simulation is similar to the
simple roller coaster. please see that page for more information on how the simulation is implemented.