This page is optimized for mobile devices, if you would prefer the desktop version just click here

1.14 Game 2302-0140: our first 3d game program  (Page 10/27)

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.

<< Chapter < Page Page > Chapter >>

Read also:

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.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.