CPSC 120 Principles of Computer Science Fall 2024

Lab 10
Arrays

Due: Fri 11/15 at the start of class

Labs are due at the start of class. It is OK if you show up and copy your files to the handin directory at the very beginning of class, 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.


Introduction

Your browser does not support the canvas tag.

Loops expand our ability to easily draw lots of similar things when there is some kind of pattern in the positioning, color, size, and orientation of those things — stripes on a road, a flock of birds, a patch of wildflowers, shapes in a quilt block, etc.

Arrays expand our ability to easily animate lots of similar things in similar ways by making it possible to write loops where what is changing from one repetition to the next is (or includes) the animation variables themselves — arrays are used when each thing would need its own set of animation variables because the values may be different for different things (without a pattern).

This week's lab focuses on working with arrays using a development strategy intended to simplify the task of animating lots of things: first figure out the drawing and animating for a single thing, then transform the single-thing solution to a many-thing solution by adding arrays.

Animating lots of things has many applications in computer graphics. One such example is particle systems, a technique used to model complex natural phenomena like fire, smoke, water, falling leaves, clouds, snow, dust, hair, and fur using huge numbers of very small particles. The fireworks in the demo below and in exercise 3 are inspired by the idea of particle systems and exercise 2 uses a basic particle system to create the spark plume. Next week's lab will explore behavioral animation, a technique where simple rules applied to individuals can give rise to complex group behaviors often observed in the natural world.

Successfully completing this lab means that you are able to:


Academic Integrity and Collaboration

The short version:

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

Handin

To hand in your work:


Preliminaries

Getting Started

As always, refer to the class materials (slides, in-class exercises handouts, and examples) posted on the schedule page. In particular, see this week's slides on array-ification, and compare the before-and-after versions of the examples in the slides and the posted solutions for the in-class exercises.


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

This lab is about creating sketches with many animated things using the array-ify process discussed in class. Be sure to follow the "to create this sketch" steps in each exercise! In all three exercises, you have been provided with a complete sketch that animates one item — array-ify it and make other modifications as directed in each exercise rather than writing a new sketch from scratch. Little credit will be given if this process is not followed.

Exercise 1

Your browser does not support the canvas tag.

Create a sketch called lab10a which produces an animation similar to the one shown — there should be 50 circles, each of which starts in a random position and then, one by one, starts to move after a delay. Stagger the delays so that the first circle starts to move after 40 frames, the next after 80 frames, the next after 120 frames, etc. (Click to reset the demo; you do not need to be able to click to reset in your sketch.)

To create this sketch:

About Countdown Timers

In this sketch, the animation starts after a delay instead of as soon as the sketch starts. This delayed start can be implemented using a variation of the state machine pattern where a countdown variable is used in place of the state variable. The countdown variable is an int which is initialized to some value greater than 0. Update the countdown by subtracting 1, and use it by doing the action when its value is less than or equal to 0.

In the provided code for this exercise, the countdown variable is called timer. Observe that it is initialized to 200 in setup(), so that the ellipse will start moving after 200 frames. Also observe the update and use steps in draw() — the value of timer is decreased by 1 (update) and the ellipse's x coordinate is updated when the countdown has reached 0 (use).

Exercise 2

Create a sketch called lab10b which produces an animation of a plume of sparks. (The picture below is just a single snapshot; in the completed sketch, the plume will move to the right and gradually shift colors from red through the rainbow to purple.)

To create this sketch:

About Particle Systems

Particle systems have their origins in special effects for movies. They were pioneered in the early 1980s by William Reeves at Industrial Light and Magic and first introduced in 1982 in Star Trek II: The Wrath of Khan for the Genesis effect. (watch the clip from the movie)

In a nutshell, a particle system consists of a large number of individual particles which have animated properties such as position, speed, color, transparency, size, and lifespan and which are drawn as very small shapes (such as an ellipse or short time) plus a set of rules for how particles are animated. The lifespan defines how long a particle remains visible — at the end of its lifespan, the particle dies off and is replaced by a new particle. New particles are produced by an emitter which defines how the particle's properties are initialized. The emitter itself can be animated so that later particles are generated with different properties from earlier ones.

In this sketch, particles have position, speed, color, transparency, and a noise parameter t. Noise is used to provide a smooth random variation when each particle's x coordinate is updated so that they waft upwards in a more realistic-looking way rather than moving in a straight line. The emitter defines the x coordinate and color (hue) of the new particles. Both of these properties of the emitter are animated so the location of the plume and its color change over the course of the animation.

Particles die off when they become fully transparent and are thus no longer visible. The update part of draw is thus at its core a two-alternative on-the-spot decision — if the particle is still visible, update its properties (position, speed, transparency, etc), and if not, reset its properties to those for a newly-generated particle (effectively replacing the old particle with a new one). So that the particles aren't all replaced at the same time, dead particles are not immediately resurrected. This is handled by the

  else if ( random(0,100) <= 1 )

part of the conditional in draw. random(0,100) generates a random number between 0 and 100; there's only a small chance that the number generated will be less than or equal to 1 so in each frame only a few of the dead particles are replaced. This makes for a much better-looking effect.

About HSB Color

You might notice the statement colorMode(HSB) in setup() and the animation variables named hue, saturation, and brightness relating to color. RGB is just one way to describe colors, via the combination of red, green, and blue light. HSB (or HSV) is another color model which aligns more closely with how humans perceive and describe colors — hue captures the color itself (red, orange, yellow, etc), saturation captures the intensity of the color, and brightness (or value) captures how light or dark a color is. HSB is used instead of RGB for this sketch because it makes it easy to smoothly change the color (hue) without affecting how intense or bright the color is (saturation and brightness).

HSB/HSV color modelRGB color model
images source

Exercise 3

Create a sketch called lab10c which produces an animation of a series of exploding fireworks like the demo at the beginning of this handout (but without the fading trails and moon and mountains scenery).

To create this sketch:

About Fireworks

The fireworks sketch is a two-stage animation — there's a launch stage where the spark(s) move upwards, and an explosion stage where the spark(s) fall back towards the ground. This is an example of a state machine with two states (launch and explode) and the boolean state variable exploding keeps track of the current stage. Look for the other elements of the state machine pattern in the provided code — actions that only take place in one of the stages and the transition from one state to another.

The transition between stages is based on the passage of time, so a countdown timer named timer is used to keep track of this. Since the same timer is used for both stages, switching between stages is based on both the timer reaching 0 and the current stage.

There's some initialization that needs to be done for each stage — transitioning from one state to another isn't just a matter of setting the state variable to a new value. Note those extra things going on in the "handle stage transitions" code.

Extra Credit

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

More challenging elements will earn more points.