starsystem.pde 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. System sol;
  2. void setup(){
  3. size(640, 480);
  4. sol = new System(width/2, height/2);
  5. }
  6. void draw(){
  7. sol.update();
  8. background(0,0,0);
  9. sol.draw();
  10. }
  11. class Planet {
  12. //float r3,r,nu,v,hz,h,e;
  13. private float x;
  14. private float y;
  15. private float xVelocity;
  16. private float yVelocity;
  17. //private float energy;
  18. //private float period;
  19. private float starMass;
  20. private float r;
  21. private float r3;
  22. Planet(float inStarMass) {
  23. x = 100;
  24. y = 0;
  25. xVelocity = 0;
  26. yVelocity = 10;
  27. //v = 0;
  28. r = sqrt((x*x) + (y*y));
  29. r3 = 1/(r*r*r);
  30. //nu = 0;
  31. starMass = inStarMass;
  32. }
  33. void update() {
  34. //hz = (x * yVelocity) - (y * xVelocity);
  35. //energy = ((v*v)/2) - (starMass/r);
  36. //e = sqrt(1+ ((2*energy*hz*hz)/(starMass*starMass)));
  37. //v = sqrt((xVelocity*xVelocity) + (yVelocity*yVelocity));
  38. //period = (hz * hz) / starMass;
  39. //nu = acos(((period/r) - 1)/e);
  40. xVelocity -= starMass * x * r3;
  41. yVelocity -= starMass * y * r3;
  42. x += xVelocity;
  43. y += yVelocity;
  44. r = sqrt((x*x) + (y*y));
  45. r3 = 1/(r*r*r);
  46. }
  47. float getX() {
  48. return x;
  49. }
  50. float getY() {
  51. return y;
  52. }
  53. void draw() {
  54. stroke(0,255,0);
  55. noFill();
  56. ellipse(x,y,10,10);
  57. }
  58. }
  59. class Star {
  60. private float x;
  61. private float y;
  62. private float mass;
  63. private float diameter;
  64. public Star() {
  65. x = 0;
  66. y = 0;
  67. mass = 7000;
  68. diameter = 70;
  69. }
  70. public float getMass() {
  71. return mass;
  72. }
  73. public void update() {
  74. }
  75. public void draw() {
  76. stroke(255,255,0);
  77. noFill();
  78. ellipse(x,y,diameter,diameter);
  79. }
  80. }
  81. class System {
  82. private float x;
  83. private float y;
  84. private Star star;
  85. private Planet[] planets;
  86. public System(float inX, float inY) {
  87. x = inX;
  88. y = inY;
  89. star = new Star();
  90. planets = new Planet[1];
  91. for(int i = 0; i < planets.length; i++) {
  92. planets[i] = new Planet(star.getMass());
  93. }
  94. }
  95. public void update() {
  96. star.update();
  97. for(int i = 0; i < planets.length; i++) {
  98. planets[i].update();
  99. }
  100. }
  101. public void draw() {
  102. translate(x, y);
  103. translate(-(planets[0].getX() / 70), -(planets[0].getY() / 70));
  104. star.draw();
  105. for(int i = 0; i < planets.length; i++) {
  106. planets[i].draw();
  107. }
  108. }
  109. }