Good morning!
I had a great idea the other day: what if I made a cool sand piling applet!
...
I soon realized that I wasn't the only one trying lol. It's amazing how much people want to make an applet like that. Unfortunatly, so very few source codes are available for processing (or I haven't been looking at the right places)
After a few hours of search, I finally found one on the web. But when I tried to run it from processing on my computer, it just didn't work. It might be because this person was using an old version of processing
I've been looking at the code with my brother and we have had no luck on figuring out what the problem was. Maybe one of you guys would be able to give us a hand on this
The working applet can be found at
http://umlautllama.com/projects/processing/snow/was made by Scott Lawrence (maybe some of you guys know him)
And here is the code
Code:
// snow
//
// Scott "Jerry" Lawrence
//
//http://umlautllama.com/projects/processing/snow/
// based on koenie's "sand" CA
//
// 2003-10-27
//global variables
int[][] world; // declaring an array of integer type
//END global variables
//setup() sets up the initial window settings
void setup() {
size(200, 200); // size
loadPixels();
world = new int[width][height]; //declare two data in the array: width and height
for( int i=0 ; i<width; i++ ) // sweeps the screen
for( int j=0 ; j<height; j++ )
world[ i ][ j ] = 0;
}//END setup()
void loop()
{
// snow comes out of the mouse when it is pressed
if( mousePressed )
{
int ccc = randomColor();
//constraining the random snow around the arrow of the mouse
for( int i=0 ; i<40 ; i++ )
world[constrain(mouseX + (int)random(30) - 15, 1, width-2)]
[constrain(mouseY + (int)random(30) - 15, 0, height-2)] = ccc; //assign random colour
}
// drop random snow
if( keyPressed && key == 'd' ) // drop the snow
{
for( int i=0 ; i<20 ; i++ )
world[constrain((int)random(width), 1, width-2)]
[constrain((int)random(20), 0, height-2)] = randomColor();
}
if( keyPressed && key == 'm' ) // check for snow melt button
{
for( int i=0 ; i<width ; i++ )
world[i][height-1] = 0;
}
if( keyPressed && key == 'a' ) // age the snow
{
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
{
// get the pixel
int p = world[i][j];
// get the colors
int r = (p >> 16) & 0x000000ff;
int g = (p >> 8 ) & 0x000000ff;
int b = (p ) & 0x000000ff;
// darken them
r = constrain( r-2, 0, 255 );
g = constrain( g-2, 0, 255 );
b = constrain( b-2, 0, 255 );
// reinstall them
world[i][j] = (r<<16) + (g<<8) + b;
}
}
// draw the screen
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
{
pixels[j*width+i] = world[i][j]; //pixel take all the values from the world variable
}
for (int i = 0; i < width; i++)
{
for (int j = height-2; j >= 0; j--)
{
if( world[i][j] != 0 )
{
if( !drop_south( i, j ) )
{
if( random(100) > 50.0 )
{
if ( !drop_southeast( i, j ))
drop_southwest( i, j );
} else {
if ( !drop_southwest( i, j ))
drop_southeast( i, j );
}
}
}
}
updatePixels();
}
if( !online && keyPressed && key == 'G' )
{
saveFrame();
}
}//END loop()
//grains of sand move south
boolean drop_south( int i, int j )
{//if pixel above is equal to 0, do this
if( world[i][j+1] == 0 )
{
world[i][j+1] = world[i][j]; //world[i][j] is giving data to world[i][j+1]
world[i][j] = 0; // resets value
return true;
}
return false;
}//END drop_south
//grains of sand move southeast if there is space under it
boolean drop_southeast( int i, int j )
{
if( i < 1 )
{
return false; // gets out of the function. If the program would register a 0, it would make an error cause it would be out of array
}
if( world[i-1][j+1] == 0 )
{
world[i-1][j+1] = world[i][j];
world[i][j] = 0;
return true;
}
return false;
}//END drop_southeast()
//grains of sand move west if there is space under it
boolean drop_southwest( int i, int j )
{
if( i > width-2 )
{
return false; // -2 is a safety net so that the value doesn't go over the array
}
if(world[i+1][j+1] == 0)
{
world[i+1][j+1] = world[i][j];
world[i][j] = 0;
return true;
}
return false;
}//END drop_southwest()
//randomColor assigns a value for the colour
int randomColor( )
{
int r = ( 200 + (int) random( 55 ) ) & 0x000000ff;
int g = ( 200 + (int) random( 55 ) ) & 0x000000ff;
int b = ( 200 + (int) random( 55 ) ) & 0x000000ff;
return( (r<<16) + (g<<8) + b ); // so that the total value fits with the value possible for a colour
}//END randomColor()
I was also wondering if any of you knew of any other sand pilling applets out there which are free to use and modify?
I think I will learn greatly from any input from you guys on this problem.
Thank you a whole bunch in advance