<< Chapter < Page Chapter >> Page >

The code in Listing 26 is very similar to code that I explained earlier in conjunction with the behavior of the prey fish. Therefore, no explanationbeyond the embedded comments should be required.

Execute the attack

When the user clicks the Attack button, the value of the variable named attack is set to true , causing the code in Listing 27 to be executed during subsequent iterations of the animation loop.

Listing 27 . Execute the attack.
}else{//attack is true predator = predator.addVectorToPoint(predatorVec.scale(0.25)); //Check to see if the predator is outside the// spherical boundary that defines the playing // field.if(predator.getDisplacementVector(origin). getLength()>0.5*osiHeight){ //Shift out of attack mode and start circling// the prey fish again. attack = false;attackButton.setEnabled(true); }//end if}//end else

The predator is in attack mode

The predator is in attack mode at the beginning of Listing 27 , and will remain in attack mode using the same displacement vector to control its speedand direction until it leaves the spherical playing field. When it leaves the playing field, the value of the attack variable will be set to false , causing the predator to revert to non-attack mode.

As the predator encounters prey fish along its trip toward the edge of the playing field, the code in Listing 21 (explained earlier) will determine whether those prey fish escape or get eaten by the predator.

Control of speed and direction

The displacement vector that controls the speed and direction of the predator ( predatorVec in Listing 27 ) is created in the actionPerformed method in Listing 3 in response to a click on the Attack button.

This vector is constructed to point to the most recently computed geometric center of the cluster of prey fish. Note however, that the vector may no longerpoint to the exact center of the cluster because the exact center of the cluster may have changed since it was last computed. In other words, the position of the geometric center of the prey-fish cluster changes as the predator attacks andcauses the prey fish to scatter. As programmed, the predator is unable to respond to such changes and continues to move in the same direction at the samespeed until it leaves the playing field.

In other words, even though the prey fish scatter, the predator is constrained to move in a straight line across the playing field once an attackhas begun.

An interesting upgrade:

A program upgrade to cause the predator to accommodate such changes in the geometric center would be an interesting exercise for the student.

Draw the predator on the off-screen image

Listing 28 sets the drawing color to BLUE and draws the predator's direction vector on the off-screen image.

Listing 28 . Draw the predator on the off-screen image.
g2D.setColor(Color.BLUE); //Enable the following statement to draw a circle// around the point that represents the predator. //predator.draw(g2D);//Draw the predator's vector.predatorVec.normalize().scale(15.0).draw( g2D,predator);g2D.setColor(Color.RED);//restore red color

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