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

Planetoids with Torque!

Developed by Chris Orban

This lab will guide you through coding up a web-based implementation of the classic video game asteroids but with a twist! In the process we will illustrate how torque and angular acceleration work for a ship traveling through free space. **You should complete the Planetoids (similar to asteroids) activity first before working on this one!** This example will use a programming language called [p5.js](http://p5js.org) 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 computer or chromebook without downloading any software!** In the classic game of Asteroids, there is a little ship that flies around in space, very far away from stars and planets so that the gravitational acceleration is zero. 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). This exercise is part of a series of exercises developed by Prof. Chris Orban. The next exercise in the series is [Planetoids with a Spring!](http://www.compadre.org/PICUP/exercises/exercise.cfm?I=251&A=PlanetoidsSpring) 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 (orban@physics.osu.edu). The first paper from this study [is available at this link](https://arxiv.org/abs/1701.01867), the second paper which discusses the electromagnetism exercises [is available at this link](https://arxiv.org/abs/1707.00185)
Subject Area Mechanics
Levels High School and First Year
Learning Objectives
1. Create an interactive model of a ship traveling through free space that can rotate freely and apply thrusters (constant torque) to change the rotation rate by modifying the Planetoids exercise 2. Understand in a visual way how velocity, force, acceleration and torque vectors affect the motion of the ship 3. Develop a conceptual knowledge of how the motion of the ship depends on the mass and length of the ship as well as the force of the thrusters by changing these values and trying to survive in the game as long as possible
Time to Complete 60 min
In this lab we will make the planetoids game more realistic by allowing the space craft to spin around as it would if it were really traveling in space. So in addition to a rocket engine that can propel the space craft forward, the ship will have thrusters on the front and back that can provide torque to spin the ship either the clockwise or counter-clockwise direction.

Step 0. Opening 2D Planetoids code

For convenience we have supplied an "old" version of the planetoids game so you can play it and remember the feel of the game without the craft spinning around like mad. [Click here to open the planetoids code](http://alpha.editor.p5js.org/ChrisOrban/sketches/BJzCa2fe-) This version is similar to the earlier planetoids game except the name is changed and it actually has planetoids. There are also some new variables that we will use later.

Very Important: Sign in to your account! Then click "Duplicate" so you can have your own version of the code!

Step 1. Think about thrusters and spin (no coding needed in this step).

Imagine that these thrusters work through clever engineering that diverts the thrust of the main engine so that half of this thrust goes to the front thruster and the other half goes to thruster at the back of the ship. ![](images/PlanetoidsTorque/torque.png "")

We can figure out the net torque on the ship by assuming that the center of mass is at the center of the ship (half-way from either end).

For counter-clockwise torque: $$\tau_{\rm net} = \sum_i \tau_i = +\left( \frac{F_{\rm thrust}}{2} \right) \cdot \left(\frac{L_{\rm ship}}{2}\right) + \left( \frac{F_{\rm thrust}}{2} \right) \cdot \left(\frac{L_{\rm ship}}{2}\right) = \frac{F_{\rm thrust} L_{\rm ship}}{2} $$

For clockwise torque: $$\tau_{\rm net} = \sum_i \tau_i = -\left( \frac{F_{\rm thrust}}{2} \right) \cdot \left(\frac{L_{\rm ship}}{2}\right) - \left( \frac{F_{\rm thrust}}{2} \right) \cdot \left(\frac{L_{\rm ship}}{2}\right) = -\frac{F_{\rm thrust} L_{\rm ship}}{2} $$

There are no other sources of torque in the planetoids game. This means that we can use the equations above to figure out the angular acceleration ($\alpha$).

Angular acceleration: $$\alpha = \frac{\tau_{\rm net}}{I}$$

where $I$ is the moment of inertia (sometimes I call this the rotational inertia). We will talk about what to use for the moment of inertia later.

From the angular acceleration ($\alpha$) we can figure out the angular speed ($\omega$): $$\omega_f = \omega_i + \alpha \cdot \Delta t$$

Finally, from the angular speed ($\omega$) we can figure out the angle that the ship is pointing: $$\theta_f = \theta_i + \omega \cdot \Delta t $$

In what follows we will turn these equations into code.

Step 2. Make changes to the code so that the ship can spin around.

You will be given the code but it is up to you to figure out the right place in torque.js to put it.

First, comment out the lines in torque.js that change theta using //

if (keyIsDown(LEFT_ARROW)) {
//  theta += 0.05;
} else if (keyIsDown(RIGHT_ARROW)) {
//  theta += -0.05;
} else if (keyIsDown(UP_ARROW) ) {

Next add code so that pressing the left arrow produces a positive angular acceleration like this:

ang_accel = torque_thrusters/Iship;

and pressing the right arrow produces a negative angular acceleration like this:

ang_accel = -torque_thrusters/Iship;

Now use this angular acceleration to change the angular speed (omega) as in Eq. $\ref{eq:omega}$ by adding code like this:

omega = omega + ang_accel*dt;

In the code above, notice that the omega on the right side is the old omega value ($\theta_i$) and the omega on the left is the new value of omega (which is $\theta_f$).

Finally, add an equation so that theta is determined from the initial angle ($\theta_i$) and the angular speed ($\omega$).

theta = theta + omega*dt;

In the above I have used the same trick where theta on the right side is the old value ($\theta_i$) and theta on the left side is the new value ($\theta_f$).

You also need to add something that stops the ship from rotating out of control, similar to how there is a something to stop the ship from accelerating out of contol in the x and y direction.

Your program should not work yet because you haven't specified the torque or the moment of inertia yet. If you run the code the ship will only be able to move in the forward direction and not change direction at all.

Step 3. Specify the torque and moment of inertia for the ship.

Near the beginning of the code and shortly before function setup() there are four new variables. Here they are:

 ang_accel = 0;
 Iship = 0;
 Lship = 0;
 torque_thrusters = 0;
  

Currently, these variables are just set to zero but you are about to change that. Follow the syntax of other variables like Fship, mass and dt.

Set Lship equal to 100.

Consider the comments on torque in Step. 1 and modify torque_thrusters like this:

torque_thrusters = Fthrust*Lship/2;

Does the line above make sense to you? Why is there a factor of 2? If not see Step 1.

Now consider the variable Iship, which is the moment of interita for the ship. Look up the formula for the moment of inertia of a rod of length $L_{\rm ship}$. Follow the syntax of other variables and use this formula to determine Iship. Before you do this read through these hints:

Hint #1: use a decimal instead of a fraction in the formula for Iship. <-- Very important!

Hint #2: use Lship*Lship instead of Lship^2.

Hint #3: Remember that in the code the mass of the ship is just the variable mass.

If you modify all these lines of code in the right way [you should get this behavior](www.physics.ohio-state.edu/~orban/physics_coding/torque_files/v1/index.html)

Step 4. Add a timer and see how long you can survive.

Right after display();, add this line:

t += dt;
drawText("time = ",0.8*width,0.9*height);
drawText(t,0.85*width,0.9*height);

If successful your code should behave like this

Step 5. What happens when you change Lship, mass and Fthrust?

Experiment with different values of Lship, mass and Fthrusters. In what you turn in for the lab, comment on the effect of changing each of these variables (make one variable larger and try the game, then change another variable, etc.) What do you think are the best values to use for surviving the longest in the game? Add comments to the end of your torque.js code or in the comments in the dropbox submission.

Extra credit: Add a projectile to the game

Option #3 In the original planetoids programming lab was to add a projectile to the ship. This projectile will travel at constant speed in whatever direction the ship is pointing when you press the spacebar. Either re-use the code you used to add a projectile to the earlier lab or develop it for the first time here. In what you turn in for this lab, indicate whether you reused old code or developed the projectile code for the first time.

Bonus Points: If the projectile hits an planetoid, the planetoid should shrink in size or split in two.

How to get full credit for this programming lab!!!

**1. Make sure the code works as intended** Make sure that your ship, once spinning, will continue spinning without speeding up or slowing down. A lot of people don't manage to do this. Common problems are that the ship only spins when you press the left and right arrow keys. Or once you press the left or right arrow keys, even if you let go of these keys, the angular acceleration is constant and it spins faster and faster. Make sure your ship just spins without speeding up or slowing down when you are not pressing the left and right arrow keys. In other words, [make sure the ship behaves like this](http://www.physics.ohio-state.edu/~orban/physics_coding/torque_files/v2/index.html) **2. Figure out the best combination of Lship, mass and Fthrust as discussed in Step 5** Make sure to include in the comment box when you submit your code the values that you think make it the easiest to survive. If you forget to do this you may lose points.

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