Gravity and music

6.11 Gravity and music

Before we leave our Newton’s Lab scenario behind, we have one more thing to play with: adding music. Well, noise, in any case. 4

The idea is as follows: We add a number of Obstacles into our world. When obstacles are touched by our planets, they make a sound. Then we create a few planets or stars, let them fly around, and see what happens.

We will not discuss this implementation in detail. Instead, we leave you to study it yourself, and just point out some of the more interesting features. You can find an implementation of this idea in the book scenarios as Newtons-Lab-3.

Exercise 6.28 Open the scenario named Newtons-Lab-3 and run it. Have a look at the source code. Try to understand how it works.

Here is a summary of the most interesting changes we have made from the previous version to create this:

We have added a new class Obstacle . You can easily see objects of this class on screen. Obstacles have two images: the orange rectangle you see most of the time, and a lighter ver- sion of the rectangle to show when they are touched. This is used to create the “lighting up” effect. Obstacles are also associated with a sound file, just as the piano keys were in our piano scenario. In fact, we are reusing the sound files from the piano scenario here, so they do sound the same.

We have modified the Body class so that bodies bounce off the edges off the screen. This gives a better effect for this kind of scenario. We have also increased gravity a bit to get faster

4 The idea to add sound to a gravity project was inspired by Kepler’s Orrery (see https://keplers-orrery. dev.java.net/ or search for “Kepler’s Orrery” on YouTube).

98 Chapter 6 | ■ Interacting objects: Newton’s Lab

movement and modified the code so that bodies automatically slow down once they get too fast. Otherwise they might speed each other up more and more indefinitely.

Finally, we have added code in the Space class to create a fixed row of obstacles, and to create five random planets (random size, mass, and color).

The implementation of these three changes includes a few interesting snippets of code that are worth pointing out.

In the Obstacle class, we use a method named getOneIntersectingObject to check whether the obstacle is being hit by a planet. The code pattern is the following:

Object body = getOneIntersectingObject(Body.class); if (body != null) {

} Concept:

The getOneIntersectingObject method is defined in class Actor , and is available to all actors. It will return an actor that this one intersects with if there is such an actor, or null

The Greenfoot API contains methods

if no other actor intersects with this one. The following if statement that checks whether

for collision

body is null is therefore a check whether any other object intersected this one.

detection. These make it possible

This is an example of collision detection, and we will discuss more of it in the next

to detect when

chapter.

one actor touches another. (More

In the Space class, we have added two methods, createObstacles and randomBodies .

about this in the

The first creates the obstacles with their associated sound file names, quite similar to the

next chapter.)

initialization code in the piano example. The second uses a while loop to create a number of Body objects. The bodies are initialized with random values. The while loop counts down from a given number to 0, to create the right number of objects. It is worth studying as another example of a loop.

Exercise 6.29 Change the number of bodies that are created by default in this scenario.

Exercise 6.30 Play with the movement parameters to see whether you can create nicer movement for the planets. The parameters are: the value for GRAVITY; the acceleration value used when bouncing off an edge (currently 0.9); the speed threshold (currently 7) and acceleration (0.9) used in the applyForces method to slow down fast objects; and the initial mass used for the planets (in the Space class).

Exercise 6.31 Create a different arrangement of obstacles in your scenario. Exercise 6.32 Use different sounds (different sound files) for your obstacles.

6.12 Summary of programming techniques

Exercise 6.33 Use different images for your obstacles. Exercise 6.34 Make planets change color every time they bounce off the edge of the

universe. Exercise 6.35 Make planets change color every time they hit an obstacle.

Exercise 6.36 Make a different kind of obstacle that gets switched on and off by being hit. When on, it continuously blinks and produces a sound at fixed intervals.

Exercise 6.37 Add some keyboard control. For example, pressing the right arrow key could add a small force to the right to all Body objects.

Exercise 6.38 Allow adding more planets. A mouse click into the universe while it is running should create a new planet at that location.

There are countless other possible ways to make this scenario more interesting and nicer to look at. Invent some of your own and implement them!