cmezak
YaBB Newbies
Offline
Posts: 4
Water simulation help
Feb 7th , 2007, 6:37pm
Hey folks, I've been playing with Processing for a while now, but I'm a largely self-taught programmer. I'm also a rather new programmer. Anyway, I'm playing with the water surface simulation that I've seen done so many places: http://bodytag.org/nav.php?u=tp02/ and http://www.neilwallis.com/java/water.html , for instance. I've written some code to do this, but it is running very slowly! Rather than run at screen resolution, I'm drawings rects to the canavas as pixels, but still it is slow. I was hoping that somebody could offer me some insight as to why this is happening. Here's the code: int[][] buffer1; int[][] buffer2; int holder; int numCells, cellWidth, cellHeight, up, down, left, right; void setup(){ size (400,400); colorMode(HSB,200); numCells = 50; buffer1 = new int[numCells][numCells]; buffer2 = new int[numCells][numCells]; cellWidth = width/numCells; cellHeight = height/numCells; for (int i = 0; i < width/cellWidth; i++) { for (int j = 0; j < height/cellWidth; j++) { buffer1[i][j] = 0; buffer2[i][j] = 0; } } } void draw(){ if (mousePressed) { buffer1[constrain(mouseX,0,width-1)/cellWidth][constrain(mouseY,0,height-1)/cellHeight] = 50; } for (int i = 0; i < width/cellWidth; i++) { for (int j = 0; j < height/cellWidth; j++) { up = buffer2[i][constrain(j-1,0,numCells-1)]; down = buffer2[i][constrain(j+1,0,numCells-1)]; left = buffer2[constrain(i-1,0,numCells-1)][j]; right = buffer2[constrain(i+1,0,numCells-1)][j]; buffer1[i][j] = (int)((up+down+left+right)/2.0 - buffer1[i][j]); buffer1[i][j] = int(buffer1[i][j] - buffer1[i][j]/1024.0); } } int maxdis = 0; //display for (int i = 0; i < width/cellWidth; i++) { for (int j = 0; j < height/cellWidth; j++) { if (buffer1[i][j] > maxdis) { maxdis = buffer1[i][j]; } fill(120,200,buffer1[i][j]*20+80); rect(i*cellWidth,j*cellHeight,cellWidth,cellHeight); } } println(maxdis); //switch buffers for (int i = 0; i < width/cellWidth; i++) { for (int j = 0; j < height/cellWidth; j++) { holder = buffer1[i][j]; buffer1[i][j] = buffer2[i][j]; buffer2[i][j] = holder; } } } Many thanks for any assistance! Charlie