int TUBMIN=30; int HALF_TUBMIN=TUBMIN/2; int RADIOPELOTA=4; int FADINGDETUBES=45; int MINVELPELOTA=1; int MAXVELPELOTA=40; Tuberia sTuberia; Vector Tuberias; Pelota sPelota; Tuberia oTuberia; boolean saveCurrent=false; float[][] paleta1={{ 153, 116, 176}, { 192, 0, 0}, { 237, 227, 0}, { 9, 0, 139}, { 10, 200, 35}}; // Multicolor float[][] paleta2={{ 155, 50, 50}, { 255, 0, 200}, { 150, 10, 30}, { 155, 30, 150}}; // Red float[][] paleta3={{ 50, 155, 50}, { 200, 255, 0}, { 30, 150, 10}, { 150, 155, 30}}; // Green float[][] paleta4={{ 50, 50, 155}, { 0, 200, 255}, { 10, 30, 150}, { 30, 150, 155}}; // Blue float[][] paleta5={{ 13, 15, 40}, { 5, 45, 41}, { 15, 45, 11}, { 35, 5, 41}, { 40, 11, 15}, { 35, 45, 1}}; // Dark float[][] paleta6={{200, 0, 0}, {201, 0, 200}, {0, 0, 200}}; // Rossi float[][] paleta7={{0, 200, 0}, {200, 201, 0}, {200, 0, 0}}; // Greeni float[][] paleta8={{0, 0, 200}, {0, 200, 201}, {0, 200, 0}}; // Bluei float[][] paleta; // oscP5 instance for the osc communication OscP5 oscP5; int receiveAtPort; int sendToPort; String host; String oscP5event; void initOsc() { receiveAtPort = 12099; sendToPort = 9999; host = "127.0.0.1"; oscP5event = "oscEvent"; oscP5 = new OscP5( this, host, sendToPort, receiveAtPort, oscP5event ); } void oscEvent(OscIn oscIn) { //println("received ..."); } void setup(){ size(500, 500); background(0); framerate(20); noStroke(); ellipseMode(CENTER_RADIUS); try{ initOsc(); }catch(Exception e){ println("OSC sender not correctly initialized..."); } // Tuberias Tuberias = new Vector(); sTuberia = null; sPelota = null; oTuberia = null; // Escoger la paleta //int numpaleta = int(random(8))+1; int numpaleta = 1; switch(numpaleta){ case 1: paleta=paleta1; break; case 2: paleta=paleta2; break; case 3: paleta=paleta3; break; case 4: paleta=paleta4; break; case 5: paleta=paleta5; break; case 6: paleta=paleta6; break; case 7: paleta=paleta7; break; case 8: paleta=paleta8; break; default: paleta=paleta1; break; } } void loop(){ background(0); for(int i=Tuberias.size()-1; i >= 0; i--){ ((Tuberia)Tuberias.get(i)).paint(); } if(sTuberia!=null){ sTuberia.paint(); } if(sPelota!=null){ sPelota.update(null); sPelota.paint(null); } if(saveCurrent){ saveFrame(); saveCurrent=false; } } void mousePressed() { if((sTuberia==null) && (!keyPressed)) { // boton derecho pulsado for(int i=Tuberias.size()-1 ; i>=0 ; i--){ Tuberia tub = (Tuberia)(Tuberias.get(i)); if(tub.over()&&(tub.dead==false)){ sPelota = new Pelota(mouseX,mouseY,tub); oTuberia = tub; return; } } } if((sPelota==null)) { // boton izquierdo pulsado if(Tuberias.size()<=0){ int limiteTop = 0; int limiteBottom = height; int mX = mouseX; int mY = mouseY; sTuberia = new Tuberia(mX,mY,limiteTop,limiteBottom); return; }else{ int limiteTop = 0; int limiteBottom = height; int mX = mouseX; int mY = mouseY; for(int i=Tuberias.size()-1 ; i>=0 ; i--){ Tuberia tub = (Tuberia)(Tuberias.get(i)); if(tub.over()){ if(keyPressed && key==SHIFT) tub.eliminar(); return; } if(mY>tub.y2){ limiteTop = max(limiteTop,tub.y2); }else{ limiteBottom = min(limiteBottom,tub.y1); } } sTuberia = new Tuberia(mX,mY,limiteTop,limiteBottom); return; } } } void mouseReleased(){ if(sPelota != null){ int mX = constrain(mouseX,0,width); sPelota.crear(mX,mouseY); oTuberia.pelotasTub.add(sPelota); sPelota = null; oTuberia = null; }else if(sTuberia != null) { int ind = Tuberias.size(); int mY = constrain(mouseY,sTuberia.maxy1,sTuberia.maxy2); if(abs(mY-sTuberia.y1)>TUBMIN){ int mX = constrain(mouseX,0,width); sTuberia.crear(mX,mY,ind); Tuberias.add(sTuberia); } sTuberia = null; } } void keyPressed(){ if(key == 'z'){ saveCurrent = true; } if(Tuberias.size()==0){ if(key == '1'){ paleta = paleta1; } if(key == '2'){ paleta = paleta2; } if(key == '3'){ paleta = paleta3; } if(key == '4'){ paleta = paleta4; } if(key == '5'){ paleta = paleta5; } if(key == '6'){ paleta = paleta6; } if(key == '7'){ paleta = paleta7; } if(key == '8'){ paleta = paleta8; } } } class Tuberia{ int id; color c; int y1, y2; int ypos, talla; int maxy1, maxy2; boolean dead = false; int countdead = 254; BGraphics gfx; BGraphics buffer; Vector pelotasTub; Tuberia(int _x,int _y, int _maxy1, int _maxy2){ maxy1 = _maxy1; maxy2 = _maxy2; y1 = _y; c = calculaColor(_x,255); pelotasTub = new Vector(); int disty1 = abs(y1-maxy1); int disty2 = abs(y1-maxy2); if((disty10) { float r = random(1); if(r>0.5) if(blur(this.buffer)) this.eliminar(); } gfx=buffer; noTint(); noStroke(); image(gfx,0,y1,width,y2-y1); for(int i=0 ; i=y1)&&(mouseY<=y2))) { return true; }else{ return false; } } void sonidoCreate(){ OscMessage oscMsg = oscP5.newMsg("/createtube"); float _c = c; float _r = red(c); float _g = green(c); float _b = blue(c); float _talla = talla; float _y = ypos; _talla = normaliza(_talla,TUBMIN,height); _y = normaliza(_y,HALF_TUBMIN,height-HALF_TUBMIN); _r = normaliza(_r,0,255); _g = normaliza(_g,0,255); _b = normaliza(_b,0,255); //println("y de la tub creada="+_y); //println("talla de la tub creada="+_talla); //println("r de la tub creada="+_r); //println("g de la tub creada="+_g); //println("b de la tub creada="+_b); // add an int to the osc message oscMsg.add(_y); // x oscMsg.add(_talla); // talla oscMsg.add(_r); // r oscMsg.add(_g); // g oscMsg.add(_b); // b // send the osc message try{ oscP5.sendMsg(oscMsg); }catch(Exception e){ println("No permissions for TCP/IP..."); } } } class Pelota{ color c; // color color ctub; // color de la tuberia float x; // posicion x float y; // posicion y float vx; // velocidad en las x float vy; // velocidad en las y float v; // velocidad float ang; // angulo de la direccion int r=RADIOPELOTA; // radio de la pelota Pelota(int _x, int _y, Tuberia _tub){ x = _x; y = _y; c = 255; ctub=_tub.c; } void crear(int _x,int _y){ calcDirVel(_x,_y); vx = v * cos(ang); vy = v * sin(ang); } void update(Tuberia tub){ if((this!=sPelota)&&(tub!=null)){ x+=vx; y+=vy; int tuby1=tub.y1; int tuby2=tub.y2; color tubc=tub.c; if((y-r<=tuby1)&&(vy<0)){ y=2*(tuby1+r)-y; vy=-vy; sonidoPelota(); } if((y+r>=tuby2)&&(vy>0)){ y=2*(tuby2-r)-y; vy=-vy; sonidoPelota(); } if((x>width)&&(vx>0)){ x=x%width; //println("x="+x+" and vx="+vx); } if((x<0)&&(vx<0)){ x=(width+x)%width; //println("x="+x+" and vx="+vx); } }else{ calcDirVel(mouseX,mouseY); float cosval=cos(ang); float sinval=sin(ang); vx = v*cosval; vy = v*sinval; } } void sonidoPelota(){ OscMessage oscMsg = oscP5.newMsg("/choque"); float _r = red(ctub); float _g = green(ctub); float _b = blue(ctub); float _x = normaliza(x,0,width); float _vel = normaliza(v,MINVELPELOTA,MAXVELPELOTA); float _angulo = normaliza(abs(abs(ang)-HALF_PI),0,HALF_PI); _angulo = abs(1-_angulo); _vel = abs(1-_vel); _r = normaliza(_r,0,255); _g = normaliza(_g,0,255); _b = normaliza(_b,0,255); int _sample=1; if((_r>=_g)&&(_r>=_b)){ _sample=1; }else if((_g>=_b)){ _sample=2; }else{ _sample=3; } //println("angulo del choque="+_angulo); //println("x del choque="+_x); //println("vel del choque="+_vel); // add an int to the osc message oscMsg.add(_x); // x oscMsg.add(_angulo); // angulo oscMsg.add(_vel); // velocidad oscMsg.add(_sample); // sample // send the osc message try{ oscP5.sendMsg(oscMsg); }catch(Exception e){ println("No permissions for TCP/IP..."); } } void calcDirVel(int _x,int _y){ v = constrain(dist(x,y,_x,_y),MINVELPELOTA,MAXVELPELOTA); ang = atan2(_y-y,_x-x); } void paint(Tuberia tub){ if(this==sPelota){ noFill(); stroke(255); line(x+vx/v*r,y+vy/v*r,x+vx*1,y+vy*1); ellipse(x,y,r,r); }else{ BGraphics target = tub.buffer; target.fill(c); target.noStroke(); target.ellipse(x,y-tub.y1,r,r); } } } float normaliza(float val, float valmin, float valmax){ return (val - valmin)/(valmax - valmin); } int Red(int c) { return (c & 0xff0000) >> 16; } int Green(int c) { return (c & 0x00ff00) >> 8; } int Blue(int c) { return c & 0x0000ff; } boolean blur(BGraphics target) { int index,left,right,top,bottom; int sumr, sumg, sumb; float[] whitecount = new float[3]; float nbpixels= target.width * target.height; boolean whited = false; whitecount[0]=0; whitecount[1]=0; whitecount[2]=0; for(int j=0;j0) left=-1; else left=target.width-1; if(j==(target.width-1)) right=-target.width+1; else right=1; if(i>0) top=-target.width; else top=(target.height-1)*target.width; if(i==(target.height-1)) bottom=-(target.height-1)*target.width; else bottom=target.width; // Calculate the sum of n neighbors sumr=Red(target.pixels[left+index]); // left middle sumg=Green(target.pixels[left+index]); sumb=Blue(target.pixels[left+index]); sumr+=Red(target.pixels[right+index]); // right middle sumg+=Green(target.pixels[right+index]); sumb+=Blue(target.pixels[right+index]); sumr+=Red(target.pixels[index]); // middle middle sumg+=Green(target.pixels[index]); sumb+=Blue(target.pixels[index]); sumr+=Red(target.pixels[index+top]); // middle top sumg+=Green(target.pixels[index+top]); sumb+=Blue(target.pixels[index+top]); sumr+=Red(target.pixels[index+bottom]); // middle bottom sumg+=Green(target.pixels[index+bottom]); sumb+=Blue(target.pixels[index+bottom]); sumr=(int)((float)sumr/4.99); sumg=(int)((float)sumg/4.99); sumb=(int)((float)sumb/4.99); target.pixels[index]=(sumr<<16)+(sumg<<8)+sumb; whitecount[0]+=(sumr)/nbpixels; whitecount[1]+=(sumg)/nbpixels; whitecount[2]+=(sumb)/nbpixels; } } if((whitecount[0]>240)&&(whitecount[1]>240)&&(whitecount[2]>240)) whited = true; return whited; }