/* In this example each SimObject exerts force on every other SimObject; we've effectively combined the Attractor and SimObject from the Attractor_Bodies example. */ // set our maximum number of objects public static final int MAX_BODIES = 10; // declare our array of sim objects SimObject[] so = new SimObject[MAX_BODIES]; // variables for displaying our labels int showVectors = 1; PFont labelFont; void setup() { // setup our environment size(500, 500); background(15); smooth(); // load our font labelFont = loadFont("LucidaGrande-16.vlw"); textFont(labelFont, 16); // setup our objects reset(); } void draw() { // clear the screen background(15); /* To create a situation where each object exerts a force on every other object, we must loop through all of the objects twice; with the first loop we choose which object we are calculating hte force for, in the inner loop we are calculating the force of every object on the object we chose. */ for (int i = 0; i < so.length; i++) { for (int j = 0; j < so.length; j++) { // an object can't exert force on itself if (i != j) { // calculate the gravitational pull between the two Vector2D f = so[i].getGravityForce(so[j]); so[i].addForce(f); } } // draw our overlay drawOverlay(so[i].getVelocity(), so[i].getLocation(), 50, "Velocity"); drawOverlay(so[i].getAcceleration(), so[i].getLocation(), 1000, "Acceleration"); // let the object simulate so[i].simulate(); } } void reset() { // setup each of our objects for (int i = 0; i < so.length; i++) { Vector2D a = new Vector2D(0.0, 0.0); Vector2D v = new Vector2D(0.0, 0.0); Vector2D l = new Vector2D(random(width), random(height)); so[i] = new SimObject(l, v, a, random(5, 15), 0.2); so[i].setContained(true); } } void mousePressed() { reset(); } void keyPressed() { showVectors++; if (showVectors > 2) { showVectors = 0; } }