COS 126
N-body Simulation |
Programming Assignment 9
Due: Wednesday, 11:59pm |
Write a program that simulates the motion of N bodies, mutually affected by
gravitational forces, in a two-dimensional space.
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,
Nbody.java or
NbodyDeluxe.java,
and your task is to write class Body
that implement the methods show, move,
computeForce, and resetForce,
which are called from the applet.
Each body is characterized by 6 variables: the x and y
position coordinates; initial values for vx and vy (the x and y
components of the velocity); the mass of the body; 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.
- (force calculation)
Newton's law of gravitation says that
the strength of the gravitational force between two bodies is given by
the product of their masses divided by the square of the distance
between them, scaled by a constant G.
To calculate the force acting on one body, you need to calculate
the gravitational force between this body and every other body in
the system. The principle of superposition says that
the net force acting on a body is the sum of the individual pairwise
forces. Note that this is a vector sum: the pull of one body
towards another acts on the line between them.
Since you are working with Cartesian coordinates in Java, it
is convenient to break up the gravitational force between two bodies
into its x and y components.
Adding these up gives the net force in the
x and y direction, which we denote by Fx and Fy.
-
(acceleration calculation)
Next, use Newton's second law F[ ] = ma[ ] to get the x and y
components of the acceleration of each body that is implied by these
forces over the given time quantum dt.
-
(putting it together)
The updated position and velocity can be derived using basic
calculus:
the change in position is given by p[ ] = v[ ] dt + a[ ]*dt*dt/2, and
the change in velocity is given by v[ ] = a[ ] dt.
The square
brackets are intended to indicate that these are all vector formulae:
for example p[ ] represents the x and y position coordinates, v[ ]
represents the x and y coordinates of the velocity, and so forth.
class Body will be similar to 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.
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.
-
The following constructor should initialize the appropriate quantities
and set the initial forces to zero.
Body(double x, double y, double vx, double vy, double mass, Image image, Applet applet)
-
Method void move(int dt) updates the position and velocity
of a body, using the current velocity and force for an interval of time
dt. It is similar to method move in Ball,
except that the force causes an acceleration. Also, in Ball
we used dt = 1.
-
Method void show() displays the graphical
image associated with the Body.
-
Method void resetForce() is used to reset the force acting
on a particular body to zero.
-
Method void computeForce(Body b) computes and updates the
force acting on a body that is exerted by a second body b,
using the physics of mutual gravatational interaction.
class Nbody is similar to MovingBall, and two
version are provided to you.
Nbody.java is a no frills version;
NbodyDeluxe.java
adds a graphical user interface.
The class Nbody is the client program,
and you should use exactly as is.
The code below (included in Nbody.java)
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++)
a[i].resetForce();
for(i = 0; i < N; i++)
for(j = 0; j < N; j++)
if (i == j) continue;
else a[i].computeForce(a[j]);