/////////////// // libraries // /////////////// import processing.opengl.*; import traer.physics.*; ///////////// // globals // ///////////// // particles ParticleSystem physics; Particle[][] particles; // images PImage gSrcImg; PImage [][] gImageMatrix; // grid int gGridSize; int gGridStepX; int gGridStepY; int gOffsetX; int gOffsetY; // interaction int gMouseDistance; boolean gClicked; float []gClickedOffsetX; /////////// // setup // /////////// void setup() { // general sketch properties size(800, 600, OPENGL); framerate(24); background(0); noStroke(); // grid gGridSize = 5; gGridStepX = int((float) (((width-200)/2)/(gGridSize-1))); gGridStepY = int((float) ((height/2)/(gGridSize-1))); gOffsetX = ((width-200)/2) - ((gGridSize/2)*gGridStepX)+100; gOffsetY = (height/2) - ((gGridSize/2)*gGridStepY)+50; // build particles physics = new ParticleSystem(1.0, 0.1); particles = new Particle[gGridSize][gGridSize]; for (int i = 0; i < gGridSize; i++){ for (int j = 0; j < gGridSize; j++){ // particles float tempX = float((i*gGridStepX + gOffsetX)); float tempY = float((j*gGridStepY + gOffsetY)); particles[i][j] = physics.makeParticle(0.1, tempX, tempY, 0.0); // make fixed if last row if(j == (gGridSize-1)){ particles[i][j].makeFixed(); } } } // define spring behaviour of particles for (int i = 0; i < gGridSize; i++){ for (int j = 0; j < gGridSize; j++){ // x if(i < gGridSize-1){ physics.makeSpring(particles[i][j], particles[i+1][j], 5.0, 0.5, gGridStepX); } else{ physics.makeSpring(particles[i][j], particles[i-1][j], 5.0, 0.5, gGridStepX); } // y if(j < gGridSize-1){ physics.makeSpring(particles[i][j], particles[i][j+1], 5.0, 0.5, gGridStepY); } else{ physics.makeSpring(particles[i][j], particles[i][j-1], 5.0, 0.5, gGridStepY); } } } // image matrix gSrcImg = loadImage("helveticaV1.jpg"); gImageMatrix = new PImage[gGridSize][gGridSize]; // build matrix for (int i = 0; i < gGridSize-1; i++){ for (int j = 0; j < gGridSize-1; j++){ // create new image gImageMatrix[i][j] = new PImage(gGridStepX,gGridStepY); // copy parts of source image in image matrix gImageMatrix[i][j].copy(gSrcImg,(i*gGridStepX),(j*gGridStepY),gGridStepX,gGridStepY,0,0,gGridStepX,gGridStepY); } } // interaction gMouseDistance = gGridStepY*3; gClicked = false; gClickedOffsetX = new float[gGridSize]; } ////////// // draw // ////////// void draw() { // clear background fill(0,50); rect(0,0,width,height); // physics physics.advanceTime(0.1); // color fill(255,70); // limes gravity particles for (int i = 0; i < gGridSize; i++) { for (int j = 0; j < gGridSize; j++){ if(particles[i][j].position().y() >= particles[1][gGridSize-1].position().y()){ float tempX = particles[i][j].position().x(); float tempY = particles[1][gGridSize-1].position().y(); float tempZ = 0.0; particles[i][j].moveTo(tempX, tempY,tempZ); } } } // draw image matrix for (int i = 0; i < gGridSize-1; i++) { for (int j = 0; j < gGridSize-1; j++){ // no stroke noStroke(); // draw textureMode(NORMALIZED); beginShape(); texture(gImageMatrix[i][j]); vertex(ceil(particles[i][j].position().x()),ceil(particles[i][j].position().y()),0,0); vertex(ceil(particles[i+1][j].position().x()),ceil(particles[i+1][j].position().y()),1,0); vertex(ceil(particles[i+1][j+1].position().x()),ceil(particles[i+1][j+1].position().y()),1,1); vertex(ceil(particles[i][j+1].position().x()),ceil(particles[i][j+1].position().y()),0,1); endShape(); } } // interaction if (mousePressed){ // just one time if(gClicked == false){ for (int i = 0; i < gGridSize; i++){ // offsetX (just for top particles) gClickedOffsetX[i] = float(mouseX)- particles[i][0].position().x(); } // change status gClicked = true; } // check distance int tempDistance = abs(mouseY-int(particles[1][0].position().y())); if(tempDistance <= gMouseDistance){ for (int i = 0; i < gGridSize; i++){ float tempX = float(mouseX)-gClickedOffsetX[i]; float tempY = float(mouseY); float tempZ = 0.0; particles[i][0].moveTo(tempX, tempY,tempZ); particles[i][0].velocity().clear(); } } } } void mouseReleased(){ // change status gClicked = false; }