Goals

Part 0:   preparation

  • Do the Bouncing Ball warmup first. This assignment will not be very difficult if you do the warmup first.

  • Download the latest version of the N-body assignment from the course Web page. The course packet contains a preliminary version.

  • Copy the following files from /u/cs126/files/Nbody/ into your public_html directory:
    Nbody.java   Nbody.html   NbodyDeluxe.java   NbodyDeluxe.html
    earth.gif    sun.gif      venus.gif          mars.gif
    mercury.gif  2001.au      starfield.jpg 
    
    You can do this with the following command:
    cp /u/cs126/files/Nbody/* ~/public_html
    

  • Your job is to write the implementation for class Body, an ADT that encapsulates information about a planetary body. The client programs Nbody.java and NbodyDeluxe.java use this ADT to create an applet that simulates planetary motion.

  • Part 1:   class Body members

  • The client program uses the following methods:

  • void move (int dt)

  • void show ( )

  • void resetForce ( )

  • void computeForce (Body b)
  • So, you will need to define these appropriately later on. For now, define these as methods (functions) that do nothing.

  • You also need to think about what data members should be in class Body. As in the Moving Ball warmup, each body should have a position, velocity, image, and applet associated with it. You may also find it convenient for each Body to have a mass and force associated with it. Since all the Body's share the same gravitational constant, you can define a shared classwide (static) variable G that can be shared among all the Body's.

  • Part 2:   move and show

    To get started, ignore gravity, and simply make the planets move in a straight line, as in the warmup.

  • For move(int dt) you can reuse much of the code from the warmup. The parameter dt is new; in the warmup it was assumed to be 1. Ignoring gravity, the new position should be the old position + dt times the velocity. Also, remove the "bounce" code.

  • For show() the code will be similar, but you will need to rescale the planetary coordinates to Java coordinates. First, understand how the rescaling is done in the show() method from the warmup. In the warmup, the ball coordinates stay between -0.5 and 0.5. The scaled x coordinate xpix must be an integer between 0 and applet.getSize.width(). The planetary coordinates are in a different range than the ball coordinates, so you will need a different formula to transform them into Java coordinates. (Hint: recall the Mandelbrot assignment.)

  • To accomplish the rescaling, you need to know the magnitude of the largest x or y coordinate. The easiest way is to hardwire in this value. A better approach (that will continue to work if you simulate other universes) is to declare a shared classwide variable
    private static double maxXY = 0.0;
    
    that keeps track of the maximum coordinate. You can update this quantity after each new Body is instantiated by adding a few lines of code to the Body constructor. You can use the method Math.abs() to take absolute values.

  • Part 3:   resetForce and computeForce

  • resetForce() should be straightforward to implement. If class Body has data members for force in the x and y directions, then you can simply reset these values to 0.0.

  • computeForce() requires Newton's law of gravitation. Make sure you get the +/- sign correct.

  • Each time computeForce() it should update the current force acting on the Body.

  • Use the method Math.sqrt to compute square roots. You should not need to use the sine or cosine functions.

  • Part 4:   move

  • Once you have Part 3 working correctly, write code for move() to account for gravity. This requires using Newton's second law and a calculus formula, both of which are described in the Assignment.

  • Make sure that you update the velocity and position, e.g., the new velocity will be the same as the old velocity if there is no force acting upon it. Also make sure that you update the velocity and position "simultaneously." That is, do not update the position using the updated velocity. If you do, the the planets will spiral into the sun.

  • If the oribts of your planets eventually expand, don't worry, this is to be expected. To get a more refined simulation, update the position using the formula:
    p[ ] = p[ ] + v[ ] dt + a[ ]*dt*dt
    
    Explanation: the original formula assumes the acceleration is constant over an interval from time t to t + dt, whereas the modified formula assumes it is constant from time t - dt/2 to t + dt/2.

  • General debugging hints

  • Use System.out.println() instead of printf() for debugging.

  • Adjust the time quantum parameter dt for a finer or courser approximation. Adjust the delay parameter pause to prevent hoarding system resources. For smoother graphics, you can change the parameter calcPerDisplay. Drawing to the screen is slow. So instead, the code updates the bodies several times before redrawing.

  • Compiling and executing
  • Compile the standard version with:
    javac Body.java Nbody.java
    
    or compile a deluxe version with extra bells and whistles using:
    javac Body.java NbodyDeluxe.java
    

  • View the corresponding applet with one of the following commands:
    appletviewer Nbody.html
    appletviewer NbodyDeluxe.html
    

  • To view in a web browser, make sure that all of the appropriate files are in your public_html directory and are world readable. Set your Web browser to the page http://www.princeton.edu/~login/Nbody.html. To make the appropriate files world readable, type:
    chmod 644 *.html *.class *.gif *.au *.jpg
    chmod 600 *.java
    

  • Submission and readme   (1 point)

  • Use the submit command:
    submit126 9 Body.java readme
    

  • The readme file should contain:

  • Name, precept number, high level description of code, description of problems encountered, and whatever help (if any) your received.

  • Extra credit

  • Creating your own "solar system" is an easy way to earn extra credit, although we expect some creativity if you choose this option. Some ideas include: simulating a binary star system, adding moons, asteroids, comets, etc. There is a possibility that we can display your solar systems on the Frist Campus Center display wall.

  • If you do the extra credit, be sure to submit all files necessary to compile and run it.

  • Enrichment Links
  • Check the Java section of the Frequently Asked Questions page for more info and useful links.

  • Here's some information about our solar system.

  • Here's some of the N-body physics.


  • Kevin Wayne