/* ------------------- Bottle Disintegration by Zach Eveland midterm assignment for ICM written in Proce55ing 0091 When the mouse is clicked the bottle disintegrates and blows away the images "piece.jpg" and "background.jpg" at 600x600 pixels each should be in the same directory as the .pde file or the applet --------------------- */ int stageWidth = 600; //the width and height of the stage are set here... int stageHeight = 600; //this way they can be referenced before the stage is actually drawn int redThreshold = 1; //how much difference in the value of red() to tolerate... int greenThreshold = 1; //between the pixels in the target picture and the background... int blueThreshold = 1; //picture int arrayMax = 0; //this variable keeps track of how many target pixels there are... //the "oFixedPixel" and "oFlyingPixel" arrays contain only the pixels //to be animated - positions from 0 to "arrayMax" are occupied int redFadeVal = 10; int greenFadeVal = 10; int blueFadeVal = 10; int disintegrateStart = 0; int howManyToDisintegrate = 1000; boolean disintegrating = false; int panic = 5000; PImage b; //the picture of the target to be disintegrated OR the foreground PImage c; //the picture of the target's background BackgroundPixel[] oBackgroundPixel = new BackgroundPixel[stageWidth * stageHeight]; FixedPixel[] oFixedPixel = new FixedPixel[stageWidth * stageHeight]; FlyingPixel[] oFlyingPixel = new FlyingPixel[stageWidth * stageHeight]; void setup() { size(stageWidth,stageHeight); framerate(12); /* --------------- if substituting other images for the ones included follow the naming convention "piece.jpg" for the image containing the object to disintegrate and "background.jpg" for the image that doesn't contain the object to disintegrate ----------------- */ b = loadImage("piece.jpg"); c = loadImage("background.jpg"); loadPixels(); //this must be done to initialize the "pixels[]" array makeObjects(); } void draw() { panic = 5000; /* ----------------- This routine disintegrates the bottle in a scanning pattern ------------------ */ if(disintegrating == true) { for(int i=0;i 0)) { whichOne = int(random(0,arrayMax)); panic = panic - 1; } oFixedPixel[whichOne].startFading(); oFlyingPixel[whichOne].startFlying(); } */ } for(int i=0;i=redThreshold && abs(gf-gb)>=greenThreshold && abs(bf-bb)>=blueThreshold) { oFixedPixel[arrayMax] = new FixedPixel(sourcePixel,b.pixels[sourcePixel]); oFlyingPixel[arrayMax] = new FlyingPixel(col,row,b.pixels[sourcePixel]); arrayMax = arrayMax + 1; } } } } class FlyingPixel { int xPos; int yPos; color pixelColor; color displayColor; int destPixel; int xMove; int yMove; int decay; boolean flying = false; FlyingPixel(int _x,int _y,int _pc) { xPos = _x; yPos = _y; pixelColor = _pc; } void refresh() { xPos = xPos + xMove; yPos = yPos + yMove; if((xPos < 0) || (xPos > width)) { xMove = 0; xPos = 0; } if((yPos < 0) || (yPos > height)) { yMove = 0; yPos = 0; } destPixel = yPos * width + xPos; if(destPixel < 0) { destPixel = 0; } if(destPixel > pixels.length) { destPixel = pixels.length; } /* these three lines are different attempts at calculating a color value for the flying pixels that improve the quality of the illusion. if a pixel retains its original color it looks too... solid so I average the pixels original color with the color the pixel it is passing over to simulate an alpha effect (since pixels can't have an alpha value */ // displayColor = color((red(pixelColor)+red(c.pixels[destPixel]))/2,(green(pixelColor)+green(c.pixels[destPixel]))/2,(blue(pixelColor)+blue(c.pixels[destPixel]))/2); displayColor = color((2*red(pixelColor)+red(c.pixels[destPixel]))/3,(2*green(pixelColor)+green(c.pixels[destPixel]))/3,(2*blue(pixelColor)+blue(c.pixels[destPixel]))/3); // displayColor = color((red(pixelColor)+red(b.pixels[destPixel]))/2,(green(pixelColor)+green(b.pixels[destPixel]))/2,(blue(pixelColor)+blue(b.pixels[destPixel]))/2); if(flying == true) { pixels[destPixel] = displayColor; if((destPixel - 2*width) > 0) { pixels[destPixel-2*width] = displayColor; } if((destPixel + 2*width) > 0) { pixels[destPixel+2*width] = displayColor; } if((destPixel - 2) > 0) { pixels[destPixel-2] = displayColor; } if((destPixel + xMove) > 0) { pixels[destPixel + xMove] = displayColor; } if((destPixel + yMove*width) > 0) { pixels[destPixel + yMove*width] = displayColor; } } decay = decay - 1; if((flying == true) && (decay <= 0)) { startFlying(); } } void startFlying() { xMove = int(random(-5,1)); yMove = int(random(-3,-1)); decay = int(random(5,10)); flying = true; } } /* the FixedPixel objects are the component pixels of the image of the target object they are slowly faded to the background color to make the animation more realistic - if the flying pixels just leave an area the background is immediately visible. this is a workable solution but can definitely be improved */ class FixedPixel { int destPixel; color pixelColor; int redFade; int greenFade; int blueFade; boolean fading = false; FixedPixel(int _dp,int _pc) { destPixel = _dp; pixelColor = _pc; } void refresh() { if(abs(red(pixelColor) - red(oBackgroundPixel[destPixel].pixelColor)) <= abs(redFade)) { redFade = 0; pixelColor = color(red(oBackgroundPixel[destPixel].pixelColor),green(pixelColor),blue(pixelColor)); } if(abs(green(pixelColor) - green(oBackgroundPixel[destPixel].pixelColor)) <= abs(greenFade)) { greenFade = 0; pixelColor = color(red(pixelColor),green(oBackgroundPixel[destPixel].pixelColor),blue(pixelColor)); } if(abs(blue(pixelColor) - blue(oBackgroundPixel[destPixel].pixelColor)) <= abs(blueFade)) { blueFade = 0; pixelColor = color(red(pixelColor),green(pixelColor),blue(oBackgroundPixel[destPixel].pixelColor)); } if(fading == true) { pixelColor = color(red(pixelColor)+redFade,green(pixelColor)+greenFade,blue(pixelColor)+blueFade); } pixels[destPixel] = pixelColor; } void startFading() { if(red(pixelColor) > red(oBackgroundPixel[destPixel].pixelColor)) { redFade = -1 * redFadeVal; } else { redFade = redFadeVal; } if(green(pixelColor) > green(oBackgroundPixel[destPixel].pixelColor)) { greenFade = -1 * greenFadeVal; } else { greenFade = greenFadeVal; } if(blue(pixelColor) > blue(oBackgroundPixel[destPixel].pixelColor)) { blueFade = -1 * blueFadeVal; } else { blueFade = blueFadeVal; } fading = true; } } class BackgroundPixel { color pixelColor; int destPixel; BackgroundPixel(int _dp,int _pc) { destPixel = _dp; pixelColor = _pc; } void refresh() { pixels[destPixel] = pixelColor; } }