| CPSC 120 | Principles of Computer Science | Fall 2025 | 
Topics showcase applications of the core concepts, in this case acceleration/deceleration and making choices.
Successfully completing this topic means that you are able to:
define/explain the terms "gravity", "air resistance", "bouncing", and "damping" — what they are and how they affect motion
implement physically-based motion taking into account gravity, air resistance, bouncing (for an ellipse hitting horizontal or vertical surfaces), and damping
Hand in a hardcopy (paper) of your worksheet in class or under my office door (Lansing 302).
To hand in your sketches:
Make sure that your name and a short description of the sketch are included in a comment at the beginning of each sketch.
Make sure that you've auto-formatted each sketch.
Copy the entire physics1 and physics2 directories from your sketchbook (~/cs120/sketchbook) to your handin directory (found inside /classes/cs120/handin).
Like labs, topics are individual assignments — what you hand in must be your own work, your own ideas, your own effort. You may get help in office hours and from Teaching Fellows, but you may not work together with a partner or in a group with others to create solutions or write code.
The policies on late work and extensions, academic integrity, and the use of AI for topics are the same as for lab 2. Review the policies there. One extension token is needed for revise-and-resubmit without an initial handin.
Also review assignments and evaluation on the Policies page for how topics factor into the final grade. The short version: topics are optional for a passing grade (C-), but achieving proficiency for at least some topics is required for a higher grade.
Review the slides from 10/29 for the specific pattern elements for physically-based motion, and the relevant slides, in-class exercises handouts, and in-class exercise solutions for the core concepts used in this topic (animation, acceleration/deceleration, making choices).
Since the simulation only works in discrete steps, it is likely that the object will have moved just beyond the edge of the window before the need to bounce is detected. Since air resistance and damping work to decrease the speed, it is possible that the object won't be moving fast enough after the bounce to get back into the window in the next frame, resulting in the object getting stuck along the edge or escaping the window entirely. The mathematically correct solution for this situation is somewhat complicated (*), but a quick "fix" is to do two things when a bounce occurs: flip the sign of the speed and set the position of the object so that it is just at the edge of the window. The min, max, and constrain functions can be handy for this — use them similarly to how you kept the circle from shrinking again in lab 4 #1. (Click on the links for the API documentation.)
(*) The key to the correct solution is recognizing that the bounce actually happened at some point between the two frames, so properly handling a bounce requires not only flipping the sign of the speed but also adjusting the position of the ellipse to account for the post-bounce movement during that fraction of the between-frame time after the bounce. Implementing this is an extra credit option.
Bouncing is the same for obstacles as for the sides of the window: if a bounce occurs, flip the sign of the speed (include damping if desired) and employ the "fix" described above (adjust the position of the object to be just touching the side of the obstacle it bounced off of).
Detecting if a bounce occurred, however, is a bit trickier. There are two considerations:
The obstacle doesn't cover the entire window, so the ellipse only hits the top of the obstacle (for example) if the y coordinate of its bottom side is at or below the top side of the obstacle and the ellipse is also between the left and right sides of the obstacle — there's no collision (and no bounce) if the ellipse is entirely left or entirely right of the obstacle, even if its bottom side passed the top side of the obstacle.
Furthermore, the ellipse could be entirely below the obstacle — the above conditions for a bounce would still apply, so we need an extra condition: in the previous frame, the ellipse's bottom side was above the top of the obstacle (i.e. it hadn't yet hit).
Handling the second consideration means adding animation variables prevx and prevy for the previous position of the ellipse. Initialize them to the same as x and y, then update prevx and prevy before updating x and y:
prevx = x; prevy = y; x = x + ...; y = y + ...;
Do the exercises in order. Note: following the patterns discussed in class is required for credit — achieving the end result by some other means will not count.
Read through all of each exercise before you start on it. In particular, note that the "to do this" steps are what you should actually do to complete the exercise — don't just read the first sentence of the problem, look at the example, and try to write the sketch from there. Follow the steps!
Put your name and a description of the sketch in comments at the beginning of each sketch. Also don't forget to Auto Format your code before handing it in.
Be sure to save your sketch frequently (ctrl-S). (Every time you run your sketch is good.) The editor does not auto-save!
| 
   Create a sketch which contains four bouncing balls, as shown. (Click to reset the demo.) Requirements for your sketch: 
  | 
	 | 
    
To do this:
Create a new sketch, add your name and a description of the sketch in comments at the beginning, and save it as physics1.
Start with the basic sketch structure: open a drawing window and clear the background.
Read about the bounce math "fix" in the Preliminaries section above so you can include that with handling bouncing.
Add the balls one at a time: red, yellow, blue, purple.
Add clicking the mouse to reset.
| 
   Create a sketch which contains one bouncing ball and a rectangular obstacle, as shown. (Click to reset the demo.) Requirements for your sketch: 
  | 
	 | 
    
To do this:
Create a new sketch, add your name and a description of the sketch in comments at the beginning, and save it as physics2.
Start with the basic sketch structure: open a drawing window and clear the background.
Add the ball, with gravity, air resistance, damping, and bouncing. (No obstacles yet, just bouncing off the sides of the window.)
Draw the rectangle for the obstacle.
Read about bouncing off of obstacles in the Preliminaries section above.
Complete the Exercise 2 section of the physics worksheet.
Add bouncing off of the obstacle, one side at a time. It is probably easiest to start with the top side since the ball is most likely to hit that.
Add clicking the mouse to reset.
For extra credit, account for the mid-between-frame bounce and correctly compute the post-bounce position of the balls in both sketches instead of using the simpler bounce math "fix".