Categories
Nature of Code

Cannon Commander

📌 Assignment

Create a simulation where objects are shot out of a cannon. Each object should experience a sudden force when shot (just once) as well as gravity (always present). Add rotation to the object to model its spin as its shot from the cannon. How realistic can you make it look?

✍️ Process

I initially started this project with a few different ideas. I firstly attempted to recreate the ship controls from Asteroid. I got too confused when trying to implement the thrust power and couldn’t quite figure it out. Thus, I moved on to the cannon prompt.

I found this prompt much easier to accomplish and was able to do so without having to look at the solution code. I broke the project down into steps:

  1. Having a ball that can be ‘shot’ (setting velocity to a sudden vector)
  2. Adding gravity to the ball (applying a downward acceleration vector)
  3. Controlling the angle heading at which the ball is shot (follow the mouse vector)
  4. Drawing a cannon sprite that accurately points in the direction the ball will be shot
  5. Fire multiple balls (array of projectiles to which a new one is added each shot)
  6. Control the power of the shot (allow the user to control the amount by which the force vector is multiplied)

Segmenting the project in this way allowed me to gradually make progress and not get bogged down in any of the one steps. By the end of Steps One and Two, I only had a ball that would shoot across the screen when you pressed spacebar (forgot to document this). Step Three came fairly easy after looking at the example Rectangle Grab code from 3.2.

Working on Step 4

Step Four just took some playing around with translate and drawing the cannon in the proper position. The cannon follows the same heading as the cannon ball.

Step Four Completed!

Step Five took some fooling around to do. I had to reorganize how the firing was handled and move some functions around. My solution (similar to my project from last week) was to have an array of particles that is added to each time a ball is fired. There is a for loop in the draw function that iterates through this array and updates each cannon ball individually. There is also a ct variable that keeps track of the most recently fired ball.

Step Five done Woo!!!

Finally, I wanted to allow the user to control the speed at which the balls are fired. This is down in a similar fashion to my sketch from last week. The user holds down their mouse to charge up a shot and the shot is fired upon release. This is all handled by the chrg variable.

There is a line indicator that gives the user some kind of visual guide as too how much they have charged the shot.

Step Six Completed 😎

🤭 Concluding Thoughts

I am fairly satisfied with my work this week. I know there is much more potential to the material covered in the Oscillation chapter but I was having trouble with some of the more advanced concepts. I will finish the rest of the videos and try to complete the Asteroid movement exercise using my own knowledge.

Future Developments:

  1. Gamification – I could combine my code from last week to create a fun Bloons Tower Defense clone in which you have to use the cannon to shoot swarms of balloons
  2. Cannon Recoil – have a recoil animation on the cannon in which it moves back each time a projectile is fired

Leave a Reply

Your email address will not be published. Required fields are marked *