Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. package p;
  2.  
  3. import java.awt.Color;
  4. import java.util.ArrayList;
  5.  
  6. //2020-01-21
  7. public class PlanetsModel{
  8. private PlanetsView view;
  9.  
  10. private static final double G = 4 * Math.PI * Math.PI;
  11.  
  12. private static final double solarmass = 1;
  13.  
  14. private static final double earthmass = 1/332946.0;
  15.  
  16. private static final double jupitermass = 0.001*solarmass;
  17.  
  18. private static final double AU = 1;
  19.  
  20. private static final double earthStartSpeedY = 2*Math.PI;
  21.  
  22. private static final double jupiterStartSpeedY = (2*Math.PI) / (Math.sqrt(5.2));
  23.  
  24. private ArrayList<Planet> listOfPlanets = new ArrayList<Planet>();
  25.  
  26. private ArrayList<ArrayList<Planet>> linesForPlanets = new ArrayList<ArrayList<Planet>>();
  27.  
  28. public PlanetsModel(PlanetsView view) {
  29. this.view = view;
  30.  
  31. listOfPlanets.add(new Planet(0,0,0,0, solarmass, 4, Color.RED));
  32. listOfPlanets.add(new Planet(AU,0,0,earthStartSpeedY, earthmass, 2, Color.BLUE));
  33. listOfPlanets.add(new Planet(5.2*AU,0,0,jupiterStartSpeedY, jupitermass, 3, Color.ORANGE));
  34.  
  35. for(int i = 0; i < listOfPlanets.size(); i++) {
  36. linesForPlanets.add(new ArrayList<>());
  37. }
  38. }
  39.  
  40. // resets all the accelerations between the planets, needs to be done at each time frame,
  41. // since the acceleration depends on the positions, which constantly change
  42. private void resetAcc() {
  43. for(int i = 0; i < listOfPlanets.size(); i++) {
  44. listOfPlanets.get(i).setAx(0);
  45. listOfPlanets.get(i).setAy(0);;
  46. }
  47. }
  48.  
  49. // calculates and sets the acceleration between all planets in the list (listOfPlanets) at the specific point of time its called
  50. private void updateAcc() {
  51. for(int i = 0; i < listOfPlanets.size(); i++) {
  52. for(int j = 0; j < listOfPlanets.size(); j++) {
  53. if(listOfPlanets.get(i).getX() == listOfPlanets.get(j).getX() && listOfPlanets.get(i).getY() == listOfPlanets.get(j).getY())
  54. continue;
  55. ax(listOfPlanets.get(i), listOfPlanets.get(j));
  56. ay(listOfPlanets.get(i), listOfPlanets.get(j));
  57. }
  58. }
  59. }
  60.  
  61. // calculates the acceleration between two planets in the x direction
  62. private void ax(Planet planetToMove, Planet planetThatCauseMove) {
  63. double a = G * planetThatCauseMove.getMass() / (Math.pow(planetToMove.getX() - planetThatCauseMove.getX(), 2) + Math.pow(planetToMove.getY() - planetThatCauseMove.getY(), 2));
  64. planetToMove.addAx(a * (planetThatCauseMove.getX() - planetToMove.getX()) / Math.sqrt(Math.pow(planetToMove.getX() - planetThatCauseMove.getX(), 2) + Math.pow(planetToMove.getY() - planetThatCauseMove.getY(), 2)));
  65. }
  66.  
  67. // calculates the acceleration between two planets in the y direction
  68. private void ay(Planet planetToMove, Planet planetThatCauseMove) {
  69. double a = G * planetThatCauseMove.getMass() / (Math.pow(planetToMove.getX() - planetThatCauseMove.getX(), 2) + Math.pow(planetToMove.getY() - planetThatCauseMove.getY(), 2));
  70. planetToMove.addAy(a *(planetThatCauseMove.getY() - planetToMove.getY()) / Math.sqrt(Math.pow(planetToMove.getX() - planetThatCauseMove.getX(), 2) + Math.pow(planetToMove.getY() - planetThatCauseMove.getY(), 2)));
  71. //System.out.println(planetToMove.getAy());
  72. }
  73.  
  74. private void position(double dt) {
  75. for(int i = 0; i < listOfPlanets.size(); i++) {
  76. listOfPlanets.get(i).updatePosition(dt);
  77. }
  78. }
  79.  
  80. private void velocity(double dt) {
  81. for(int i = 0; i < listOfPlanets.size(); i++) {
  82. listOfPlanets.get(i).updateVelocity(listOfPlanets.get(i).getAx(), listOfPlanets.get(i).getAy(), dt);
  83. }
  84. }
  85.  
  86. private void addRiceGrain() {
  87. for(int i = 0; i < listOfPlanets.size(); i++) {
  88. linesForPlanets.get(i).add(new Planet(listOfPlanets.get(i).getX(),listOfPlanets.get(i).getY(),0,0, solarmass, 2, Color.WHITE));
  89. }
  90. }
  91.  
  92.  
  93.  
  94. private void drawPlanets() {
  95. for(Planet curInstance: listOfPlanets) {
  96. view.showPlanet(curInstance);
  97. }
  98.  
  99. for(ArrayList<Planet> list: linesForPlanets) {
  100. for(Planet p: list) {
  101. view.showPlanet(p);
  102. }
  103. }
  104.  
  105. }
  106.  
  107. // updates the acceleration, position and velocity in accordance to Verlets Algorithm
  108. private void updateAll (double dt){
  109. updateAcc();
  110. addRiceGrain();
  111. position(dt);
  112. velocity(dt);
  113. resetAcc();
  114. updateAcc();
  115. velocity(dt);
  116. resetAcc();
  117. }
  118.  
  119. public void update(double dt) {
  120. // Calculate the new positions and velocities of the planets
  121. updateAll(dt);
  122. view.clear();
  123. // Draw the planets
  124. drawPlanets();
  125. view.repaint();
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement