CPSC 120 Principles of Computer Science Fall 2025

Topics: Boids II

Due: Fri 12/5 11:59pm


Introduction

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!


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

Provided Code

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.

Reference

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


Exercises

Exercise 1

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:

  • Create a new sketch named advboids1 and paste in the starter code.

  • Add the boids library to the sketch as directed in Sketches With More Than One File in the Boids I handout.

  • Run the sketch — you'll see a red boid (the prey) and a blue boid (the predator) which start with random positions and velocities and move in straight lines across the screen. (That's because there aren't yet any behaviors to affect its velocity.) Over time the red boid's color will fade to white as it gets hungry. There's also a black circle, which is a fully-stocked food source.

  • Modify the sketch to have 100 prey boids: first complete the Exercise 1a section of the Boids II worksheet, then, based on your answers from the worksheet, array-ify the sketch so there are 100 prey boids.

  • Fill in prey steps 2a and 2b (in the "update prey" section) in draw() so the prey behaves as follows:

    • If the prey boid is within 250 pixels of the predator, it should evade the predator and flock with the other prey boids. (This means four behaviors are active in this step — evade, separation, alignment, and cohesion.)
    • Otherwise if the prey is not near the food (more than 100 pixels away) and there's potential for colliding with another prey boid, avoid the collision.
    • Otherwise if the prey boid is hungry (hunger is 0 or lower) and is within 300 pixels of the food, it should arrive at the food.
    • Otherwise the prey boid should wander.

    Complete the Exercise 1b section of the Boids II worksheet before writing the code.

    See the "Evade and Pursue" section below for how to use the evade behavior, and refer back to earlier exercises for the other behaviors. Use the weights you worked out for #2 in Boids I as a starting point for the flocking behaviors, though you may need to tweak them a bit. You'll also need to find a good weight for evade.

  • Fill in predator steps 2a and 2b (in the "update predator" section) in draw() so the predator behaves as follows:

    • If there's prey in sight, the predator should pursue it.
    • Otherwise the predator should wander.

    Complete the Exercise 1c section of the Boids II worksheet before writing the code.

    See the "Evade and Pursue" section below for how to use the pursue behavior, and refer back to earlier exercises for the other behaviors. What is "in sight" for the predator is defined by the predator's neighborhood — you can use the magnitude of the steering vector returned for the predator's pursue behavior to determine if any prey is in sight.

Evade and Pursue

The 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 2

In 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:

  • Name the sketch advboids2.

  • There must be some sort of visible and describable individual or group behavior that arises from a combination of behaviors. For example, you might use offset pursuit to have a group of boids fly in formation, or you might have a game of tag where a boid pursues another that tries to hide behind obstacles. Describe the result — e.g. "fly in formation" or "game of tag" — and include the description in a comment in your sketch. (If you can only describe what happens in terms of the behaviors it is built from, it's not a describable behavior.)

  • Do something substantially different from what was implemented in Exercise 1 and in the Boids I exercises. Simply combining two or more exercises doesn't count as "substantially different" — add to them.

  • Use at least one behavior not used in Exercise 1 and the Boids I exercises. There are additional behaviors in the boids library file — look through that, focusing on the comments and the function headers. (You can look at how the behaviors are implemented in the function bodies if you are curious, but it isn't necessary to do that to use them.)

  • Demonstrate all three patterns for combining behaviors: a single active behavior, multiple behaviors active at the same time, and choosing between behaviors. These can overlap, for example, a boid which seeks the mouse when the mouse is nearby and otherwise flocks with other boids covers all three patterns.

To do this:

  • Create a new sketch named advboids2 and paste in the single-boid starter code (from Boids I) if you will have one kind of boid or the predator-prey starter code from Exercise 1 if you will have more than one kind of boid (like predator and prey).

  • Add the boids library to the sketch as directed in Sketches With More Than One File in the Boids I handout.

  • Implement the sketch. First adjust the animation variables as needed — remove unnecessary ones like preyhunger, food, and foodpos from the predator-prey starter code, or add another set of boid properties and another copy of steps 2-4 in draw if you have additional kinds of boids. Next, array-ify the sketch (before adding behaviors) if you have a group of boids because group behaviors like separation and alignment require an array of the boids in the group. Finally, implement steps 2a and 2b for your boids.

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