Advertisement
aknipfing

Untitled

Oct 27th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. package checkPoint;
  2.  
  3. import java.util.ArrayList;//imports arraylist class
  4. import edu.princeton.cs.introcs.StdDraw;
  5.  
  6. public class System {//helped by Andre Soto and TA Julia
  7. public double G = 6.67384e-11;//Universal Gravitational Constant
  8. //creates an array list for our objects to be put into
  9. ArrayList<Body> BODYLIST;
  10. //initializes array
  11. public void system(ArrayList<Body> bodyList) {//calculates acceleration for each body
  12. BODYLIST = bodyList;
  13. }
  14. public double[] computeAcceleration(Body planet) {
  15. double ax = 0;//x acceleration
  16. double ay = 0;//y acceleration
  17. double changeY = 0;//change in y
  18. double changeX = 0;//change in x
  19. double a; //acceleration
  20. double r = 0; //radius
  21.  
  22. for (int i = 0; i < BODYLIST.size(); i++) {//for loop to give us an arraylist making our bodies in the system
  23. if (planet.equals(BODYLIST.get(i))) {
  24. continue;
  25. }
  26. changeX = BODYLIST.get(i).X - planet.X;//changes x
  27. changeY = BODYLIST.get(i).Y - planet.Y;//changes y
  28. r = Math.sqrt(changeX*changeX + changeY*changeY);//makes the radius
  29. a = G * BODYLIST.get(i).MASS / (r*r);//makes the acceleration
  30. ax += a * changeX / r;
  31. ay += a * changeY / r;
  32. }
  33. double[] acceleration = new double[2];//uses an array to make the acceleration
  34. acceleration[0] = ax;//x acceleration
  35. acceleration[1] = ay;//y acceleration
  36.  
  37.  
  38. return acceleration;//gives us acceleration
  39. }
  40. public void update(double timestep) {//updates things
  41. double[] as;
  42. for (int i = 0; i < BODYLIST.size(); i++) {
  43. as = computeAcceleration(BODYLIST.get(i)); //updates acceleration
  44. BODYLIST.get(i).updateVelocity(as[0], as[1], timestep);//updates velocity
  45. BODYLIST.get(i).updatePosition(timestep);//updates position
  46. }
  47. }
  48.  
  49. public void draw(double cx, double cy, double pixelsPerMeter) {//drawsthe body
  50. StdDraw.clear(StdDraw.BLACK);
  51. for (int i = 0; i < BODYLIST.size(); i++) {
  52. BODYLIST.get(i).draw(cx, cy);
  53. }
  54. StdDraw.show(20);//updates every 20 milliseconds
  55. }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement