This Exercise Set has been submitted for peer review, but it has not yet been accepted for publication in the PICUP collection.

Planetoids with a Spring!

Developed by Chris Orban

As a continuation of the Planetoids with Momentum! exercise, this exercise (Planetoids with a Spring!) will attach the "blob" to a spring so you can fly the ship into the blob and watch it oscillate back and forth. These oscillations will happen because the computer is calculating the changes in velocity, acceleration and position and **NOT** because we have coded up a sine or a cosine wave or anything like that. You can actually take out all of the trig functions from the code and the ship will still oscillate back and forth naturally after it hits the blob (like it should!). This example will use a programming language called [p5.js]( that is very similar to C and C++ programming. (Note: If you are familiar with C or C++ the main difference you will see is that there is no main() function and instead the draw() function serves this role.) **Importantly, this exercise can be completed using any chromebook without downloading any software!** This exercise set is designed for an algebra-based physics class at the college or high school level. It may also be useful for calculus-based physics for non-majors (e.g. engineering & science majors). There are pre-and-post assessment questions associated with this exercise (not available here) that are being used in an educational research study. If interested to collaborate on that study please e-mail Prof. Chris Orban ( The first paper from this study [is available at this link](, the second paper which discusses the electromagnetism exercises [is available at this link](
Subject Area Mechanics
Levels High School and First Year
Available Implementation Javascript
Learning Objectives
Students will be able to: 1. Implement the spring force (Hooke's law) into the "planetoids" game 2. Understand how the force, acceleration and velocity change as the spring becomes compressed and stretched 3. Fire the ship's thrusters to show (in a quantitative way) how the period of oscillations is independent of the amplitude 4. Compare measurements of the oscillation time to expectations for an object on a spring 5. (Challenge) implement drag force, predict and measure the change in the period of motion
Time to Complete 60 min

These exercises are not tied to a specific programming language. Example implementations are provided under the Code tab, but the Exercises can be implemented in whatever platform you wish to use (e.g., Excel, Python, MATLAB, etc.).

Planetoids with a spring!

Think back to the momentum exercise. The little ship can collide with a "blob" and stick to it. Now imagine that this blob is attached to a spring. Wouldn't it be interesting if the ship could collide with the blob and then the spring would push and pull both around? This is what we will do in this exercise. This spring will be attached to the blob and do the left-hand side of the screen as shown in this image: ![](images/PlanetoidsSpring/spring_screenshot.png "")

Step 0. Read/think about spring forces.

Thus far the only force we have considered is $F_{\rm thrust}$ which is a constant force in whatever direction the ship is pointing. (We also considered gravity in the lunar descent exercise, but for simplicity this was treated as a constant acceleration instead of a constant force.) The force from a spring is a very different kind of force. The more the spring is compressed, the more it pushes back and the more the spring is stretched out the larger the force to try and pull it back into the rest position. The formula that corresponds to this is given here: $$F_{\rm spring} = -k (x - L_{\rm relaxed})$$ The idea is that if $x = L_{\rm relaxed}$ then the force is zero. When $x < L_{\rm relaxed}$ then the spring force is positive so that it pushes the object to a larger $x$ value. And if $x > L_{\rm relaxed}$ then the spring force is negative and the object is pulled to a lower $x$ value. In the code you will soon develop, you should use Lrelaxed to represent $L_{\rm relaxed}$ and Fspring for $F_{\rm spring}$. [Click on this link to open the code in a p5.js editor](

Step 1. Open a modified version of the momentum code.

This version will have a spring drawn on it and other minor tweaks. [Click on this link to open the code in a p5.js editor]( Press play to run the code. [It should behave like this]( Please note that this exercise will remain 1-Dimensional for simplicity! Sorry!

Step 2. Add the spring force! Let the blob accelerate!

We are going to add a spring force to the blob. If the position of the blob (x_blob) is not equal to the relaxed length (Lrelaxed) the force will cause the blob to accelerate. To add the spring force, change the code in the following ways:
  • Make sure that the "Update velocities" section updates the velocity of both the ship and the blob. Create a variable called deltaVx_blob for this purpose.
  • Near the beginning of spring.js add a variable k for the spring constant and set it to 0.5.
  • Near the beginning of spring.js add a variable Lrelaxed for the natural length of the spring and set it to 300, and set x_blob equal to Lrelaxed
  • Near the beginning of spring.js add a variable Fspring, and set it equal to zero
  • Somewhere in draw(); add a line of code to determine Fspring that depends of the position of the blob, k constant and the relaxed length of the spring (Lrelaxed)
  • Use $F = ma$ and $\Delta v_x = a_x \Delta t$ to calculate the change in velocity of the blob (deltaVx_blob)
If you are able to make all these changes [your code should behave like this](

Step 3. Get the spring to do this! Think about the motion!

[Click here to see video!]( When is the velocity the largest? When is the force the largest? When is the acceleration the largest?

Step 4. Analyze the motion!

Let's modify the code in order to measure how long it takes for the blob to go from the relaxed position to either the fully stretched or fully compressed position and back. The easiest way to do this is to measure the amount of time it takes for the blob to return to $x = L_{\rm relaxed}$. This turns out to be trickier than it sounds. Follow the directions below. If you can think of a different way to do this, please feel free to try it out! Step 4.a. Somehere after display(); add this:
tcounter += dt; 
drawText("counter time = ",0.7*width,0.8*height);
Step 4.b.We need to set this variable to the value of tcounter every time the blob moves past the relaxed position! Somewhere after display(); add this:
// if the position of the blob is near the relaxed position
if ((abs(x_blob - Lrelaxed) < abs(vx_blob)*dt) & (tcounter > dt)) {
tlasttime = tcounter;
tcounter = 0; //Reset the clock

drawText("half cycle time = ",0.7*width,0.65*height);
When x_blob is close to the value of Lrelaxed and tcounter is much larger than a timestep, then the value of tcounter gets copied to tlasttime. Then the "clock" gets reset to zero with tcounter = 0. If you modify your code correctly [your program should behave like this]( Step 4.c. Measure the "half cycle time" when the ship is attached to the blob Fly the ship into the blob and then without firing the thrusters, watch the ship move back and forth a few times from the force of the spring. Write down what the typical "half cycle time" is. Now fire the thrusters to increase the amplitude of the oscillations! Is the half cycle time the same or different? Make sure you watch the oscillations for a few cycles before you decide. Does the answer surprise you? Step 5. Calculate the expected "half cycle time" from what you know about oscillating springs What is the "half cycle time" equivalent to? The angular speed? frequency? period? Use what you know about the period of an object attached to a frictionless spring to calculate what you would expect the "half cycle time" to be based on the mass of the objects and the spring constant. You should be able to estimate this quite accurately. (Hint: the angular speed of a frictionless spring is $\omega = \sqrt{k/m}$.) Step 6. Does the "half cycle time" depend on whether the ship is attached to the blob? It should be possible to fly your ship into the blob to get it oscillating and then fire your thrusters so that the ship flies away from the blob, but the blob is still oscillating. What is the "half cycle time" in this case? The "half cycle time" should be slightly different when the spring is only pushing around the blob! Step 6.a. Modify your code if nessary Is this true for your program? What should you modify in your code in order to make sure this happens? (Hint: add one line of code so that deltaVx_blob is different depending on whether the ship is stuck to the blob or not.) If you modify your code in the right way [your program should behave like this]( Step 6.b. Measure the "half cycle time" Measure the half cycle time for both when the ship is attached to the blob and when the ship is not attached to the blob. Make sure you can explain both results for the half cycle time from your knowledge of oscillating spring systems (i.e. from Step 5) Change the mass of the blob (mass_blob) and the mass of the ship (mass) and see if the "half cycle time" changes for both cases where the ship is and is not attached to the blob. Which values for the masses did you choose? Can you explain the new results for the "half cycle time"? (Challenge part 1) Step 7a. Add damping to the spring system! Add a drag (a.k.a. damping) force to the spring system. There is more than one kind of damping force you could use, but the simplest is $F_{\rm drag} = - b v$ where $b$ is a constant.
  • Near the beginning of your program add a variable b and set it to 0.1
  • Modify $F_{\rm spring}$ so that it includes an extra $-bv$ term. The damping is not normally thought of as a "spring force" but this is the easiest way to add damping to the program.
Test it out! Roughly how many cycles does it take for the motion to slow to a stop? Underdamped systems go through multiple oscillations before they slow to a stop. Make b much larger until the damping is so large that the system slows to a stop without oscillating. Overdamped systems slow to a stop without going through multiple oscillations. What value of b should you use to simulate a critically damped system? (Challenge part 2) Step 7b. Calculate and Measure the half-cycle time for the damped system Use the default values for the mass of the ship, the mass of the blob and the spring constant and set the damping constant b = 2.0. What do you measure for the half-cycle time now? You should be able to do a calculation that explains why you get the number that you get. (Hint: the angular frequency of a damped spring system is $\omega = \sqrt{k/m - (b/2 m)^2}$).

Things you need to do to get full credit for this programming lab:

1. Submit your code after making all the changes asked for in steps 1-4 and (if you decide to) the challenge steps 6 & 7 2. Measuring and calculating the half-cycle time In carmen there is a comment box when you submit the code. Make sure to write the half-cycle time you measured in part 4, and describe the calculation you did in part 5 to explain why the half-cycle time turned out to be the number that it did. The number you calculate and the number you measure should be very similar. If the numbers are way off then you probably won't get full credit. 3. State whether the half-cycle time depends on the amplitude Fly into the blob and then use your thrusters to make the amplitude of motion larger. Then stop firing the thrusters and measure the half-cycle time. Does it change or not? 3. The challenge is optional but if you take the time to do it you will get a few points of extra credit

Download Options

Credits and Licensing

The instructor materials are ©2017 Chris Orban.

The exercises are released under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 license

Creative Commons Attribution-NonCommercial-ShareAlike 4.0 license