<< Chapter < Page Chapter >> Page >

Although somewhat tedious, the code in Listing 24 is straightforward and shouldn't require an explanation beyond the embedded comments.

Draw the prey fish on the off-screen image

Depending on the states of the two check boxes in Figure 1 , Listing 25 draws the GM01.Point3D objects that represent the prey fish or GM01.Vector3D objects that represent the direction vectors for the prey fish, or both, on the off-screen image. (Note that only the vectors were drawn on the off-screen image in Figure 1 .)

Listing 25 . Draw the prey fish on the off-screen image.
g2D.setColor(Color.RED); for(int cnt = 0;cnt<preyObjects.size();cnt++){ tempPrey = preyObjects.get(cnt);if(drawPoints){ tempPrey.draw(g2D);//draw circle around point}//end ifif(drawVectors){ //Draw the vector with its tail at the point.displayVectors.get(cnt).draw(g2D,tempPrey); }//end if}//end for loop

There is nothing new in Listing 25 , so I won't bore you with a detailed explanation of the code.

It's time to deal with the predator

So far, we have been dealing exclusively with code that controls the behavior of the prey fish. The time has come to deal with the code that controls thebehavior of the predator.

Cause the predator to slowly circle the cluster of prey fish

When the player clicks the Attack button, a boolean variable named attack is set to true , causing the predator to enter attack mode during the next iteration of the animation loop. However, when the value ofthis variable is false , the predator is not in attack mode and its behavior is to swim slowly around the cluster of prey fish encouraging them tobunch up into a smaller and tighter cluster.

The code that accomplishes this circling behavior is shown in Listing 26 .

Listing 26 . Cause the predator to slowly circle the cluster of prey fish.
//When the predator is not in attack mode, cause // it to slowly circle the cluster of prey// objects. if(!attack){//Get a displacement vector pointing from the // predator to the preyCenter.predatorVec = predator.getDisplacementVector(preyCenter);//Create a vector that is rotated relative to// predatorVec. Note how each component in // this new vector is set equal to a different// component in predatorVec tempVectorB = new GM01.Vector3D(new GM01.ColMatrix3D( predatorVec.getData(1),predatorVec.getData(2), predatorVec.getData(0))).scale(0.15);//Scale predatorVec and add the two vectors.// Then move the predator according to the sum // of the vectors.//Moving the prey object in the direction of // the sum of these two vectors produces a// motion that causes the predator to // spiral toward the preyCenter instead of// simply moving in a straight line toward the // preyCenter. The scale factors control the// relative motion between the two directions, // and are fairly sensitive.predatorVec = predatorVec.scale(0.095).add(tempVectorB);predator = predator.addVectorToPoint(predatorVec.scale(1.0));

Questions & Answers

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 genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
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