public class System{ ArrayList bodies; int SUN = 0; int PLANET = 1; int ASTEROID = 2; boolean twoSuns; float perlinIncrement = 0.0; float perlinIncrement2 = 100.0; System(boolean _twoSuns){ twoSuns = _twoSuns; bodies = new ArrayList(); setSuns(); } void setSuns(){ if (twoSuns){ Vector3D sunLocation1 = new Vector3D(width/2 - 100, height/2); Vector3D sunLocation2 = new Vector3D(width/2 - 40, height/2); Body sun1 = new Body(sunLocation1, 50, 0.2, color(255,255,0), SUN); Body sun2 = new Body(sunLocation2, 50, 0.2, color(255,255,0), SUN); sun1.initializeVelocity(sunLocation2, 100); sun1.velocity.mult(0.5); sun2.velocity = sun1.velocity.copy(); sun2.velocity.mult(-1); bodies.add(sun1); bodies.add(sun2); } else{ Vector3D sunLocation = new Vector3D(width/2 - 70, height/2); Body sun = new Body(sunLocation, 100, 0.2, color(255,255,0), SUN); bodies.add(sun); } } void toggleSuns(){ for (int i = bodies.size() - 1; i >= 0; i--){ Body thisBody = (Body) bodies.get(i); if (thisBody.type == SUN){ bodies.remove(i); } } twoSuns = !twoSuns; setSuns(); } void compare(){ for (int i = bodies.size() - 1; i >= 0; i--){ Body thisBody = (Body) bodies.get(i); thisBody.update(); for (int j = bodies.size() - 1; j >= 0; j--){ if (j != i){ Body otherBody = (Body) bodies.get(j); thisBody.attract(otherBody.getLocation(), otherBody.mass); if (sq(thisBody.location.x - otherBody.location.x) + sq(thisBody.location.y - otherBody.location.y) < sq(thisBody.getRadius() + otherBody.getRadius())){ Body collisionBody; Body collisionBody2; if (thisBody.mass > otherBody.mass){ collisionBody = (Body) bodies.get(i); collisionBody2 = (Body) bodies.get(j); } else{ collisionBody = (Body) bodies.get(j); collisionBody2 = (Body) bodies.get(i); } float newRed = (red(collisionBody.bodyColor)* collisionBody.mass + red(collisionBody2.bodyColor)* collisionBody2.mass)/(collisionBody.mass + collisionBody2.mass); float newGreen = (green(collisionBody.bodyColor) * collisionBody.mass + green(collisionBody2.bodyColor) * collisionBody2.mass)/(collisionBody.mass + collisionBody2.mass); float newBlue = (blue(collisionBody.bodyColor) * collisionBody.mass + blue(collisionBody2.bodyColor) * collisionBody2.mass)/(collisionBody.mass + collisionBody2.mass); collisionBody.bodyColor = color(newRed, newGreen, newBlue); Vector3D collisionRatio = collisionBody.velocity.copy(); collisionRatio.mult(collisionBody.mass); Vector3D collision2Ratio = collisionBody2.velocity.copy(); collision2Ratio.mult(collisionBody2.mass); collisionBody.velocity = Vector3D.add(collisionRatio, collision2Ratio); collisionBody.velocity.div(collisionBody.mass + collisionBody2.mass); collisionBody.mass = collisionBody.mass + collisionBody2.mass; collisionBody2.mass = 0; collisionBody2.setDead(true); } } } } for (int i = bodies.size() - 1; i >= 0; i--){ Body thisBody = (Body) bodies.get(i); if( thisBody.checkDeath()){ bodies.remove(i); // println(i); } } } void display(){ for (int i = 0; i < bodies.size(); i++){ Body thisBody = (Body) bodies.get(i); thisBody.display(); } } void addPlanet(int _x, int _y){ Vector3D planetLocation = new Vector3D(_x, _y); Body planet = new Body(planetLocation, random(.001,0.01), 0.02, color(random(0,255),random(127,255),random(127,255)), PLANET); Vector3D sunLocations = new Vector3D(0,0); int sunIndex = 0; float sunMass = 0; for (int i = 0; i < bodies.size(); i++){ Body maybeSun = (Body) bodies.get(i); if (maybeSun.type == SUN){ Vector3D addition = maybeSun.location.copy(); addition.mult(maybeSun.mass); sunLocations = Vector3D.add(addition, sunLocations); sunIndex++; sunMass += maybeSun.mass; } } sunLocations.div(sunMass); planet.initializeVelocity(sunLocations, sunMass); println(sunLocations.x + "\t" + sunLocations.y + "\t" + sunMass); bodies.add(planet); } void addAsteroids(){ // println("press"); int stormSize = (int) random(1,20); //Vector3D stormBeginning = new Vector3D((float) (width - 140)*int(random(2)), random(- 200,height + 200)); // float stormEnding = random(0,height); // println(stormBeginning.x); // int side = floor(4*noise(perlinIncrement)); perlinIncrement += 0.1; perlinIncrement2 += 0.1; Vector3D stormBeginning; Vector3D stormEnding; if (side == 0){ stormBeginning = new Vector3D(0.0,height * noise(perlinIncrement)); stormEnding = new Vector3D(width,height * noise(perlinIncrement2)); } else if (side == 1){ stormBeginning = new Vector3D(width * noise(perlinIncrement), 0.0); stormEnding = new Vector3D(width * noise(perlinIncrement2),height); } else if (side == 2){ stormBeginning = new Vector3D(width,height * noise(perlinIncrement)); stormEnding = new Vector3D(0.0,height*noise(perlinIncrement2)); } else{ stormBeginning = new Vector3D(width * noise(perlinIncrement),height); stormEnding = new Vector3D(width * noise(perlinIncrement2),0.0); } Vector3D direction = Vector3D.sub(stormEnding, stormBeginning); float startingVelocity = random(0.1,2); for (int i = 0; i < stormSize; i++){ Vector3D fluctuation = new Vector3D(random(-10,10), random(-10,10)); fluctuation = Vector3D.add(stormBeginning, fluctuation); Body asteroid = new Body(fluctuation, random(.000001,0.00001), 0.01, color(255,random(127,255),0), ASTEROID); asteroid.velocity = Vector3D.sub(direction,stormBeginning); asteroid.velocity.normalize(); asteroid.velocity.mult(startingVelocity); bodies.add(asteroid); // println(i); } } void reset(){ background(0,0,10); // println("reset"); for (int i = bodies.size() - 1; i >= 0; i--){ bodies.remove(i); // println(i); } } void nebula(){ Vector3D centerPoint = new Vector3D(width/2 - 80, height/2); int initialRowLength = 1; int maxRowLength = 10; int cumRowLength = initialRowLength; int rowLength = initialRowLength; int stormSize = (maxRowLength * (maxRowLength - 1) * 3) + 1; int row = 0; int columnPosition = 0; float separation = 15; for (int i = 0; i < stormSize; i++){ if (row < (maxRowLength - 1) * 3){ if (i == cumRowLength){ row++; if (rowLength < maxRowLength - 1){ rowLength = row + initialRowLength; } else{ if (row%2 == 0){ rowLength = maxRowLength - 1; } else{ rowLength = maxRowLength; } } cumRowLength = cumRowLength + rowLength; columnPosition = 0; } } else{ if (i == cumRowLength){ row++; rowLength--; cumRowLength = cumRowLength + rowLength; columnPosition = 0; } } float rowY = row * (separation/2); float columnX = columnPosition * sqrt(sq(2*separation) - sq(separation)) + sqrt(sq(2*separation) - sq(separation))*(maxRowLength - rowLength)/2; columnPosition++; float xOffset = (width - 90 - maxRowLength * sqrt(sq(2*separation) - sq(separation)))/2; float yOffset = (height - (2*(maxRowLength-1)) * separation)/2; Vector3D perlinLocation = new Vector3D(columnX + xOffset, rowY + yOffset); Vector3D perlinVelocity =new Vector3D(noise(i*0.001), noise(i*0.001 + 100)); //Body nebulaparticle = new Body(perlinLocation, random(0.05,0.5), 0.2, color(255,random(127,255),0), SUN); Body nebulaparticle = new Body(perlinLocation, 1, 0.2, color(255,random(127,255),0), SUN); Vector3D distance = Vector3D.sub(centerPoint, perlinLocation); float theta = atan2(distance.x, distance.y); theta += PI/2; //float velocitymagnitude = 10/sq(distance.magnitude()); // float velocitymagnitude = 0; float velocitymagnitude = 0.5; nebulaparticle.velocity.setX(velocitymagnitude * sin(theta)); nebulaparticle.velocity.setY(velocitymagnitude * cos(theta)); bodies.add(nebulaparticle); } } void drag(){ for (int i = 0; i < bodies.size(); i++){ Body thisPlanet = (Body) bodies.get(i); thisPlanet.location.x += mouseX - pmouseX; thisPlanet.location.y += mouseY - pmouseY; } } }