If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

Main content

1D spring-mass system

Learn how to create a mass spring system using Hooke's Law in an interactive JavaScript simulation. Add a spring to a particle, calculate spring force, and simulate the motion. Then determine the spring constant (K) and anchor points to create realistic movement.

Want to join the conversation?

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.