int numitems = 0;
int MAXITEMS = 1000;
thingy itemarr[] = new thingy[MAXITEMS];

void setup(){
  size(300,300);
  ellipseMode(CENTER_DIAMETER);
  fill(255); //white circles
}

void loop(){
  background(100);

  float centerX = 0, centerY = 0;
  float howMany=0;

  //centerx and y represent the "center of gravity"
  // that balls are drawn to; here we show all balls
  //in current position and add their center point to
  //be averaged out
  for(int i = 0; i < numitems; i++){
    thingy th = itemarr[i];
    th.showthingy();
    centerX += th.getX();
    centerY += th.getY();
    howMany ++;
  }

  if(howMany > 0){
    centerX /= howMany;
    centerY /= howMany;
  }
//add in attractive force to center as
//well as screen center to each ball
  for(int i = 0; i < numitems; i++){
    thingy th = itemarr[i];
    th.recenter(width/2,height/2);
    th.recenter(centerX,centerY);
    //line(th.getX(),th.getY(),centerX,centerY);
  }
//mark the center point
  point(centerX,centerY);
  if(numitems < MAXITEMS){
//show current ball being drawn if mouse is down (just outline
        if(mousePressed){
      //noFill();
      float sz = (millis() - m)/10;
      ellipse(mouseX,mouseY,sz,sz);
      //fill(255);
    }
  }
}

//thingy is ball

class thingy {
  float x, y, thingsize;
  float xspeed, yspeed;
  float accel;
  float maxspeed = 10;

  float getX(){
    return x;
  }
  float getY(){
    return y;
  }
  float getSize(){
    return thingsize;
  }

  thingy(float newx, float newy, float newsize, float newaccel){
    x = newx;
    y = newy;
    thingsize = newsize;
    accel = newaccel;
  }
  void showthingy(){
    fill(255);
    ellipse(x,y,thingsize,thingsize);
    //rect(x,y,(1/xspeed)*20,(1/yspeed)*20);
  }
  //recenter makes attraction to x y point
  void recenter(float inx, float iny){
    if(x < inx){
      xspeed += accel;
    }
    if(x > inx){
      xspeed -= accel;
    }

    if(y < iny){
      yspeed += accel;
    }

    if(y > iny){
      yspeed -= accel;
    }

    xspeed = constrain(xspeed,-maxspeed,maxspeed);
    yspeed = constrain(yspeed,-maxspeed,maxspeed);

    x += xspeed;
    y += yspeed;
  }
}

float m;

//ball size is how long they hold mouse,
//remember start time
void mousePressed(){
  m = millis();
}

//when mouse released, let new ball drop into wild...
void mouseReleased(){
  if(numitems < MAXITEMS){
    itemarr[numitems] = new thingy(mouseX,mouseY,(millis() - m)/10,.1);
    numitems++;
  }
}