Applying gravity
6.9 Applying gravity
In Code 6.6, we have solved the task of accessing each object in space, but we have deferred the task of actually applying the gravitational force. The method applyGravity (another example of a private method) still needs to be written.
This is now a little easier than before, though, since this method now only needs to deal with two objects at a time: the current object, and one other object specified in its parameter. We now want to apply the gravitational force from the other object to this one. This is where Newton’s Law comes into play.
Newton’s formula for gravitation looks like this:
mass1 * mass2
force =
distance 2
In other words, to calculate the force we need to apply to the current object, we need to multiply the mass of this object with the mass of the other object, and then divide by the square of the distance between the two objects. Finally, the value gets multiplied by the constant G—the Gravitational Constant. (You may remember that we have already defined a constant for this value in our class, named GRAVITY.)
94 | Chapter 6 ■ Interacting objects: Newton’s Lab
If you are very confident or adventurous, you may like to try to implement the applyGravity method yourself. You need to create a vector in the direction from the current body to the other body, with a length specified by this formula. For the rest of us, we now look at the finished implementation of that method (Code 6.7).
Code 6.7 Calculating and
/** applying gravity from
* Apply the gravity force of a given body to this one. another body
*/ private void applyGravity(Body other) {
double dx = other.getExactX() - this .getExactX(); double dy = other.getExactY() - this .getExactY(); Vector force = new Vector (dx, dy); double distance = Math.sqrt (dx * dx + dy * dy); double strength = GRAVITY * this .mass * other.mass /
(distance * distance); double acceleration = strength / this .mass; force.setLength (acceleration); addForce (force);
This method is not quite as complicated as it looks. First, we calculate the distances between our object and the other object in the x and y coordinates (dx and dy). Then we create a new vector using these values. This vector now has the right direction, but not the correct length.
Next, we calculate the distance between the two objects using the Pythagoras theorem (a 2 + b 2 = c 2 in right-angled triangles, see Figure 6.7).
Figure 6.7 other object The distance in
a 2 +b 2 =c 2
relation to dx and dy
distance
dy
a dx this object
This tells us that the distance is the square root of dx squared plus dy squared. In our code (Code 6.7), we use a method called sqrt from the Math class to calculate the square root. ( Math is a class in java.lang, and thus automatically imported.)
6.10 Trying it out
Exercise 6.21 Look up the class Math in the Java documentation. How many parame- ters does the sqrt method have? What types are they? What type does this method return?
Exercise 6.22 In the Math class, find the method that can be used to find the maximum of two integers. What is it called?
The next line in our code calculates the strength of the force by using Newton’s formula of gravitation given above.
The final thing to do is to calculate the acceleration, since the actual movement change to our object is not only determined by the force of gravity, but also the mass of our object: The heav- ier the object, the slower it will accelerate. Acceleration is computed using the following formula:
force acceleration = mass
Once we have calculated the acceleration, we can set our force vector to the correct length and add this vector to the movement of our body. Doing this is easy, using the addForce method that is provided by the SmoothMover class.
Exercise 6.23 Map the variables in Code 6.7 to Newton’s formula, the Pythagoras theorem, and the acceleration formula given above. Which variable corresponds to which part of which formula?
With this, our task is completed. (An implementation of the code described so far is also available in the book scenarios as Newtons-Lab-2.)
This task clearly involved more background knowledge in math and physics than the others we have seen. If this is not your favorite area, don’t worry, we shall return to less mathematical projects shortly. Remember: programming can do anything you like. You can make it very mathematical, but you can also make it very creative and artistic.