import processing.video.*; Capture video; Ball[] allBalls = new Ball[300]; int arrayPositionForNewBall = 0; void setup(){ video = new Capture(this, 320, 240, 30); size(320, 240); noStroke(); } void captureEvent(Capture camera) { camera.read(); } color getVideoPixelColor(int _x, int _y){ //width * number of row + number of column is the way you convert from //two x and y coordinates into the array which is really just a long linear list of coordinates color pixColor = video.pixels[_y*video.width + _y]; return pixColor; } void draw(){ for (int i = 0; i < allBalls.length; i++){ Ball thisBall = allBalls[i]; if (thisBall != null){ thisBall.doEverything(); } } } void mousePressed(){ //make a new ball everytime you click, store it in the next place in the array if (arrayPositionForNewBall >= allBalls.length){ arrayPositionForNewBall = 0; } allBalls[arrayPositionForNewBall] = new Ball(mouseX,mouseY); arrayPositionForNewBall++; } class Ball{ int xpos, ypos; float xspeed = 2.0; // Speed of the shape float yspeed = 2.2; // Speed of the shape int xdirection = 1; // Left or Right int ydirection = 1; // Top to Bottom int ballSize = 15; Ball(int _startx, int _starty){ xpos = _startx; ypos = _starty; } void doEverything(){ incrementPosition(); checkWalls(); drawMe(); } void incrementPosition(){ // Update the position of the shape xpos = xpos + int( xspeed * xdirection ); ypos = ypos + int( yspeed * ydirection ); } void checkWalls(){ if (xpos > width-ballSize || xpos < 10) { xdirection *= -1; } if (ypos > height-ballSize || ypos < 10) { ydirection *= -1; } } void drawMe(){ // Draw the shape color colorOfThisPixelInVideo = getVideoPixelColor(xpos,ypos); fill( colorOfThisPixelInVideo); rect(xpos, ypos, ballSize, ballSize); } }