/* In this example we have a multiple objects moving around a single attractor object. Think of this like a planets orbiting the sun. */ // determine how many objects we want public static final int MAX_BODIES = 5; // Our simulation object SimObject[] so = new SimObject[MAX_BODIES]; // Our object exerting gravitational pull Attractor att; // Flag for showing the force vectors int showVectors = 1; // Font for labeling vectors PFont labelFont; void setup() { size(400, 400); background(15); // setup all of our sim objects for (int i = 0; i < MAX_BODIES; i++) { Vector2D a = new Vector2D(0.0, 0.0); Vector2D v = new Vector2D(random(-1, 1), random(-1, 1)); Vector2D l = new Vector2D(random(width), random(height)); so[i] = new SimObject(l, v, a, random(5, 15)); so[i].setContained(true); } // setup the attractor att = new Attractor(new Vector2D(width / 2, height / 2), 20, 0.4); // load our font labelFont = loadFont("LucidaGrande-16.vlw"); textFont(labelFont, 16); } void draw() { background(15); // apply our forces to each object for (int i = 0; i < MAX_BODIES; i++) { // figure out the force exerted on our SimObject by the attractor Vector2D attForce = att.getGravityForce(so[i]); // add the forces to our object so[i].addForce(attForce); drawOverlay(so[i].getVelocity(), so[i].getLocation(), 50, "Velocity"); drawOverlay(so[i].getAcceleration(), so[i].getLocation(), 1000, "Acceleration"); // let the object run so[i].simulate(); } // let the attractor run att.simulate(); } void mousePressed() { att.clicked(mouseX, mouseY); } void mouseReleased() { att.stopDragging(); } void keyPressed() { showVectors++; if (showVectors == 3) { showVectors = 0; } }