class Circle { float xPos; float yPos; int diam = 30; // circle diameter float G = 30; // gravitational constant color theColor; Vector3D a ; Vector3D v ; Vector3D l ; Circle[] otherCircles; int myIndex; Circle(Circle[] theOthers, int _myIndex) { xPos = random(width); // random starting position yPos = random(height); l = new Vector3D(xPos,yPos); v = new Vector3D(0.0,0.0); a = new Vector3D(0.0,0.0); theColor = color(255,128); otherCircles = theOthers; myIndex = _myIndex; } void drawCircle() { moveCircle(); fill(theColor); ellipse(l.x, l.y, diam, diam); } void moveCircle() { Vector3D sum = new Vector3D(); for (int i = 0; i < otherCircles.length; i++) { float d = Vector3D.distance(l,otherCircles[i].l); // distance between circles if (myIndex != i) //if (myIndex != i && (abs(dist(otherCircles[i].l.x,otherCircles[i].l.y,l.x,l.y)) > 10)) //if (myIndex != i && (abs(dist(otherCircles[i].xPos,otherCircles[i].yPos,xPos,yPos)) > 10)) { // ------------------------------------------------------------------------- gravity functions //println("go"); Vector3D dir = Vector3D.sub(otherCircles[i].l,l); // get the direction dir.normalize(); d = constrain(d,20,1000); // if too close, drop to threshold value d = sq(d); dir.mult(1/d); dir.mult(G); sum.add(dir); // summate forces //a = (a + (Vector3D.sub(otherCircles[i].l,l)).normalize()/(sq(Vector3D.distance(l, otherCircles[i].l))))/otherCircles.length; //x_gravity = (x_gravity + (otherCircles[i].xPos - xPos)/(sq(dist(otherCircles[i].xPos,otherCircles[i].yPos,xPos,yPos))))/otherCircles.length; //y_gravity = (y_gravity + (otherCircles[i].yPos - yPos)/(sq(dist(otherCircles[i].xPos,otherCircles[i].yPos,xPos,yPos))))/otherCircles.length; } } //sum.div(otherCircles.length); // ----------------------------------------------------------------------------- bounce if (l.x >= width) { v.x = -abs(v.x*damp) ; } if (l.x <= 0) { v.x = abs(v.x*damp) ; } if (l.y >= height) { v.y = -abs(v.y*damp); } if (l.y <= 0) { v.y = abs(v.y*damp); } a = sum; // acceleration is sum of a.mult(attracted); // attract or repulse v.add(a); //v.limit(1.0); //println(v.magnitude()); l.add(v); /* xVel = xVel + 20*x_gravity*attracted; // change velocities yVel = yVel + 20*y_gravity*attracted; xPos = xPos + xVel; // change positions yPos = yPos + yVel; */ } }