// Mutual Attraction // Daniel Shiffman // adapted for use with Max/MSP by Adam Parrish // Mouse click resets all bodies to random locations // Key press turns on and off vector display // Created 2 May 2005 import noc.*; //import maxlink.*; int MAX = 3; Thing[] t = new Thing[MAX]; Thing mthing; long lastReset = 0; long resetWait = 20000; //MaxLink lnk; void setup() { size(640,480); smooth(); frameRate(30); colorMode(RGB,255,255,255,100); reset(); // lnk = new MaxLink(this, "attractmax"); } void draw() { background(0); for (int i = 0; i < t.length; i++) { // For every Thing t[i] for (int j = 0; j < t.length; j++) { // For every Thing t[j] if (i != j) { // Make sure we are not calculating gravtional pull on oneself Vector3D f = t[i].calcGravForce(t[j]); // Use the function we wrote above t[i].add_force(f); // Add the force to the object to affect its acceleration } } t[i].go(); // Implement the rest of the object's functionality // send values to max /* float cx = constrain(100+t[i].loc.x, 0, 1000); float cy = constrain(100+t[i].loc.y, 0, 1000); lnk.output(i*2, cx); lnk.output((i*2)+1, cy); */ } if (lastReset + resetWait < millis()) { reset(); lastReset = millis(); } } void reset() { // Some random bodies for (int i = 0; i < t.length; i++) { Vector3D ac = new Vector3D(0.0,0.0); Vector3D ve = new Vector3D(random(-1, 1),random(-1, 1)); Vector3D lo = new Vector3D(random(40+(width-40)),random(40+(height-40))); t[i] = new Thing(ac,ve,lo,random(1,100)); } } void mousePressed() { reset(); lastReset = millis(); }