// Flux 00 (2005. 1. 8.) // by SeungJoon Choi // erucipe@hanmail.net // // http://processing.org VCell[] vcells; Particle[] ptcls; int nw, nh, n_ptcls; float csize; //float[] clearZ; int kR, kG, kB; void setup() { size(400, 300); background(255); rectMode(CENTER_DIAMETER); //clearZ = new float[width*height]; //System.arraycopy(clearZ, 0, g.zbuffer, 0, g.zbuffer.length); g.depthTest = false; noStroke(); nw = 8; nh = 6; csize = width / nw; vcells = new VCell[nw * nh]; for(int y = 0; y < nh; y++) for(int x = 0; x < nw; x++) { int i = y * nw + x; vcells[i] = new VCell(x * csize - (nw - 1) * csize * 0.5, y * csize - (nh - 1) * csize * 0.5, csize); } kR = (int)random(255); kG = (int)random(255); kB = (int)random(255); color pcolor = color(kR, kG, kB); n_ptcls = 100; ptcls = new Particle[n_ptcls]; for(int i = 0; i < n_ptcls; i++) { ptcls[i] = new Particle((1-random(2))*200, (1-random(2))*150, 0, pcolor); } } void loop() { if(random(1) > 0.8) { fill(255, 255, 255, 1); rect(width*0.5, height*0.5, width, height); } push(); beginCamera(); perspective(60, (float)width / (float)height, 1, 1000); //lookat(0, 0, 261, 0, 0, 0, 0, 1, 0); lookat(0, 0, 261 - 10*(1+sin(millis()*0.001)), 0, 0, 0, 0, 1, 0); endCamera(); pop(); if(mousePressed) { background(255); stroke(kR, kG, kB); drawVCells(); } drawPtcls(); push(); //translate(0, 0, 1); rotateY(PI*0.01 * cos(millis()*0.01)); drawPtcls(); pop(); } void mouseReleased() { //System.arraycopy(clearZ, 0, g.zbuffer, 0, g.zbuffer.length); background(255); kR = (int)random(255); kG = (int)random(255); kB = (int)random(255); for(int i = 0; i < n_ptcls; i++) { ptcls[i].kr = kR + (int)((1-random(2))*64); ptcls[i].kg = kG + (int)((1-random(2))*64); ptcls[i].kb = kB + (int)((1-random(2))*64); } } void drawVCells() { for(int y = 0; y < nh; y++) for(int x = 0; x < nw; x++) { int i = y * nw + x; vcells[i].Draw(); } } void drawPtcls() { for(int i = 0; i < n_ptcls; i++) { ptcls[i].Move(); ptcls[i].Draw(); } } class VCell { public VCell(float _x, float _y, float _size) { x = _x; y = _y; size = _size; arrow = new Arrow(x, y, size, (1-random(2))*PI); } public void Draw() { push(); translate(x, y, 0); rect(0, 0, size, size); if(mousePressed) takeAngle(); arrow.Draw(); pop(); } private void takeAngle() { if(mouseY > screenY(0, -size*0.5, 0) && mouseY < screenY(0, size*0.5, 0)) if(mouseX > screenX(-size*0.5, 0, 0) && mouseX < screenX(size*0.5, 0, 0)) { arrow.angle = atan2(mouseY - pmouseY, mouseX - pmouseX); //arrow.angle = atan2(mouseY - screenY(0, 0, 0), mouseX - screenX(0, 0, 0)) + PI*0.5; } } public float x, y, size; Arrow arrow; } class Arrow { public Arrow(float _ox, float _oy, float _size, float _angle) { ox = _ox; oy = _oy; radius = _size * 0.5 * 0.75; angle = _angle; } public void Draw() { push(); rotateZ(angle + PI*0.5); beginShape(POLYGON); vertex(0, -radius, 0); vertex(-radius*0.3, -radius + radius*0.4, 0); vertex(-radius*0.1, -radius + radius*0.4, 0); vertex(-radius*0.1, radius, 0); vertex(radius*0.1, radius, 0); vertex(radius*0.1, -radius + radius*0.4, 0); vertex(radius*0.3, -radius + radius*0.4, 0); vertex(0, -radius, 0); endShape(); //line(0, -radius, 0, radius); pop(); } public float ox, oy, radius, angle; } class Particle { public Particle(float _x, float _y, float _z, color pcolor) { oldx = _x; oldy = _y; oldz = _z; x = _x; y = _y; z = _z; rw = nw * csize * 0.5; rh = nh * csize * 0.5; ax = 100; ay = 100; vx = 0; vy = 0; dt = 0.012; kr = (int)red(pcolor); kg = (int)green(pcolor); kb = (int)blue(pcolor); kr += (int)((1-random(2))*64); kg += (int)((1-random(2))*64); kb += (int)((1-random(2))*64); } public void Move() { oldx = x; oldy = y; oldz = z; int ix = (int)((x + rw) / csize); int iy = (int)((y + rh) / csize); if(ix > nw-1) ix = 0; if(iy > nh-1) iy = 0; if(ix < 0) ix = nw - 1; if(iy < 0) iy = nh - 1; VCell vcell = vcells[iy * nw + ix]; vx += ax*cos(vcell.arrow.angle)*dt; vy += ay*sin(vcell.arrow.angle)*dt; x += vx*dt; y += vy*dt; vx *= 0.99; vy *= 0.99; if(x < -rw) {x = rw; oldx = rw;} if(x > rw) {x = -rw; oldx = -rw;} if(y < -rh) {y = rh; oldy = rh;} if(y > rh) {y = -rh; oldy = -rh;} } public void Draw() { //stroke(kr, kg, kb, 127); //line(oldx, oldy, oldz, x, y, z); push(); translate(x, y, 0); noStroke(); fill(kr, kg, kb, 32); rect(0, 0, 2, 2); pop(); if(mousePressed) { push(); translate(x, y, 0); noFill(); stroke(kR, kG, kB, 255); rect(0, 0, 2, 2); pop(); } } float oldx, oldy, oldz; float x, y, z, rw, rh; float vx, vy, ax, ay, dt; int kr, kg, kb; }