// smoke2 by Glen Murphy. // View the applet in use at http://bodytag.org/ // Code has not been optimised, and will run fairly slowly. int WIDTH = 300; int HEIGHT = 300; int RES = 2; int PENSIZE = 30; int lwidth = WIDTH/RES; int lheight = HEIGHT/RES; int PNUM = 30000; vsquare[][] v = new vsquare[lwidth+1][lheight+1]; vbuffer[][] vbuf = new vbuffer[lwidth+1][lheight+1]; particle[] p = new particle[PNUM]; int pcount = 0; int mouseXvel = 0; int mouseYvel = 0; int randomGust = 0; int randomGustMax; float randomGustX; float randomGustY; float randomGustSize; float randomGustXvel; float randomGustYvel; class particle { float x; float y; float xvel; float yvel; float temp; int pos; particle(float xIn, float yIn) { x = xIn; y = yIn; } void reposition() { x = WIDTH/2+random(-20,20); y = random(HEIGHT-10,HEIGHT); xvel = random(-1,1); yvel = random(-1,1); } void updatepos() { int vi = (int)(x/RES); int vu = (int)(y/RES); if(vi > 0 && vi < lwidth && vu > 0 && vu < lheight) { v[vi][vu].addcolour(2); float ax = (x%RES)/RES; float ay = (y%RES)/RES; xvel += (1-ax)*v[vi][vu].xvel*0.05; yvel += (1-ay)*v[vi][vu].yvel*0.05; xvel += ax*v[vi+1][vu].xvel*0.05; yvel += ax*v[vi+1][vu].yvel*0.05; xvel += ay*v[vi][vu+1].xvel*0.05; yvel += ay*v[vi][vu+1].yvel*0.05; v[vi][vu].yvel -= (1-ay)*0.003; v[vi+1][vu].yvel -= ax*0.003; //v[vi][vu+1].yvel -= ay*0.003; if(v[vi][vu].yvel < 0) v[vi][vu].yvel *= 1.00025; x += xvel; y += yvel; } else { reposition(); } if(random(0,400) < 1) reposition(); xvel *= 0.6; yvel *= 0.6; } } class vbuffer { int x; int y; float xvel; float yvel; float pressurex = 0; float pressurey = 0; float pressure = 0; vbuffer(int xIn,int yIn) { x = xIn; y = yIn; pressurex = 0; pressurey = 0; } void updatebuf(int i, int u) { if(i>0 && i0 && u0 && i0 && u 0) { adj = x - randomGustX; opp = y - randomGustY; dist = sqrt(opp*opp + adj*adj); if(dist < randomGustSize) { if(dist < RES*2) dist = randomGustSize; mod = randomGustSize/dist; xvel += (randomGustMax-randomGust)*randomGustXvel*mod; yvel += (randomGustMax-randomGust)*randomGustYvel*mod; } } xvel *= 0.99; yvel *= 0.98; } void addcolour(int amt) { col += amt; if(col > 196) col = 196; } void display(int i, int u) { float tcol = 0; if(i>0 && i0 && u WIDTH/2) randomGustXvel = random(-8,0); else randomGustXvel = random(0,8); randomGustYvel = random(-2,1); } randomGust--; } for(int i = 0; i < lwidth; i++) { for(int u = 0; u < lheight; u++) { vbuf[i][u].updatebuf(i,u); v[i][u].col = 0; } } for(int i = 0; i < PNUM-1; i++) { p[i].updatepos(); } for(int i = 0; i < lwidth; i++) { for(int u = 0; u < lheight; u++) { v[i][u].addbuffer(i, u); v[i][u].updatevels(mouseXvel, mouseYvel); v[i][u].display(i, u); } } randomGust = 0; }