Part 5: Planetoids with torque!

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. 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.

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 \label{eq:omega} $$

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 = 100.0;
 torque_thrusters = 0;

Currently, most of 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.

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

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);

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 Fthrust. 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

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.