<< Chapter < Page Chapter >> Page >

When all of the prey fish have been eaten, the final time that is displayed is the time that was required for the predator to eat all of the fish in thepopulation and the final kill count that is displayed is the same of the original number of prey fish in the population. (Also note that the player must click the Stop button before starting a new game. Fixing this inconvenience will be a good exercise for the student.)

When the escape attempt is successful...

When the escape attempt is successful, a clone of the prey fish in its new location is stored in the preyObjects container and a normalized versionof the fish's direction vector is stored in the displayVectors container as shown in Listing 22 .

Listing 22 . Restore the prey fish to the population.
}else{ //The escape attempt was successful. Restore// a clone of the prey object in its new // location along with its direction vector// to the population. preyObjects.set(cnt,tempPrey.clone());//Save a normalized version of the direction // vector for drawing later. It will// overwrite the previously saved vector and // if you watch closely it will show the// prey object running away from the // predator.displayVectors.set( cnt,tempVectorA.normalize().scale(15.0));}//end else }//end if distance<50 }//end for loop on population

Listing 22 also signals the end of the for loop that began in Listing 19 .

Keep the prey fish in the playing field

I have one more section of code to explain that deals with the behavior of the prey fish during each iteration of the animation loop. The code in Listing 23 causes prey fish that stray outside the playing field to return to the playing field.

Listing 23 . Keep the prey fish in the playing field.
for(int cnt = 0;cnt<preyObjects.size();cnt++){ tempPrey = preyObjects.get(cnt);if(tempPrey.getDisplacementVector(origin).getLength()>0.5*osiHeight){ tempVectorA =tempPrey.getDisplacementVector(origin); tempPrey = tempPrey.addVectorToPoint(tempVectorA.scale(0.1)); preyObjects.set(cnt,tempPrey.clone());}//end if prey object is out of bounds }//end for loop to process each prey object

The playing field is the interior of a sphere

The playing field is defined to be the interior of a sphere that is centered on the origin with a radius that is one-half the height of the off-screen image.The for loop in Listing 23 iterates once for each prey fish remaining in the population. The if statement in Listing 23 tests to determine if a prey fish is outside of the sphere.

If the prey fish is outside the sphere, the code in Listing 23 gives that prey fish a nudge back toward the origin and saves a clone of the prey fish inits new location.

Erase the off-screen image and draw the large circle

Listing 24 erases the off-screen image and draws the large circle that defines the playing field shown in Figure 1 .

Listing 24 . Erase the off-screen image and draw the large circle.
//Erase the screen g2D.setColor(Color.WHITE);GM01.fillRect(g2D,-osiWidth/2,osiHeight/2, osiWidth,osiHeight);//Draw a broken-line circle that represents the // intersection of the spherical boundary with the// x-y plane. Although the prey objects and the // predator can stray outside this sphere, they// prefer to be inside the sphere. GM01.Point3D tempPointA = new GM01.Point3D(new GM01.ColMatrix3D(osiHeight/2,0,0)); GM01.Point3D tempPointB;//The circle is defined by 39 points around the // circumference.g2D.setColor(Color.BLACK); for(int cnt = 0;cnt<39;cnt++){ tempPointB =new GM01.Point3D(new GM01.ColMatrix3D( osiHeight/2*Math.cos((cnt*360/39)*Math.PI/180),osiHeight/2*Math.sin((cnt*360/39)*Math.PI/180), 0));//Connect every third pair of points with a lineif(cnt%3 == 0){ new GM01.Line3D(tempPointA,tempPointB).draw(g2D); }//end if//Save the old point. tempPointA = tempPointB;}//end for loop//Draw the final line required to close the circle new GM01.Line3D(tempPointA,new GM01.Point3D(new GM01.ColMatrix3D( osiHeight/2,0,0))).draw(g2D);

Questions & Answers

how did the oxygen help a human being
Achol Reply
how did the nutrition help the plants
Achol Reply
Biology is a branch of Natural science which deals/About living Organism.
Ahmedin Reply
what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
cell is the smallest unit of the humanity biologically
Abraham
what is biology
Victoria Reply
what is biology
Abraham
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Game 2302 - mathematical applications for game development. OpenStax CNX. Jan 09, 2016 Download for free at https://legacy.cnx.org/content/col11450/1.33
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?

Ask