Interacting with objects in range
7.9 Interacting with objects in range
We now have a proton wave that we can release at a press of a button. The remaining problem is: This proton wave does not actually do anything to the asteroids.
We now wish to add code that causes damage to the asteroids when they get hit by the proton wave.
Exercise 7.60 Prepare for this new functionality: In class ProtonWave, add a method stub for a method called checkCollision. The method has no parameters and does not return a value. Call this method from your act method.
Exercise 7.61 The purpose of this new method is to check whether the wave touches an asteroid, and cause damage to it if it does. Write the method comment.
This time we do not want to use the getIntersectingObjects method, since the invisible image areas at the corners of the proton wave image (included in the bounding box, but not part of the blue-ish circle) are fairly large, and asteroids would be destroyed long before the wave seems to reach them.
Instead, we will use another collision detection method, called getObjectsInRange . The getObjectsInRange method returns a list of all objects within a given radius of the call-
ing object (see Figure 7.4). Its signature is
List getObjectsInRange(int radius, Class cls)
When called, we can specify the class of objects we are interested in (as before), and we also specify a radius (in cells). The method will then return a list of all objects of the requested class that are found within this radius around the calling object.
To determine which objects are within the range, the center points of objects are used. For example, an asteroid would be within range 20 of a rocket if the distance of its center point to
Figure 7.4 The range around an actor with a given radius
| ■ Chapter 7 Collision detection: Asteroids
the center point of the rocket is less than 20 cell widths. The size of the image is not relevant for this method.
Using this method, we can implement our checkCollision method. Our proton wave will have images of increasing size. At each act cycle, we can use the size of
the current image to determine the range of our collision check. We can find out our current image size using the following method calls:
getImage().getWidth()
We can then use half of this size as our range (since the range is specified as a radius, not a diameter).
Exercise 7.62 In checkCollision, declare a local variable named range and assign half the current image width to it.
Exercise 7.63 Add a call to getObjectsInRange that returns all asteroids within the calculated range. Assign the result to a variable of type List<Asteroid>. Remember that you also have to add an import statement for the List type.
These exercises give us a list of all asteroids in the range of the proton wave. We now want to do some damage to each asteroid in range.
The Asteroid class has a method called hit that we can use to do this. This method is already being used to do damage to the asteroid when it is hit by a bullet, and we can use it again here.
We can use a for-each loop to iterate through all asteroids in the list we received from the getObjectsInRange call. (If you are unsure about writing for-each loops, look back to Section 6.8.)
Exercise 7.64 Find the hit method in the Asteroid class. What are its parameters? What does it return?
Exercise 7.65 The ProtonWave class has a constant defined toward the top, called DAMAGE, that specifies how much damage it should cause. Find the declaration of this constant. What is its value?
Exercise 7.66 In method checkCollision, write a for-each loop that iterates over the asteroid list retrieved from the getObjectsInRange call. In the loop body, call hit on each asteroid using the DAMAGE constant for the amount of damage caused.
Once you have completed these exercise, test. If all went well, you should now have a playable version of this game that lets you shoot at asteroids and also release proton waves to destroy
7.10 Further development
many asteroids in one go. You will notice that you should make the reload time for the proton wave quite long, since the game gets too easy if you can use the wave too often.
This version of the game, including all the changes made in the last few sections, is available in the book projects as asteroids-3. You can use this version to compare it to your own scenario, or to look up solutions if you get stuck in one of the exercises.