CS177 C++ Programming
Fall 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 is a pond containing 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 be any number of frogs in the world. Bugs are even simpler. They tend to bunch together, moving randomly at a constant speed, but toward the greatest concentration of bugs within their sensory range, and keeping a minimum distance from other bugs. If they sense a frog, they move at three times their normal speed directly away from it.
Modeling
A frog has the following properties:
A bug has the following properties:
The pond has the following properties:
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:
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: