Saturday, 18 September 2010

day 17 - mouse-following psychedelic worm in Processing

Great work today! I managed to half figure-out how to do a proper echo effect in video using Processing. I had been cheating by having a rectangle with very low alpha value as the bottom layer, so when "void draw" runs there's always a shadow of the previous image left behind, essentially an echo effect. But this technique is very limiting. So I flexed my array muscles and got this together, creating an array that tracks the current position as [0] and continually shunts the previous 20 positions down the array, creating a tail. In this version I've also made the previous positions shrink and change colour, getting this pleasing psychedelic effect.

I say half figured-out because this works fine in grayscale but the colour implementation isn't quite a successful in making the transparency work. This is mainly because I'm being lazy and using "color" rather than just sending the RGBA values through as separate variables. I'd like to figure out how to manipulate the alpha in "color" separately.

So this version is a worm rather than an echo. Code below.



float x;
float y;
float easing = 0.05;

void setup() {
 size(400,400);
smooth();

}

float [] holdX = new float[20];
float [] holdY = new float[20];
color col = color(200,0,100,255);

void draw() {
  background(0);
  noStroke();
  fill(255);
  float targetX = mouseX;
  x += (targetX - x) * easing;
    float targetY = mouseY;
  y += (targetY - y) * easing;
 ellipse(x,y, 20, 20);
 for (int i=0; i<20; i++) {
   //fill((255-i*(255/20)), (255-i*(255/20)));
   fill(col - i*(col/20));
   ellipse(holdX[i],holdY[i], 20-i,20-i);
 }

 for (int i=19; i>=1; i--) {
   holdX[i] = holdX[i - 1];
   holdY[i] = holdY[i - 1];
 }
 holdX[0] = x;
 holdY[0] = y;
}

void mouseClicked() {
 for (int i=1; i<19; i++) {
   println(holdX[i]);
 }
}

No comments:

Post a Comment