/** * LBi Balls * By: Anthony Tripaldi */ int numBalls = 200; int orbit = 160; int range = 300; int force = 3000; float half_w; float half_h; Ball[] balls = new Ball[numBalls]; void setup() { size(550,500,P3D); //frameRate(30); fill(255); noStroke(); //sphereDetail(15); setBalls(); } void draw() { background(255); fill(255,0,0); //lights(); //simple camera rotation half_w = width * 0.5; half_h = height * 0.5; translate( half_w, half_h, 0 ); //rotate( radians(frameCount * 0.35) ); for( int i = 0; i < numBalls; i++ ) { balls[i].draw(mouseX - half_w, mouseY - half_h); } } void mousePressed() { println( frameRate ); //setBalls(); } void setBalls() { for( int i = 0; i < numBalls; i++ ) { /* //random placement balls[i] = new Ball(int(random(-range,range)), int(random(-range,range)), int(random(8,25)) ); */ //circle placement float rd = random(360); balls[i] = new Ball( orbit * cos( TWO_PI * rd/360 ), orbit * sin( TWO_PI * rd/360 ), int( random(5,15) ) ); } //sets a random connection to another ball guarenteeing //each ball having at least one line for( int i = 0; i < numBalls; i++ ) { balls[i].setConnection( balls[ int( random(numBalls) ) ] ); } } class Ball { float x, y; float sx, sy, size; int seed = int( random(10000) ); int count = 0; Ball connect; Ball( float Sx, float Sy, int Size ) { sx = Sx; sy = Sy; x = sx; y = sy; size = Size; } void setConnection( Ball ball ) { connect = ball; } void draw(float mx, float my) { count++; x = x + 15 * sin((seed + count)*.03); y = y + 15 * cos((seed + count)*.03); float xDif = mx-x; float yDif = my-y; float distance = sqrt(xDif*xDif+yDif*yDif); float tempX = x - (force/distance)*(xDif/distance); float tempY = y - (force/distance)*(yDif/distance); x = (sx - x)/2+tempX; y = (sy - y)/2+tempY; //2D Circles smooth(); stroke(200); line( x, y, 0, connect.x, connect.y, 0 ); noStroke(); noSmooth(); float s = size * (sin( (count+seed) * .05 ) + 2 ); ellipse( x, y, s, s ); /* //3D Sphere pushMatrix(); translate( x, y ); sphere( size * (sin( (count+seed) * .02 ) + 2 ) ); stroke(180); line( 0, 0, 0, connect.x - x, connect.y - y, 0 ); noStroke(); popMatrix(); */ } }