import processing.opengl.*; import javax.media.opengl.GL; GL gl; ModeTimer count=new ModeTimer(1,100); ModeTimer av=new ModeTimer(0,TWO_PI); ModeTimer dv=new ModeTimer(1,8); void setup(){ size(640,480,OPENGL); frameRate(60); particles=new ArrayList(); gl=((PGraphicsOpenGL)g).gl; colorMode(HSB,255); background(0); } float gravity=0.01; int px=320; int py=320; void draw(){ gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); gl.glBlendEquation(GL.GL_FUNC_ADD); fill(0); rect(0,0,width,height); //text("Things: "+particles.size()+" adding "+count.get()+" per frame",32,32); px+=dv.get()*cos(av.get()); py+=dv.get()*sin(av.get()); if(px>width) px=0; if(px<0) px=width; if(py>height) py=0; if(py<0) py=height; for(int i=0;itimeOut){ current=random(mn,mx); timeOut=frameCount+(int)random(200); } return current; } } ArrayList particles; class Particle{ float x,y; float vx,vy; float life; float c; Particle(float _x,float _y, float _vx,float _vy){ x=_x; y=_y; vx=_vx; vy=_vy; life=sqrt(vx*vx+vy*vy)*40; c=frameCount%100; } void draw(){ x+=vx; y+=vy; vy+=gravity; gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE); stroke(c,100,100*life/320); line(x,y,x-vx,y-vy); life--; if(life<0) particles.remove(particles.indexOf(this)); //delete else for(int i=0;i<10;i++) this.attractTo((Particle)particles.get((int)random(particles.size()))); } void attractTo(Particle p){ if(p.x==x || p.y==y) return; float d=dist(p.x,p.y,x,y); //distance //curve fit from http://statpages.org/nonlin.html float a=-1+0.059*d-0.00129*d*d; //attraction float ndvx=(x-p.x)/d; //normalised distance vector x float ndvy=(y-p.y)/d; //normalised distance vector y //println(d); x+=a*ndvx/200; y+=ndvy*a/200; //and the same for the other particle? } }