// Pile up! // version 1.0 // Craig Kapp // September 18th, 2008 // declare the size of the canvas int canvasw = 500; int canvash = 500; // instantiate a new Landscape object. This object keeps track of balls that have stopped moving Landscape landscape = new Landscape(500, 500); // create a holder array for "active" balls (i.e. balls that are still moving) Ball[] ballarray; int maxballs = 50000; int currentball = 0; // declare canvas size and initalize the array for "active" balls void setup() { size (canvasw, canvash); noStroke(); ballarray = new Ball[maxballs]; // initialize the max number of ball objects that we'll be using for (int c = 0; c < maxballs; c++) { ballarray[c] = new Ball(0, 0, 5); } } // continuous loop void draw() { drop_ball(); // clear the background and ask the landscape object to render any balls that have stopped moving background (0); landscape.Display(); // cycle through all active ball objects for (int counter = 0; counter < maxballs; counter++) { // see if we need to deactivate this ball if (ballarray[counter].alive == 1 && ballarray[counter].yspeed > 0) { // test to see if this ball has hit a ball that has already been deactivated (downwards direction) boolean ditest = false; boolean ritest = false; boolean litest = false; if (floor(ballarray[counter].x) > 25 && floor(ballarray[counter].x) < width-25 && floor(ballarray[counter].y+ballarray[counter].r/2) > 25 && floor(ballarray[counter].y+ballarray[counter].r/2) < height-25) { ditest = landscape.IntersectTest(floor(ballarray[counter].x), floor(ballarray[counter].y+ballarray[counter].r/2)); ritest = landscape.IntersectTest(floor(ballarray[counter].x+ballarray[counter].r/2), floor(ballarray[counter].y)); litest = landscape.IntersectTest(floor(ballarray[counter].x-ballarray[counter].r/2), floor(ballarray[counter].y)); } // if so, test to see if it is moving sufficiently slow enough to stop. if (ditest == true) { // bounce if speed is high enough if (ballarray[counter].yspeed > 0.5) { ballarray[counter].yspeed = -1 * ballarray[counter].yspeed/2; } // ball is moving slow enough to stop else { ballarray[counter].alive = 2; } } // if so, test to see if it is moving sufficiently slow enough to stop. if (ritest == true) { // bounce if speed is high enough if (ballarray[counter].xspeed > 0.1) { ballarray[counter].xspeed = -1 * ballarray[counter].xspeed/2; // give the ball a second chance if it already got pegged as hitting a bottom facing ball ballarray[counter].alive = 1; ballarray[counter].bouncecount++; } // ball is moving slow enough to stop else { ballarray[counter].alive = 2; ballarray[counter].x = ballarray[counter].x + 1/2 * ballarray[counter].r; if (ditest == true) { ballarray[counter].x = ballarray[counter].x + 1/2 * ballarray[counter].r; } } } // if so, test to see if it is moving sufficiently slow enough to stop. if (litest == true) { // bounce if speed is high enough if (ballarray[counter].xspeed > 0.1) { ballarray[counter].xspeed = -1 * ballarray[counter].xspeed/2; // give the ball a second chance if it already got pegged as hitting a bottom facing ball ballarray[counter].alive = 1; ballarray[counter].bouncecount++; } // ball is moving slow enough to stop else { ballarray[counter].alive = 2; ballarray[counter].x = ballarray[counter].x - 1/2 * ballarray[counter].r; if (ditest == true) { ballarray[counter].y = ballarray[counter].y + 1/2 * ballarray[counter].r; } } } } // pick up any balls that need to be deactivated if (ballarray[counter].alive == 2) { // update the landscape object to keep track of this deactivated ball ballarray[counter].colorreset(); landscape.Add(ballarray[counter].x, ballarray[counter].y, ballarray[counter].redvalue, ballarray[counter].bluevalue, ballarray[counter].greenvalue, ballarray[counter].r); // reset this ball so it can be used again ballarray[counter].reset(); } // if the ball is still active, cause it to move ballarray[counter].move(); } } void keyPressed() { println ("CLEAR!"); landscape.clear(); } // if the mouse is being dragged with the button held down, drop a ball void mouseDragged() { drop_ball(); } // if the mouse is pressed a single time, drop a ball void mousePressed() { drop_ball(); } // drops a ball from the current mouse position void drop_ball() { ballarray[currentball].x = mouseX; ballarray[currentball].y = mouseY; ballarray[currentball].yspeed = random(5); ballarray[currentball].alive = 1; currentball++; if (currentball > maxballs-1) { currentball = 0; } }