COS 126

N-body Simulation
Programming Assignment 9

Due: 11:59pm

Write a program that simulates the motion of N bodies, mutually affected by gravitational forces, in a two-dimensional space. The time evolution of such systems are usually too complex to compute analytically, but scientists have made significant research contributions (in plasma physics, semiconductors, fluid dynamics and astrophysics) by formulating and simulating appropriate computer models.

This program will be a Java applet similar to the one you did for the warmup. If you have not done the warmup, do it; otherwise you will find it very difficult to complete this assignment. This time, we supply the complete code for the applet class, or, and your task is to write a class Body that implement the methods show(), move(), computeForce(), and resetForce(), which are called from the applet. Each Body is characterized by several variables, including: its position x and y (the x and y coordinates), its velocity vx and vy (the x and y components of velocity), its mass m, and the image used to display it. The position and velocity are updated when we compute the effects of mutual gravitational interactions, as described below.

Updating the position and velocity. To update the position and velocity of a Body, you will need to know its acceleration. To determine this, you first have to perform a ``force'' calculation. This requires some elementary physics, which we review now. Don't worry if your physics is a bit rusty; all of the necessary formulas are included below.

class Body will be similar to class Ball in many ways, except that now each body has a force associated with it. In the warmup, the velocity of a ball never changes, since there is no external force acting upon it. For a body, after each time quantum, you need to update the position and velocity according to the gravitational interactions. So, each body should have its own position, velocity, force, mass, and image. There should also be a classwide variable double G = 6.67e-11 to represent the universal gravitational constant (in the appropriate units). Remember that it is good programming practice to make all data members within a class private: make it a true ADT by only allowing access through public methods.

class Nbody is similar to MovingBall, and two version are provided to you. is a no frills version; adds a graphical user interface. The class Nbody is the client program, and you should use exactly as is. The code below (included in uses the computeForce method, along with the principle of superposition, to compute the total force acting on a particular body.

        for (i = 0; i < N; i++)
        for (i = 0; i < N; i++)
            for (j = 0; j < N; j++)
                if (i != j)

Extra credit. Try adding other features, such as merging bodies when they collide. Or, make the simulation three-dimensional by doing calculations for x, y, and z coordinates, then using the z coordinate to vary the sizes of the planets. Design a planetary system with interesting behavior.

Copyright © 2000 Robert Sedgewick