CPSC 120 Principles of Computer Science Fall 2024

Lab 3
Animation

Due: Fri 9/20 at the start of lab

Labs are due at the start of lab. It is OK if you show up to lab and copy your files to the handin directory at the very beginning of lab, but this should take at most a couple of minutes and you should not spend the next lab period finishing up the previous week's lab. I will check timestamps, and files handed in more than five minutes after the start of lab will be considered late.

See the policy on late work and extensions.


Introduction

This week's lab deals with animation — things that move (or change in other ways) on their own.

Successfully completing this lab means that you are able to:

Academic Integrity and Collaboration

Labs are a chance to practice and gain understanding. You may get help in office hours, at Teaching Fellows, and from other students and may use other materials (such as reference books or websites) but the course materials (including provided links or references to documentation and other materials) along with office hours and the Teaching Fellows should be your primary resources. Always start with these!

You may not, however, copy or be in possession of someone else's program or solution before you have handed in your own and you may not write code collaboratively with another student. You must document any help received and any outside resources used. See the full collaboration policy for more on this.

Be careful not to rely too much on others — things often look easy (or at least easier) when someone else does it. (You don't want the exam to be where you discover this!) You should always make the first attempt at doing something yourself.

Also keep in mind that the goal of the exercises is learning the process of creating programs, not obtaining any particular program. When you get help, also ask about that process — if you're stuck on what to do next, ask not only what to do next but also how one knows that's what to do, or if your program isn't working correctly, ask not only what's wrong and how to fix it but also how to track down the problem for yourself. And finally, make sure that you always fully understand the help you received — you should be able to explain your solution to someone else, and should never just write down code that someone else wrote or told you to write without be able to explain what it does and why.

Handin

To hand in your work:


Preliminaries

Getting Started

Before you start writing code, remind yourself of:

All of these things have been covered in class, and the slides and in-class exercises handouts posted on the schedule page are the places to find this information. It is also a good idea to review the examples from class and make sure you understand the role of each line of code in the sketch — what does it do? Why is it there? How does it relate to the patterns for sketch structure, variables, and animation?

Then, as you begin each exercise, remember decomposition and incremental development! You might draw the elements of the scene that don't change first, then draw animated elements with their initial characteristics (position, size, color, etc), and then finally add in the animation variables to make things change.

The first two exercises guide you through this process, but you'll need to do it for yourself in #3 (and in future sketches) — be thinking about how those steps exemplify the process.

Representation

Often the question "what changes from one frame to the next?" can have multiple answers. For example, for the three-circle animation from Wednesday's in-class exercises, both of the following are correct answers:

However, the second version is simpler — realizing that the relative position of each of the three circles remains fixed with respect to the other circles means that we don't need separate x and y coordinate variables for each circle. Two variables (one x coordinate variable and one y coordinate variable) suffice because the positions of the other two circles can be computed from the position of one.

This is one aspect of representation — how many different concepts are there, and thus how many different variables are needed? The positions of the three circles aren't really three separate concepts because there is a fixed relationship between the values. Representations with fewer variables (within reason) are preferred — less typing and fewer chances for errors.

A second aspect of representation is what exactly the variable(s) stand for. "The position of the circle pattern" is still vague, since the pattern has height and thus spans many x values and many y values when it is drawn. So, what position(s) would be convenient? One choice is a convenient point for one of the shapes within the pattern — ellipses can be drawn in CENTER or CORNER mode, so a reasonable choice would be to have the variables represent the center or corner of one of the circles. Another choice is a convenient point for the pattern as a whole, such as the lower left corner of an imaginary box around the whole pattern. How to decide? Pick what seems simplest or most intuitive to you, then make sure to name the variable appropriately and/or add a descriptive comment for the declaration so that it is clear to the reader what you chose.

Naming and Commenting

Tips

System Variables width and height

Processing provides two system variables width and height for the width and height of the drawing window, respectively. (Note that you can only use these values after the window has been opened with size.)

Use these variables to design more flexible sketches. For example, if you want an ellipse to be centered at the lower right corner of a 300x400 window, you could write

  ellipseMode(CENTER);
  ellipse(300,400,50,50);

However, if you change the size of the window but want the ellipse to still be in the lower right corner, you'll have to change both the size command and the coordinates where you've drawn the ellipse. A simpler solution is to instead write

  ellipseMode(CENTER);
  ellipse(width,height,50,50);

This will always center the ellipse at the lower right corner no matter what size the window is, so changing the window size means only change the size command.

Note that this doesn't mean you should always use width and height — think about whether you want something at particular coordinates no matter what size the window is or whether you want it at the same relative position e.g. lower right corner or center. Use width and height only in the latter case.

About Radians

Exercise 2 involves drawing arcs (part of a circle). You may be familiar with measuring angles in degrees — a complete circle is 360 degrees, half a circle is 180 degrees, and so forth.

Angles can also be specified in radians rather than degrees — a whole circle is 2π radians. The pictures below show how degrees and radians relate to each other:

      

Processing uses radians rather than degrees, so when you work with angles (such as with the arc command, which you'll need in exercise #2 below) you'll need to either use radians directly or convert from degrees to radians.

Important note: Processing measures angles going clockwise rather than counterclockwise, so what is shown above as 135 degrees or 3π/4 radians would be -135 or 360-135 = 225 degrees or -3π/4 = 2π-3π/4 = 5π/4 radians in Processing.

To work with radians directly, you'll need to express the value π (pi). Processing defines constants PI, HALF_PI, QUARTER_PI, and TWO_PI. This means you can just write PI, HALF_PI, etc when you want those values, and you can use them in expressions such as PI/3 (60 degrees) or 7*PI/6 (210 degrees). For example, the documentation for arc includes the following:

  size(400,400);
  arc(200, 200, 320, 320, 0, PI+QUARTER_PI, PIE);

This uses radians directly. (PI+QUARTER_PI = 5π/4)

Alternatively, to convert from degrees to radians, use

  radians(deg)

where deg is the value in degrees. For example:

  size(400,400);
  arc(200, 200, 320, 320, radians(0), radians(225), PIE);

Exercises

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. (Refer back to lab 2 for more on Auto Format if needed.)

Exercise 1

The goal in this exercise is to create a sketch named lab3a which works like the example shown — in particular, there should be an animated cloud which moves from left to right across the drawing window at a reasonable speed. Clicking the mouse resets the cloud to its starting position.

Include all of the elements shown — cloud, building, grass. You don't need to match the sizes, positions, and colors of the example exactly, but you should aim for something close. (In particular, the cloud should be made up of at least three ellipses and should be a very light gray, not white.)

The initial position of the cloud should just be outside the window to the left — it should take a few moments (but not very long) to appear. The cloud should pass behind the building.

Your browser does not support the canvas tag.

To create this sketch:

Exercise 2

The goal in this exercise is to create a sketch named lab3b which works like the example shown below — in particular, there should be a two-color wheel which rolls at a reasonable speed along the bottom of the drawing window. Design your sketch so that the wheel starts in the lower left corner of the window and moves along the bottom no matter what size the window is. In addition, the rotational speed and horizontal motion of the wheel should be matched so that the wheel appears to be rolling rather than spinning or sliding. You do not need to make the wheel reset when it reaches the right edge of the window as shown in the demo; it is fine for the wheel to just disappear off the right side of the window.

Your browser does not support the canvas tag.

To create this sketch:

Exercise 3

  • Create an animated sketch of your own design. (Name this sketch lab3c.) What the sketch depicts is up to you (here's a chance to be creative!) but for full credit it must include the following required elements:

    To create this sketch, use the same process as for #1 and #2 — create a new sketch with the right name, use the template for the right program structure, work on building up the parts of the picture that don't change, and then add the animated parts one at a time, working through the animation questions for each.

    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(es). More creative and challenging elements will earn more points.