CPSC 120 Principles of Computer Science Fall 2025

Topics: Physics

Due: Mon 11/24 11:59pm


Introduction

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:


Handin

Hand in a hardcopy (paper) of your worksheet in class or under my office door (Lansing 302).

To hand in your sketches:


Policies

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.


Preliminaries

Reference

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

Bounce Math "Fix"

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 Off Obstacles

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:

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 + ...;

Exercises

  1. Create a sketch which contains four bouncing balls, as shown. (Click to reset the demo.) Requirements for your sketch:

    • Name your sketch physics1.

    • Use the same colors as in the demo — red, yellow, blue, purple.

    • All four balls should start in the same position in the upper left corner of the window and have the same initial speeds (0 for the y speed, greater than 0 for the x speed).

    • All four balls should be subject to gravity and should bounce off the bottom, left, and right sides of the window. (It's OK for things to go off the top temporarily.)

    • The yellow ball should also be be subject to air resistance (in both x and y).

    • The blue ball should also be subject to damping when it bounces.

    • The purple ball should also be subject to both air resistance (x and y) and damping.

    • Choose a value for gravity and k values for air resistance and damping so the effects are noticeable but not too drastic. Use the same values for all of the balls so you can see the effect of the different components on the motion. Note: damping should apply for bounces in both x and y — the demo currently only applies damping in y.

    • Use the bounce math "fix" described in the Preliminaries section above to keep the balls from getting stuck along an edge or escaping the window.

    • Clicking the mouse should reset the balls to their starting positions and speeds.

  2. To do this:

  3. Create a sketch which contains one bouncing ball and a rectangular obstacle, as shown. (Click to reset the demo.) Requirements for your sketch:

    • Name your sketch physics2.

    • The ball should start centered horizontally in the window and near (but not at) the top, and have a random speed (both x and y). Allow the speed to be both positive and negative so the initial movement can be in any direction.

    • There should be a rectangular obstacle somewhere in the bottom half of the window, with space around the rectangle on all sides. You do not need to match the size and position of the rectangle in the demo.

    • The ball should be subject to gravity, air resistance (x and y), and damping and should bounce off all four sides of the window. The ball should also bounce off all four sides of the obstacle. Use a different values for damping for the sides of the window and for the obstacle, so that the window is bouncier than the obstacle.

    • Clicking the mouse should reset the ball to its starting position with a new random speed.

  4. To do this:

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