<< 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)); |
Notification Switch
Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?