import ddf.minim.*; import fisica.*; FWorld world; FBox caja, barra; FCircle bola; Minim minim; AudioSample sonido; int posX = mouseX = 400; void setup () { //Set size and framerate size(480,500, P2D); frameRate(60); noCursor(); // oculto el puntero // cargo el sonido minim = new Minim(this); sonido = minim.loadSample("waterdrop.aif", 512); if (sonido == null) println("No se ha cargado el archivo de sonido 2"); Fisica.init(this); world = new FWorld(); world.setGravity(0, 0); world.setEdges(this, color(40)); world.setEdgesRestitution(1.05f); world.setDamping(1f); // agregamos las cajas de arriba for( int x = 1; x < 8; x ++){ for( int y = 1; y < 5; y++){ float r = random(200,220); float g = random(0,160); float b = random(50,70); caja = new FBox(50, 20); caja.setPosition(60*x, 30*y); caja.setFillColor(r, g, b); caja.setStrokeColor(r, g, b); caja.setStaticBody(true); caja.setRestitution(1f); world.add(caja); } } // creo la barra barra = new FBox(100, 20); barra.setPosition(width/2, height-10); barra.setFillColor(240); barra.setStrokeColor(240); barra.setRestitution(1f); barra.setStaticBody(true); world.add(barra); // creo la bola bola = new FCircle(15); bola.setPosition(width/2, height/3); bola.setFillColor(240); bola.setStrokeColor(240); bola.setRestitution(1f); bola.setVelocity(50, 400); bola.setDamping(0); // la friccion a 0 - sin frinccion world.add(bola); } void draw () { //Clear screen //background(40); fill(40,100); noStroke(); rect(0,0,width,height); if (bola.getY() > height-20) { bola.setPosition(width/2, height/3); bola.setRestitution(1f); bola.setVelocity(50, 400); } //Draw world world.step(); world.draw(this); } // determina la colision de objetos void contactStarted(FContact contacto) { FBody cuerpo1 = contacto.getBody1(); FBody cuerpo2 = contacto.getBody2(); //FBody barra = null; if (cuerpo1 != barra && cuerpo2 != barra){ if (bola.getX() < (width-20) && bola.getX() > 20 && bola.getY() > 20 && bola.getY() < (height-20)) { cuerpo1.setFillColor(22,0,240); cuerpo1.setDrawable(false); cuerpo1.setSensor(true); } } else { bola.setForce(random(-80000, 100000), random(-200000, 200000)); sonido.trigger(); } } void mouseMoved() { // posiciono la barra barra.setPosition( mouseX, barra.getY() ); } void keyPressed() { if (key == CODED) { if (keyCode == LEFT) { barra.setPosition( barra.getX()-10, barra.getY() ); println(barra.getX()); } else if (keyCode == RIGHT) { barra.setPosition( barra.getX()+10, barra.getY() ); } } } void stop() { // always close Minim audio classes when you are done with them sonido.close(); // always stop Minim before exiting. minim.stop(); super.stop(); }