// body3.java three body gravitational attraction // F = G m1 m2 / d^2 F, A, V, S have x,y,z components // F = m2 v^2 / d d^2 = (x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2 // A = F / m1 acceleration along force components // V = V + A dt velocity update at delta time dt // S = S + V dt position update at delta time dt import java.*; import java.awt.*; import java.awt.event.*; public class body3 extends Frame { // define number of masses NM, add initial values int NM=3; // subscript is mass index xyz S[] = new xyz[NM]; // initialized positions xyz V[] = new xyz[NM]; // initialized velocities to orbit xyz A[] = new xyz[NM]; // computed xyz F[] = new xyz[NM]; // computed double R[] = {0.5, 0.0625, 0.03125}; // radius of mass double M[] = {1.0, 0.125, 0.00625}; // normalized mass double G = 2.0; // normalized gravitational constant double dt = 0.1; // normalized time step boolean debug = false; // mouse and display variables int xm, ym, bm; // mouse position int xc; // center location int yc; int xp; // last mouse location during drag int yp; boolean tracking = false; // tracking mouse motion boolean loose = false; // loose to move double x = 0.0; // integrated position double vx = 0.0; // integrated velocity boolean hspeed = false; // yellow buttons boolean dspeed = false; boolean reset = false; int height = 600; int width = 600; double xmin = -5.0; double xmax = 5.0; double ymin = -5.0; double ymax = 5.0; double xs, ys; public body3() { System.out.println("body3.java running, NM="+NM); init(); xc = width/2; yc = height/2; xs = (double)width/(xmax-xmin); ys = (double)height/(ymax-ymin); System.out.println("xmin="+xmin+",xmax="+xmax+ ", ymin="+ymin+", ymax="+ymax); System.out.println("xs="+xs+",ys="+ys); setTitle("body3"); setSize(width,height); setBackground(Color.black); setForeground(Color.white); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setVisible(true); this.addMouseListener (new mousePressHandler()); this.addMouseListener (new mouseReleaseHandler()); this.addMouseMotionListener (new mouseMotionHandler()); new MyThread("physics").start(); } public class xyz { public double x = 0.0; public double y = 0.0; public double z = 0.0; xyz() { } xyz(double a, double b, double c) { x=a; y=b; z=c; } } void physics() { int i, j; double fg, fd; double Dijx, Dijy, Dijz; double d2ij; //Idle callback, compute force, acceleration, update velocity and position for(i=0; ix && xmy && ym