For next semester: fix window size to 600 x 600 so there is no need for extra code to get height and width. Maybe use double buffering for smoother animation. Would multithreading be necessary with double buffering? Create new method computeForces() instead of doing this in moveall.

Goals

Checklist

  • Use the submit command: /u/cs126/bin/submit 8 Nbody.java Nbody.html

  • In precept we will discuss the physics of N-body simulation.

  • Do not attempt Assignment 8 before doing the warmup Bouncing Ball program.

  • readme
  • name, precept number
  • description of problems encountered and high level description of code
  • Nbody.java
  • You can start from Nbody.java
  • The html file is Nbody.html (When you click here, Netscape may try to run a nonexistent applet. Choose File Save-As to save the html file in your directory.)
  • submit Nbody.java with data from SSfive
  • use the method double Math.sqrt(double) to computer square roots
  • data
  • Here's SSfive
  • Here's BinaryStar
  • extra credit
  • Submit the data (in the format of SSfive) for your own fun and creative "solar system" with moons, planets, asteroids, etc. Please use the same value of G as in SSfive.
  • Your comet should look like a comet, with a tail and everything. It should be slow-moving and collide with one of the planets, not the sun.
  • executing
  • Use javac Nbody.java to generate the files Body.class and Nbody.class
  • Use appletviewer Nbody.html to test the applet. If it crashes the first time, try it a second time and it might work. (If you can explain this phenomenon, please send mail to cs126@cs.)
  • To view your finished applet from a Web browser, make sure the following files are in your public_html directory and are world readable: Nbody.html, Body.class, and Nbody.class . If you can't get it to work under Netscape (but it works fine using appletviewer), don't worry - it probably means your version of Java and Netscape are not in sync. To review your applet in Netscape, use Shift-Reload.
  • debugging
  • Use System.out.println instead of printf for debugging.
  • Adjust the dt parameter in the html file to speed up or slow down the universe.
  • For smoother graphics, it is recommended that you move the bodies mathematically several times (say 60), before redrawing them. The bottleneck operation is drawing to the screen. By performing more computations per screen update, you will get somewhat smoother orbits. (To reduce the flicker further, you would want to use double buffering, where you draw the image to a second page, and then swap this with the visible page. Feel free to consult the Java documentation and experiment.)
  • The planets should not spiral into the sun (despite warning in course packet). The planets may move around some before settling into their equilibrium orbits, and may in fact spiral away from the sun. Click here to see what it should look like (the parameters were tuned for a Pentium II running Netscape 4.5). Note that the tiny planet has a very eccentric orbit and is included to indicate that orbits can be quite elliptical.
  • 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 physics will be warped and the planets will quickly spiral into the sun.
  • Other Sources

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

  • I left my copy of Deitel and Deitel for Java in CS101 for your reference. Do not remove this book from the lab or the final will be really really hard. :)