| CPSC 120 | Principles of Computer Science | Fall 2025 |
|
Topics showcase applications of the core concepts, in this case arrays and making choices. Behavioral animation is an animation and simulation technique where simple behaviors are combined to yield complex individual and group behavior. These exercises are a chance to explore some of those combinations and to see how one might write a program to simulate complex group behavior; the focus is on combining behaviors to get interesting results rather than implementing the steering behaviors themselves. The Boids I exercises utilize the three patterns for boid behavior: a single behavior, multiple behaviors active at once, and choosing between behaviors or sets of behaviors. These exercises bring all of those elements together in a simulation of a small ecosystem (shown). Important prerequisite: these exercises build on the Boids I exercises — complete those first! |
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 advboids1 and advboids2 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.
Exercise #1 uses a variation of the template used in Boids I which contains two boids (predator and prey) and a food source for the prey boid. (starter code)
The boids library is the same as in Boids I.
See the "Preliminaries" section in the Boids I handout for a refresher on working sketches with more than one file and implementing boids in Processing, and the three exercises in Boids I for information about specific steering behaviors and the three patterns for combining behaviors.
Also review the slides from 11/12 (behavioral animation and boids) for specifics related to this topic and the relevant slides, in-class exercises handouts, and in-class exercise solutions for material related to the core concepts (arrays, making choices).
Do the exercises in order. Note: you must use the provided code and follow the patterns discussed in class and below 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!
|
Your task is to create a sketch which simulates an ecosystem where prey boids forage for food and try to escape predators, as shown in the demo at the top of this page. The red boids are the prey which run from the predator and the blue boid is the predator which chases the prey. Over time the color of red boids fades as they become hungry. The black dot is a food source; its colors fades as it becomes depleted. To create this sketch:
Evade and PursueThe boids library contains the following function for this behavior:
// compute the evade steering vector
// pos, vel - position and velocity of boid
// maxspeed - boid's max speed
// pursuerpos, pursuervel - position and velocity of pursuer
PVector computeEvade ( PVector pos, PVector vel, float maxspeed, PVector pursuerpos, PVector pursuervel ) { ... }
// compute the pursue steering vector - pursues the nearest quarry within the boid's neighborhood
// pos, vel - position and velocity of boid
// radius, angle - define this boid's neighborhood
// maxspeed - boid's max speed
// quarrypos, quarryvel - positions and velocities of potential quarries
PVector computePursue ( PVector pos, PVector vel, float radius, float angle, float maxspeed, PVector[] quarrypos, PVector[] quarryvel ) { ... }
These functions return a steering vector with a magnitude of 0 if there is nothing in sight. Exercise 2In this exercise you'll create your own sketch which demonstrates an interesting combination of behaviors. Here's your chance to experiment! For full credit your sketch must include the following elements:
To do this:
For extra credit, create an especially elaborate or nifty sketch and/or implement and use one or more of your own steering behaviors. Stop by office hours to discuss possibilities if you are interested in implementing new steering behaviors. (Some familiarity with working with vectors and/or geometry would be helpful.) |