<< Chapter < Page Chapter >> Page >

The code in Listing 19 gets the next prey fish in the population and gets a displacement vector describing the separation of the prey fish from thepredator.

Prey fish object takes evasive action

If the prey fish is at least 50 units away from the predator, the prey fish simply goes on swimming without concern for the predator. However, if the preyfish is less than 50 units away from the predator, the prey fish takes evasive action in an attempt to escape the predator. The evasive action is accomplishedin Listing 20 , and this is where some of the random behavior comes into play.

Listing 20 . Prey fish object takes evasive action.
if(tempVectorA.getLength()<50){ tempVectorA = tempVectorA.negate().scale(random.nextDouble()); tempPrey =tempPrey.addVectorToPoint(tempVectorA);

The prey fish moves by a random distance

In Listing 20 , the negative of the displacement vector separating the prey fish from the predator is scaled by a random value ranging from 0 to 1. Then theprey fish is moved by the distance and direction specified by the resulting vector. If the random value is 0, the prey fish won't be moved at all. If therandom value is 1, the distance between the prey fish and the predator will be doubled. For values between 0 and 1, the prey fish will be moved differentdistances away from the predator.

Did the prey fish escape?

Once the prey fish has taken the evasive action and has (possibly) moved away from the predator, the separation between the prey fish and thepredator is tested again in Listing 21 .

Listing 21 . Test the separation again.
tempVectorA = tempPrey.getDisplacementVector(predator);if(tempVectorA.getLength()<25){ tempPrey = preyObjects.remove(cnt);displayVectors.remove(cnt); Toolkit.getDefaultToolkit().beep();killCount++; timer.setText("" + (new Date().getTime() - baseTime)+ " / " + killCount);

If the new separation is at least 25 units, the escape attempt is deemed successful and the prey fish will continue to live. (You can decrease or increase the threshold distance used in the test in Listing 21 to cause the prey fish object to be more or less successful in the escape attempt. If youmake the threshold distance small enough, the prey fish will almost always escape.)

When the escape attempt is not successful...

When the escape attempt is not successful, the remaining code in Listing 21 is executed. The prey fish is deemed to have been eaten by the predator.Therefore, the prey fish and its direction vector are removed from the containers that contain them. This causes the prey fish to be removed from thepopulation.

In addition, the program sounds a beep to notify the user of the successful attack by the predator and the kill count that is displayed in the bottom rightof Figure 1 is incremented by one.

Display elapsed time and number of fish eaten

Finally, the code in Listing 21 displays the elapsed time in milliseconds since the Start button was clicked along with the number of fish that have been eaten. Note that the value that is displayed is the elapsed time whenthe most recent prey fish was eaten by the predator and is not the total elapsed time since the Start button was clicked. (The elapsed time value won't change again until another prey fish is eaten.)

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