CS177 C++ Programming
Spring 1998

The Frog and the Bugs

Object

To design and program a small simulation using classes as a modeling tool, and message passing among objects as the simulation mechanism.

Overview

This micro-world has two kinds of object: frogs and bugs. Frogs are simple creatures whose sole object is to move towards the greatest concentration of bugs within their ( limited) range of vision. If they sense a bug within a much smaller range (their eating range) they will eat the bugs, but only one at a time. There can only be one frog in the world. Bugs are even simpler. They move away from each other, moving at a constant speed away from the greatest concentration of bugs within their radius of smell. If they sense a frog, they move at twice the speed away from it.

Modeling

A frog has the following properties:

  1. a position (x,y coordinates)
  2. a speed (fixed at 10 times the normal bug speed)
  3. a direction of motion
  4. a circular range of vision around its position
  5. an circular eating range around its position

A bug has the following properties:

  1. a position (x,y coordinates)
  2. a speed (either normal or twice normal)
  3. a direction of motion
  4. a circular range of smell around its position

 

The Simulation

The world consists of one frog and many bugs ( 100 may not be too small, although testing should be done on a small number). The simplest way to run the simulation is to choose a time period, say 1 second, and update first the directions and then the positions of all the objects according to their speed, direction and position. The frog's direction is updated according to the concentration of bugs in its range of vision. If the range of vision is r, then the test for whether a bug is within it is:

where is the bug's location and is the frog's.

You may use any suitable measure of concentration to give the frog a new directlon (giving a bug a new direction is the same). The direction may be indicated by the xy components of the velocity rather than storing it as an angle. Changing to a new direction is then a matter of altering the velocity components (the speed will either remain the same, if the object is a frog or a bug that has not sensed a frog or it doubles when a bug senses a frog. The motion equations are:

where is the location of the object (frog or bug) are the components of the velocity in the x and y directions, and t is the time period. is the new position of the object.

Computing the center of mass

In order to move the frog towards the bugs, and each bugs away from its fellows, a center of mass calculation is necessary. Do this as follows:

Let the object (the frog or a single bug) be at coordinates Let the rest of the bugs be at Let the center of mass of n bugs be at . Then, since the bugs all have the same mass:

or

This, when solved for gives:

In order to use this formula, the positions of all the bugs must be known, and the number of bugs in the sensory range of the object (frog or bug). The end results are the coordinates of the center of mass. To be useful as a direction vector, subtract the object coordinates and normalize the resultant vector to unit length:

 

The Algorithm

The simulation algorithm has these components:

  1. Update frog's direction according to highest concentration of bugs in its range of vision.
  2. Update frog's position according to new direction and fixed speed.
  3. If any bugs are within eating range, eat the first one found (only).
  4. Update each bug's direction according to either the highest concentration of bugs, or whether the frog is within sensing range
  5. 5. Update each bug's position according to its direction and speed (either normal or twice normal if close to the frog).

This should be repeated until all bugs are eaten. Since the frog does not 'know' how many bugs there are, a simple count-down to zero from the total number of bugs wil1 be suificient. It may be that some bugs get away from the frog, and their coordinates may blow up if integers are used. If the coordinates are represented as long integers, this is more unlikely to happen. If your simulation is getting nowhere, i.e. the frog isn't catching the bugs, choose better initial values and positions to give the frog a better chance.

Output

Write your class member functions so that when the frog eats a bug, a message indicates that the event has taken place. There is no need to track the movement of all the bugs since there are too many, but the frog should "report" on where the best bugs are (the highest concentration within its range of vision).

Presentation

The program should show the output of one simulation run. Indicate on your output how you placed your objects in the world in their initial positions, and the values of:

  1. the range of vision of the frog,
  2. the eating range of the frog,
  3. the sense range of a bug,
  4. the base speed of a bug (bug's alarm speed is twice this, frog's speed is 10 times it).