CPSC 120 Principles of Computer Science Fall 2024

Midterm Project
Rube Goldberg Sketch

Due: Wed 11/6 at 11:59pm

The project is due at the time stated. You should plan to complete the project on time. See the policy on late work and extensions in the case of extenuating circumstances.


Introduction

The goal of the projects is to integrate the skills you've gained to build something a bit larger than you create in the labs.

Successfully completing this project means that you are able to:

Academic Integrity and Collaboration

As with the labs, you may get help with how to solve the problem and with writing and debugging code, but you must document any help received (including from TFs) and any resources used other than the textbook and posted course materials and you may not work together with other students to write code, shortcut to a solution by copying code or using someone else's program as a guide or example, or be in possession of someone else's program or solution before you have handed in your own.

Review the discussion in previous lab handouts and the full collaboration policy for more details.

Handin

To hand in your work:


Task

In this project, you'll create an animation inspired by Rube Goldberg — a Rube Goldberg machine is something that performs a simple task in a complicated, chain-reaction fashion. (For more about Rube Goldberg and his inventions and comics, see www.rubegoldberg.org.) Your task will be to create a sketch with multiple stages that link together a series of actions, such as in the example below. (Position the mouse near the left side of the window to raise the elevator and get everything started; click to restart.)

Your browser does not support the canvas tag.

Requirements

There is considerable flexibility as to the details of your sketch, but for full credit, it must contain the elements listed below. Note that the demo above does not satisfy all of the requirements! (And it also includes a few elements that have not been discussed in class, though you could investigate how to use them for extra credit.) It is intended only as an example to clarify some of the points below — you should not simply copy the demo, but rather come up with your own ideas.

In addition, you must:

Extra Credit

You can earn extra credit by going substantially beyond the required elements. Some possibilities:

Include a brief description of what you've done for extra credit in a comment at the beginning of your sketch. More creative and challenging elements will earn more points.


Process

Getting Started

Plan first. Sketch out what the scene will look like (on a piece of paper). Decide what the five stages will be — what will happen in each stage and what the trigger points are for the transitions from one stage to the next. (Keeping in mind #2 in lab 5, consider transition triggers of the form "in stage 2 and the red ball hits the pendulum bob" rather than just the "the red ball hits the pendulum bob" — this helps avoid confusing situations where the rest of the condition happens to be true in another stage as well.) Identify the multi-stage action(s) and the final goal. Write what happens in each stage, what triggers each transition, and the final goal down in comments in your sketch. Check the list of requirements to make sure you've covered everything. Note how you satisfy each of the (*) requirements in comments in your sketch.

Completing the Sketch

Practice incremental development — write one small piece at a time and test your sketch to make sure it works as you want after each piece.

Utilize patterns. When you start to implement a piece of your sketch, first identify what structure you need — is there interaction, animation, conditionals, a drawing function, or just a list of statements? Each of these gives you a code template to fill in with the specifics for your particular task. For the questions to ask yourself to identify when a structure is applicable, the code templates, and the questions to ask yourself to fill in the templates, review the slides, handouts, and posted examples for each topic.

Start with the parts you are most confident in, and work on consecutive stages rather than disconnected ones. For example:


Hints and Tips

Handling Multiple Stages

The chain-reaction nature of a Rube Goldberg machine means that it is natural to think of the sketch as a series of stages with trigger points that define the transition from one stage to the next — (stage 1) the red ball is lifted by the elevator until it reaches (trigger point) the top of the ramp, then (stage 2) it rolls down the ramp until (trigger point) it hits the pendulum bob, etc.

This is similar to #2 in lab 5 (the circle moving around the edges of the drawing window) — the circle moved in one direction until it reached a certain point, when it started moving in another direction. The code structure for multiple stages will be the same — this is a state machine pattern with a state variable for the current stage number. Combined with organizing draw() so that all the drawing is done before the updating, this means that you'll have (at least) two "usage" if statements (one for drawing the elements for that stage and one for updating the animation variables in that stage) and (at least) one "transition" if with a case for each of the trigger points and resulting stages.

  // "usage" if structure for two stages
  if ( stage == 1 ) {
  } else if ( stage == 2 ) {
  } 
  // "transition" if structure for three stages
  if ( stage == 1 && trigger condition ) {
    stage = 2;
  } else if ( stage == 2 && trigger condition ) {
    stage = 3;
  }

Physics Quirks

The logic of "if the ball has moved past the wall, flip the sign of the speed" for handling bouncing is simplified — the ball actually hit the wall sometime between the previous frame and this one, and should have actually bounced a little bit away from the wall instead of moving past the wall. This fact means that some strange effects can occur when the ball's speed is low — it can get stuck or seem to escape if its speed drops enough that it can't move back to the proper side of the wall by the next frame.

The correct solution is to work out exactly where the ball should be after a mid-frame wall hit — this takes a bit more math (though it isn't all that complicated) and can be done for extra credit.

A band-aid solution is to do two things when a wall is hit: flip the sign of the speed (incorporating damping if desired) and set the position of the ball so that it is just touching the wall. This creates other quirks (such a movement in effect gives the ball a little more energy each time, so the bouncing tends to not die out completely) but at least avoids the stuck or escaping problem.