ArrayList particles; ArrayList clearParticle; void setup(){ size( 500, 500 ); background( 0 ); particles = new ArrayList(); clearParticle = null; } void loop(){ background(0); for( int i = 0; i < 30; i++ ){ color c = color( 130, 70, 10 ); float my = mouseY > height-4 ? height-4 : mouseY; particles.add( new Particle( mouseX, my, 0, 0, c ) ); } clearParticle = null; Iterator it = particles.iterator(); while( it.hasNext() ){ Particle p = ( Particle )it.next(); p.relax(); p.drawSelf(); } if( clearParticle != null ){ it = clearParticle.iterator(); while( it.hasNext() ){ Particle p = ( Particle )it.next(); particles.remove( p ); } } } class Particle{ float x, y; float lastx, lasty; float dx, dy; color c; int count; static final float G = -0.5f; static final float E = 0.5f; Particle( float x, float y, float dx, float dy, color c ){ this.x = x; this.y = y; this.lastx = x; this.lasty = y; this.dx = dx; this.dy = dy; this.c = c; this.count = 0; } void relax(){ dx += 1.0f - random(2); dy += 1.0f - random(2); dy += Particle.G; x += dx; y += dy; dx *= 0.90; dy *= 0.90; //ǂƂ̏Փ˔ if( x < 0 ){ x = 0; dx *= -1 * Particle.E; } else if( x > width-1 ){ x = width-1; dx *= -1 * Particle.E; } if( y < 0 ){ if( dy <= 5 ){ if( clearParticle == null ){ clearParticle = new ArrayList(); } clearParticle.add( this ); y = 0; dy *= -1 * Particle.E; return; } else{ y = 0; dy *= -1 * Particle.E; } } else if( y > height-4 ){ y = height-4; dy *= -1 * Particle.E; } c = subtract( c, color( 3, 3, 3 ) ); count++; if( count > 30 ){ if( clearParticle == null ){ clearParticle = new ArrayList(); } clearParticle.add( this ); } } void drawSelf(){ int addx, addy = 0; int pos; pos = width * (int)y + (int)x; pixels[pos] = add( pixels[pos], subtract( c, color( 40, 40, 40 ) ) ); pos = width * (int)y + (int)(x+1); pixels[pos] = add( pixels[pos], subtract( c, color( 20, 20, 20 ) ) ); pos = width * (int)y + (int)(x+2); pixels[pos] = add( pixels[pos], subtract( c, color( 40, 40, 40 ) ) ); pos = width * (int)(y+1) + (int)x; pixels[pos] = add( pixels[pos], subtract( c, color( 20, 20, 20 ) ) ); pos = width * (int)(y+1) + (int)(x+1); pixels[pos] = add( pixels[pos], c ); pos = width * (int)(y+1) + (int)(x+2); pixels[pos] = add( pixels[pos], subtract( c, color( 20, 20, 20 ) ) ); pos = width * (int)(y+2) + (int)x; pixels[pos] = add( pixels[pos], subtract( c, color( 40, 40, 40 ) ) ); pos = width * (int)(y+2) + (int)(x+1); pixels[pos] = add( pixels[pos], subtract( c, color( 20, 20, 20 ) ) ); pos = width * (int)(y+2) + (int)(x+2); pixels[pos] = add( pixels[pos], subtract( c, color( 40, 40, 40 ) ) ); lastx = x; lasty = y; } color subtract( color c1, color c2 ){ float c1R = c1 >> 16 & 0xff; float c1G = c1 >> 8 & 0xff; float c1B = c1 & 0xff; float c2R = c2 >> 16 & 0xff; float c2G = c2 >> 8 & 0xff; float c2B = c2 & 0xff; color resultc = color( c1R-c2R, c1G-c2G, c1B-c2B ); return resultc; } color add( color c1, color c2 ){ float c1R = c1 >> 16 & 0xff; float c1G = c1 >> 8 & 0xff; float c1B = c1 & 0xff; float c2R = c2 >> 16 & 0xff; float c2G = c2 >> 8 & 0xff; float c2B = c2 & 0xff; color resultc = color( c1R+c2R, c1G+c2G, c1B+c2B ); return resultc; } }