Monday, 10 January 2011

deciphering the code

As my code is very much finished I thought I'd post some of it up and explain what certain bits do.

void setup()
{
frameRate (30);
shoe = loadImage("shoe.png");
size(640, 480, OPENGL);
int counter =0;

hint(ENABLE_OPENGL_4X_SMOOTH);
hint(DISABLE_DEPTH_TEST); //These two bits help with making the sketch run faster

for(float x=0; x<8; x++)
{
for(float y=0; y<6; y++)
{
Nike particle = new Nike();

particle.target.set(x*random(70,95), y*random(70,95), 0); //Sets out a grid for the particles, which then fill that grid when drawn
particle.pos.set(x*random(70,95), y*random(70,95), 0);

particles[counter] = particle;
counter ++;
}

}

opencv = new OpenCV( this ); // Initialises the OpenCV object
opencv.capture( 160, 120 ); // Opens a video capture stream
}


That's the setup.

for(int i=0; i {
Nike particle = particles[i];
if(brightness(img.get((int)particle.pos.x/4, (int)particle.pos.y/4))>15)
{
particle.vel.add(random(-2.7, 2.7),random(-2.7, 2.7), 0);
particle.alph-=50;
}

if(brightness(img.get((int)particle.pos.x/4, (int)particle.pos.y/4))<15)
{
particle.vel.add(random(-0.2, 0.2),random(-0.2, 0.2), 0);
particle.alph+=0;
}


This bit of code, placed in the draw setup, tells the sketch to look for areas in the difference image where the brightness is more than 15, if it is more than 15 then it adds a random value from -2.7 to 2.7 in velocity to any particles that are in the area of brightness. It also takes away 50 from the alpha value of that particle.

As well as that, if the brightness is less than 15 then it adds only a slight velocity value, but then adds 0 to 'alph' ('alph' is set to 1) so that the particle slowly fades back in.

alph+=1;

if(alph>255)
{
alph=255;
}

if(alph<0)
{
alph=0;
}


This bit of code in the Nike class sets the 'alph' parameters so that the values never get so high/low that the particles wont disappear/reappear when their required actions are performed.

No comments:

Post a Comment