Midterm Project - Spaceship Battle
The idea for my project came to me one day when Dan mentioned that it was possible to construct a variable that could keep track of the distance between two objects. Thus, it was possible to write a code that could make something happen if two objects came into contact with one another. Almost immediately I thought of flying objects that could be struck by missiles. Soon, though, I came up with a better idea that included using spaceships and that’s when my Mid-Term Project was born.
In my mind, I imaged ships maneuvering through space and firing on another with projectiles. At first, this seemed like a daunting programming task but then I realized it would be manageable if I just broke down the code into different modules. The first module I actually worked on was the background. This was not my intention but when we learned in class how to create arrays, I saw the perfect opportunity to create a background of stars. This actually turned out to be simpler than I had first thought as I started with an example of an array from Dan’s book and just modified it. After I created a background of randomly placed stars (ellipses of 1 by 1), I then created planet objects, with random colors.
With my background complete, it was time to move on to the heart of my idea, the spaceships. I created two spaceships by using three ellipses of varying sizes and I colored them differently, one green, the other red. I then made them move across the screen in opposite directions towards one another from random start points. Using the code from my “Planes” sketch, I had the spaceships start over every time they reached the end of the screen.
Now, though, I had to decide how I wanted the starships to interact. In the grand scheme of my idea, I wanted to have many spaceships moving back and forth across the screen firing on one another. However, I decided to keep it simple at first. Thus, my idea of “Jousting Spaceships” came about. In the end, I planned to have spaceships moving randomly around the screen like gladiators trapped in a ring, but for starters I would just have two flying at one another from the left and right.
This is when the distance variable finally came into play. Actually, I later discovered that there is an actually “Distance” function built into Processing that can track the distance between two points. I could have used that, but instead I created my own variables, d1 and d2. D1 tracks the distance between the x variables of the two spaceships, x1 and x2, and d2 tracks the distance between the y variables, y1 and y2. The formula for d1 is “x2 – x1” and the formula for d2 is “(abs)y1 – y2.” The (abs), or absolute value, is necessary since the answer to that equation might be a negative number.
With the distance variables created, it was now time for logic statements. In this case, I wanted something to happen if d1 and d2 were less than 170. This had the effect of creating a sphere around each of the spaceships with a diameter of 170 pixels. If one spaceship entered the “sensor” sphere of the other, something would happen. Thinking ahead, my plan was to have the spaceships fire on one another but since I wasn’t up to that yet, I decided the first step would be to just have them change course.
Under normal circumstances, each spaceship’s x variable is constantly changing as they move. Their y variables, however, remain constant, which gives them the appearance of moving horizontally across the screen. The spaceships start at random y variables at the start of each loop. Sometimes they pass close to one another, sometimes they don’t. Now, though, I had a logic statement that would tell the spaceships to do something if they passed within 170 pixels of one another. The question was: what should they do?
Having the spaceships immediately change course towards one another when they entered sensor range seemed a little too obvious. It also wouldn’t make for a very interesting simulation once the ships firing started. Thus, I came up with another idea. What if the spaceships didn’t always want to move towards each other when one ship detected the other? What if they got scared and tried to move away? And so the Courage/Cowardice variable (cc) was born.
The cc variables (one for each ship) were actually very simple. For every loop, cc would equal a random number between 1.5 and –1.5. This number could be added to the y variables (when they passed within 170 pixels) of each ship causing them to begin to move diagonally up (if cc was negative) or diagonally down (if cc was positive). The farther away from zero, the steeper the turn. However, once the ships moved out of range, the y variable would automatically return to zero and the ships would again move horizontally.
This code worked surprisingly well. However, there were still a few more issues to take care off. There were times when both spaceships did move right at each other and collided. Of course, with no code to handle this, nothing happened and ships just crossed through each other and continued on their merry way. Since I wasn’t yet ready to figure out how to write code for collisions, I decided the best I could do for now was to try to limit the number of times the spaceships collided. For this, I added another logic statement, which was basically a duplicate of the first but with a smaller distance quotient. If the spaceships got within 50 pixels of one another, their cc variables would be multiplied by –2. This would have the effect of changing their course by around ninety degrees and making their turns steeper. I arrived at this formula after some trial and error. Now the ships were able to avoid each other most of the time, although they still “collided” every now and then.
My last step for this phase of my project was to add one more logic statement that prevented the spaceships from flying off the top and bottom the screen. For this I simply added the statement that of the spaceship’s y variables reached either zero or the height amount (500), y would automatically go to zero. This worked fine except when both ships reached the top or bottom before crossing and would actually “collide” again. To deal with that, I added one last logic statement for only one of the spaceships, which told it to get out of the way if the other spaceship hadn’t passed it yet. This logical statement was actually my longest, featuring one “or (||)” and four “ands (&&)”.
Overall, I was pretty happy with my end result. I found myself spending several minutes just watching my spaceships interact. I really enjoyed how almost every time the interaction was different. However, I realize that if I want to continue on with this project, there is still a lot of work to be done. My next step will be to add code for collisions. At first, I will just do this for the spaceships themselves, but once I have that code down, I will move on to examining how to create projectiles along with fire control for each ship.