<< Chapter < Page Chapter >> Page >

The remainder of the inner loop

Listing 17 shows the remainder of the inner loop.

Listing 17 . The remainder of the inner loop.
//Don't test a prey object against itself. if(col != row){//Get the vector from the refPrey object to // the testPrey object.tempVectorA = refPrey. getDisplacementVector(testPrey);//If refPrey is too close to testPrey, move// it away from the testPrey object. if(tempVectorA.getLength()<10){ //Move refPrey away from testPrey by a// small amount in the opposite direction. refPrey = refPrey.addVectorToPoint(tempVectorA.scale(0.2).negate()); }//end if on proximity test}//end if col != row }//end loop on col

It wouldn't make any sense to compare the position of a prey fish with itself, so the if statement in Listing 17 prevents that from happening.

Code is relatively straightforward

Since you already know how to use most of the methods in the game-math library, you should have no difficulty understanding the code in Listing 17 . This code:

  • gets the displacement vector that defines the separation between the primary object and the secondary object,
  • compares the length of that vector with 10 units, and
  • moves the primary prey fish object in the opposite direction by 20-percent of the length of the separation vector if the separation is lessthan 10 units.

As mentioned earlier, however, moving the primary fish object away from one prey fish could cause it to be moved closer to a different prey fish object, sothe separation algorithm is far from perfect.

Listing 17 signals the end of the inner loop that began in Listing 16 .

Save the primary prey fish object and do another iteration

The primary prey fish object may or may not have been moved. Regardless, a clone of that object is created and saved in the container that contains theprey-fish population in Listing 18 .

Listing 18 . Save the primary prey fish object and do another iteration.
preyObjects.set(row,refPrey.clone()); }//end loop on row

After the object is saved in Listing 18 , control is transferred back to the top of the outer for loop in Listing 16 and the next prey fish object in the population is compared with all of the other prey-fish objectsin the population, making corrections to the position of the prey fish as necessary.

Prey fish objects react to the predator

As I mentioned earlier, each prey fish object has a reasonably good defense mechanism to avoid being eaten by the predator. However, in the final analysis,whether or not a prey fish will escape when it encounters the predator face toface is a random process. To some extent, success or failure to escape depends on how quickly the prey fish senses the presence of the predator.

Listing 19 is the beginning of a for loop in which the proximity of each prey fish to the predator is tested and evasive action on the part of theprey fish is taken if the distance between the two is less than 50 units.

Listing 19 . Prey fish objects react to the predator.
for(int cnt = 0;cnt<preyObjects.size();cnt++){ tempPrey = preyObjects.get(cnt);//Get a displacement vector from the prey object// to the predator. tempVectorA = tempPrey.getDisplacementVector(predator);

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