Main content

## Code your own simulation

Current time:0:00Total duration:2:20

# 1D spring-mass system

## Video transcript

(jumping, bouncing) (light switches on) - Great work. Now it's time to
throw a spring into the mix. Let's imagine we connect
one end of a spring to our particle, and the other end
to an anchor point, like this. The particle is being
pulled down by gravity causing the string to stretch out. When springs are displaced,
they try to pull back together and this is known as a spring force. In lesson one we saw the
strength of this spring force depends on the displacement and
the stiffness of the spring. And we can express the
stiffness of a spring with a variable K, called
the spring constant. This is known as Hooke's Law. Spring force equals negative
K times displacement. Now let's modify our program
to include this new spring. First, I'll add new variables
for the anchor position of the spring to the initial settings. One for the X coordinate and
one for the Y coordinate. I'll call these anchor X and anchor Y. I'll set these so the anchor
is in the middle of the screen. There is another initial
setting to consider which is the spring constant. It expresses how stiff the spring is. Let's call this K, I'll
give it a value, say, seven. Now we can go into our draw function and add the spring force. I'm going to define a new
variable called spring force Y to represent the vertical spring forces. From Hooke's Law, we know
this will be spring force Y equals negative K times displacement, or position Y minus anchor
Y, which is the distance from the anchor to the mass. Now we just need to add
the spring force to our existing force Y
calculation which gives us force Y equals spring force
Y plus mass times gravity. And that's it for updating our forces. Finally, we just need to do
some new drawing to account for the spring we've added. I'll draw an anchor with a
little box centered at the anchor position we've defined
in our initial settings. And I'll draw the spring
with a line which extends from the anchor to the
particle position, like this. Now, let's run our simulation
and see what happens. It's alive! Let's pause here so
you can get comfortable with this mass spring system.