Advertisement
MrMusAddict

Gravitational Pull

Feb 15th, 2018
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.93 KB | None | 0 0
  1. ArrayList<Particle> particles = new ArrayList<Particle>();
  2. float g = 6.673 * pow(10,-5);
  3. float time;
  4. float delta;
  5.  
  6. void setup() {
  7.   size(800, 800);
  8.  
  9.   //particles.add(new Sun());
  10.   for (int i = 0; i < 1000; i++) {
  11.     particles.add(new Particle());
  12.   }
  13.  
  14.   time = 0;
  15.   delta = 0;
  16.  
  17.   noStroke();
  18.   fill(255);
  19. }
  20.  
  21. void draw() {
  22.   background(51);
  23.   delta = millis()-time;
  24.   time = millis();
  25.   for (int i = particles.size()-1; i >= 0; i--) {
  26.     if (i < particles.size()) {
  27.       particles.get(i).update();
  28.     }
  29.   }
  30. }
  31.  
  32. class Particle {
  33.  
  34.   float mass;
  35.   float size;
  36.  
  37.   PVector acc;
  38.   PVector vel;
  39.   PVector pos;
  40.  
  41.   Particle() {
  42.     mass = random(9000000)+1000000;
  43.     size = sqrt(mass/PI)/1000;
  44.     acc = new PVector(0, 0);
  45.     vel = new PVector(0, 0);
  46.     pos = new PVector(random(width), random(height));
  47.   }
  48.  
  49.   void update() {
  50.     acc = new PVector(0, 0);
  51.  
  52.     for (int i = particles.size()-1; i >= 0; i--) {
  53.       if (this != particles.get(i)) {
  54.         Particle p2 = particles.get(i);
  55.         float distance = (pow(p2.pos.x - pos.x, 2) + pow(p2.pos.y - pos.y, 2))*1000; //virtually expanded the distance by 1000
  56.         float force = (g * mass * p2.mass)/distance;
  57.         PVector normalized = new PVector(p2.pos.x - pos.x, p2.pos.y - pos.y).normalize();
  58.         acc.add(normalized.mult(force/mass));
  59.         checkCollision(particles.get(i));
  60.       }
  61.     }
  62.     acc.mult(delta/100.0);
  63.     vel.add(acc);
  64.     pos.add(vel);
  65.    
  66.     ellipse(pos.x, pos.y, size, size);
  67.   }
  68.  
  69.   void checkCollision(Particle p2) {
  70.     if (dist(p2.pos.x, p2.pos.y, pos.x, pos.y)*2.0 < size + p2.size) {
  71.       if (mass > p2.mass) {
  72.         mass += p2.mass;
  73.         acc = new PVector(((p2.acc.x * p2.mass) + (acc.x * mass))/(p2.mass + mass),((p2.acc.y * p2.mass) + (acc.y * mass))/(p2.mass + mass));
  74.         vel = new PVector(((p2.vel.x * p2.mass) + (vel.x * mass))/(p2.mass + mass),((p2.vel.y * p2.mass) + (vel.y * mass))/(p2.mass + mass));
  75.         size = sqrt(mass/PI)/1000;
  76.         for (int i = particles.size()-1; i >= 0; i--) {
  77.           if (particles.get(i) == p2) {
  78.             particles.remove(i);
  79.             break;
  80.           }
  81.         }
  82.       } else {
  83.         p2.mass += mass;
  84.         p2.size = sqrt(p2.mass/PI)/1000;
  85.         p2.acc = new PVector(((p2.acc.x * p2.mass) + (acc.x * mass))/(p2.mass + mass),((p2.acc.y * p2.mass) + (acc.y * mass))/(p2.mass + mass));
  86.         p2.vel = new PVector(((p2.vel.x * p2.mass) + (vel.x * mass))/(p2.mass + mass),((p2.vel.y * p2.mass) + (vel.y * mass))/(p2.mass + mass));
  87.         for (int i = particles.size()-1; i >= 0; i--) {
  88.           if (particles.get(i) == this) {
  89.             particles.remove(i);
  90.             break;
  91.           }
  92.         }
  93.       }
  94.     }
  95.   }
  96. }
  97.  
  98. class Sun extends Particle {
  99.  
  100.   Sun() {
  101.     mass = 1000000000;
  102.     size = sqrt(mass/PI)/1000;
  103.     pos = new PVector(width/2, height/2);
  104.   }
  105.  
  106.   void update() {
  107.     ellipse(pos.x, pos.y, size, size);
  108.   }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement