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.

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!

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. The main one to mention now is this one:

can_planetoids_move = true;

If you want to you can set `can_planetoids_move = false;`

so we can make some modifications to the behavior of the ship without worrying about getting hit with a planetoid right away.

In the top left corner, press and you'll see something like this:

If you press the arrow keys you will notice that the program behaves like the classic asteroids game. We are going to add a new twist to this game!

If you press all four arrow keys you will notice there are a couple of objectives that have not yet been completed:

If the program can detect that something is wrong with your code it will show a red next to that objective.

If the program can detect that you have completed the objective it will show a green check mark next to that objective.

But the program is not very smart and if it can't tell whether you have completed the objective it will indicate a question mark like this next to that objective.

By the end of this activity your goal is to have green check marks next to all the objectives like this:

The steps below will help you to achieve all these objectives!

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.

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

ang_accel = 0; Lship = 100.0; Iship = 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 inertia 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 `Lship*Lship`

instead of `Lship^2`

.

Hint #2: Remember that in the code the mass of the ship is just the variable `mass`

.

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.1; } else if (keyIsDown(RIGHT_ARROW)) { // theta += -0.1; } 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.

If you modify all these lines of code in the right way you should get this behavior

Right after `display();`

, add this line:

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

If successful your code should behave like this

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

**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 and that you get all green checks and no

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