CS184 Lab exercise 5 - due Thursday 10/1/98 at 10pm

Notes on the lab:
To submit the lab, create a directory called lab5.

To submit, cd to the lab5 directory and type "submit lab5".

This lab is a partner lab. You should do it in groups of two. Only one partner should turnin. Make sure you include a "README" file that identifies the two partners and who did what in the lab.

A Fishtank

This lab is an exploration of flocking behavior with some simple actors - namely fish swimming.

The fish model

Each fish should be a simple model with a body and a tail. For example, scaling a sphere into an ellipsoid will give quite a good fish body. Its also OK to use a single (non-solid) polygon for the fish body. Some fish are very flat anyway (e.g. Angelfish). The tail should be a triangular polygon or perhaps a scaled cone.

There should a script that animates the tail, swinging it back and forth relative to the fish's body. A periodic sinusoidal pattern is fine.

You should texture-map the fish body and tail. The texture map doesnt need to be elaborate, and you can create it with the Microsoft paint tool. Its enough to show main features like eyes and gills. Since the paint program produces simple, gaudy color patterns, its good to use it for tropical fish.

Fish behavior

Now you should write a script that animates the fishes motion. It should swim around in a cylindrical tank. Its fine for the fish to always go in one direction (e.g. clockwise). But the motion of the fish should be interesting - it should not execute a perfectly circular motion. Real fish are interesing enough that people watch them for hours and spend money feeding and caring for them.

The fish motion should have 4 degrees of freedom. The fish should be able to move in x,y,z and rotate about a vertical axis. Other rotations are not needed. The fish motion should be computed by integrating a velocity v. At each time, the fish's position will be updated to x + v dt, where x is the old position, and dt is the time step since the last time sensor event. The velocity should include one term that steers the fish along a circular trajectory around the tank. But you should add other terms that cause the motion to deviate from this ideal. This will require calling a random number generator such as java.util.Random. You can add random terms directly to the velocity, but this will just cause the fish to wobble around the tank. A better approach is to test whether the random number is in a certain range, and if so, set a new desired depth or radius. Then adjust v to move the fish toward this depth or radius over subsequent time steps. When the fish reaches the desired state, it can make a random choice again.

Flocking/schooling behavior

When you get bored watching this fish, you can add some others. These fish will basically follow the "head fish" that you built earlier. They can have the same bodies as the lead fish. Their behavior is to follow the lead fish and hopefully not collide with each other. Its hard to prevent collisions, but you should program them so that collisions are unusual. e.g. you could program each fish to maintain a certain displacement from the lead fish, and the displacement should be different for each fish. These fish should also move by integrating a velocity. They can all use the same script, with different start-up parameters e.g. for the displacement from the lead fish (and perhaps for tail-motion frequency). You will need to pass the lead fish node as an argument to these scripts, so that they can find out where the lead fish is.

An alternative (which generally looks more realistic) is to have each fish follow a nearby fish ahead of it instead of the leader. You can do this with the same script. Instead of passing the lead fish node to each script, you pass the node for the fish that you want this fish to follow. The displacements will be smaller than before.

The number of fish is up to you and will be limited by the rendering abilities of the PCs. We suggest at least 6 fish.