Tuesday, May 19, 2009

SlinkyDancer



Been messing around with noise() a bit more. It's a really useful function!

ß e n


/**
* SlinkyDancer v1
*/

void setup()
{

size( 640, 480 ) ;
colorMode( RGB, 1.0 ) ;
smooth() ;
}

void draw()
{
background( 0.0 ) ;

float range = width / 2.0 ;
float t = millis() * 0.0005 ;

stroke( 0.2 ) ;

for ( int i = 0 ; i < 100 ; ++ i )
{
pushMatrix() ;

color colour = smoothColour( i/0.3, i/0.7, i/0.9 ) ;
fill( red( colour ), green( colour ), blue( colour ), 0.5 ) ;

translate( width/2, height/2 ) ;
translate( random_unit( t, range, 0, i ), random_unit( t, range, 1, i ) ) ;
rotate( random_unit( t, PI, 0, i ) ) ;
ellipse( 0, 0, random_unit( t, range, 2, i ), random_unit( t, range, 3, i ) ) ;

popMatrix() ;
}
}

float random_unit( float t, float scale, int index1, int index2 )
{
return 2.0 * scale * ( 0.5 - noise( t, index1, index2/100.0 ) ) ;
}

/**
* Generate a vector whose components change smoothly over time in the range [ 0, 1 ].
* Each component uses a sin() function to map the current time in milliseconds somewhere
* in the range [ 0, 1 ].A 'speed' factor is specified for each component.
*/
PVector smoothVector( float s1, float s2, float s3 )
{
float mills = 0.00003f * millis() ;

float x = 0.5f * sin( mills * s1 ) + 0.5f ;
float y = 0.5f * sin( mills * s2 ) + 0.5f ;
float z = 0.5f * sin( mills * s3 ) + 0.5f ;

return new PVector( x, y, z ) ;
}

/**
* Generate a colour which smoothly changes over time.
* The speed of each component is contolled by the parameters s1, s2 and s3.
*/
color smoothColour( float s1, float s2, float s3 )
{
PVector v = smoothVector( s1, s2, s3 ) ;

return color( v.x, v.y, v.z ) ;
}

1 comment:

monkstone said...

I'm getting some unexplained crashing of applets run from the ide (since I hooked up my installed java jdk rather than that supplied by processing.org, but it may be coincidental,could be any number of things). However if I export the code and run the application the problem seems to disappear. Like the SlinkyDancer.