Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ArrayList<Particle> particles = new ArrayList<Particle>();
- float g = 6.673 * pow(10,-5);
- float time;
- float delta;
- void setup() {
- size(800, 800);
- //particles.add(new Sun());
- for (int i = 0; i < 1000; i++) {
- particles.add(new Particle());
- }
- time = 0;
- delta = 0;
- noStroke();
- fill(255);
- }
- void draw() {
- background(51);
- delta = millis()-time;
- time = millis();
- for (int i = particles.size()-1; i >= 0; i--) {
- if (i < particles.size()) {
- particles.get(i).update();
- }
- }
- }
- class Particle {
- float mass;
- float size;
- PVector acc;
- PVector vel;
- PVector pos;
- Particle() {
- mass = random(9000000)+1000000;
- size = sqrt(mass/PI)/1000;
- acc = new PVector(0, 0);
- vel = new PVector(0, 0);
- pos = new PVector(random(width), random(height));
- }
- void update() {
- acc = new PVector(0, 0);
- for (int i = particles.size()-1; i >= 0; i--) {
- if (this != particles.get(i)) {
- Particle p2 = particles.get(i);
- float distance = (pow(p2.pos.x - pos.x, 2) + pow(p2.pos.y - pos.y, 2))*1000; //virtually expanded the distance by 1000
- float force = (g * mass * p2.mass)/distance;
- PVector normalized = new PVector(p2.pos.x - pos.x, p2.pos.y - pos.y).normalize();
- acc.add(normalized.mult(force/mass));
- checkCollision(particles.get(i));
- }
- }
- acc.mult(delta/100.0);
- vel.add(acc);
- pos.add(vel);
- ellipse(pos.x, pos.y, size, size);
- }
- void checkCollision(Particle p2) {
- if (dist(p2.pos.x, p2.pos.y, pos.x, pos.y)*2.0 < size + p2.size) {
- if (mass > p2.mass) {
- mass += p2.mass;
- 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));
- 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));
- size = sqrt(mass/PI)/1000;
- for (int i = particles.size()-1; i >= 0; i--) {
- if (particles.get(i) == p2) {
- particles.remove(i);
- break;
- }
- }
- } else {
- p2.mass += mass;
- p2.size = sqrt(p2.mass/PI)/1000;
- 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));
- 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));
- for (int i = particles.size()-1; i >= 0; i--) {
- if (particles.get(i) == this) {
- particles.remove(i);
- break;
- }
- }
- }
- }
- }
- }
- class Sun extends Particle {
- Sun() {
- mass = 1000000000;
- size = sqrt(mass/PI)/1000;
- pos = new PVector(width/2, height/2);
- }
- void update() {
- ellipse(pos.x, pos.y, size, size);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement