<< Chapter < Page Chapter >> Page >

A few more housekeeping details

Listing 8 takes care of a few more housekeeping details before the animation actually begins.

Listing 8 . A few more housekeeping details.
//Create a reference point to mark the origin. Among // other things, it will be used as the center of a// sphere, which in turn will be used to attempt to // keep the prey and predator objects from leaving// the playing field. GM01.Point3D origin =new GM01.Point3D(new GM01.ColMatrix3D(0,0,0));//Declare some variables that will be used to // compute and save the average position of all of// the prey objects. double xSum = 0;double ySum = 0; double zSum = 0;

Let the show begin

Listing 9 shows the beginning of the animation loop, which will continue to execute for as long as the value of the variable named animate is true.

Listing 9 . Beginning of the animation loop.
while(animate){ //Compute and save the average position of all the// prey objects at the beginning of the loop. Save // the average position in the Point3D object// referred to by preyCenter. xSum = 0;ySum = 0; zSum = 0;for(int cnt = 0;cnt<preyObjects.size();cnt++){ tempPrey = preyObjects.get(cnt);xSum += tempPrey.getData(0); ySum += tempPrey.getData(1);zSum += tempPrey.getData(2); }//end for loop//Construct a reference point at the average// position of all the prey objects. preyCenter = new GM01.Point3D(new GM01.ColMatrix3D(xSum/preyObjects.size(), ySum/preyObjects.size(),zSum/preyObjects.size()));

The value of animate is set to true by clicking the Start button and is set to false by clicking the Stop button. Setting the value to false causes the run method to terminate, thereby stopping the animation.

Attack toward the geometric center

When the predator attacks, it will move to and through the geometric center of the cluster of prey fish, eating prey fish all along the way. The geometriccenter of the cluster of prey fish is computed and saved by the code in Listing 9 as the average location of all the prey fish in the population.

Note that a vector that points to the geometric center may or may not be agood indicator of the best direction for eating large numbers of prey fish during an attack. In Figure 1 , for example, a predator attacking from the 11:00 o'clock position and heading toward the center would encounter quite a few preyfish. However, a predator attacking from the 8:00 o'clock position and heading toward the center would encounter far fewer prey fish.

You will also learn that although the predator will always attack in a direction pointing toward this geometric center, the positions of the prey fishcan change after the center is computed and before the attack begins, causing the position of the actual geometric center to change, before the predator hasan opportunity to attack. That can decrease the probability of a successful attack by the predator.

These issues form part of the strategy of the game that must be mastered by the player in order to earn a good score.

Questions & Answers

Explain the following terms . (1) Abiotic factors in an ecosystem
Nomai Reply
Abiotic factors are non living components of ecosystem.These include physical and chemical elements like temperature,light,water,soil,air quality and oxygen etc
Qasim
what is biology
daniel Reply
what is diffusion
Emmanuel Reply
passive process of transport of low-molecular weight material according to its concentration gradient
AI-Robot
what is production?
Catherine
Pathogens and diseases
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 respiration
Deborah
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
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