We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexDiscussionExhibition › 'Remix' my sketch
Pages: 1 2 
'Remix' my sketch (Read 3878 times)
Re: 'Remix' my sketch
Reply #15 - Jun 20th, 2008, 4:43am
 
//An edit of the last one

//ne0ngrav1ty




import processing.opengl.*;  
import javax.media.opengl.*;  
import java.util.ArrayList;  

PGraphicsOpenGL pgl;  
GL gl;  

// list that holds circles  
ArrayList circles;  
// number of circles to create initially  
int circleCount = 20;  
// max and min size of circles  
float maxSize = 4;  
float minSize = 4;  
// max speed for circles  
float speed = 3;  
float center = 100;
float vxc = random(-speed/center, speed/center);  
float vyc = random(-speed/center, speed/center);  

// glow toggle  
boolean glow=true;  

//random movement toggle:
boolean randomBool = false;

void setup() {  
 size(300,300,OPENGL);  
 background(0);  
 noStroke();  
 smooth();  
 // fill the circles list with circle objects  
 circles = new ArrayList();  
 for (int i=0; i<circleCount; i++) {  
   // random color with opacity of 20 (weighted toward blues & greens)  
   color c = color(random(150), random(180), random(150),250);  
   // add the circle to circles list  
   circles.add(new Circle(random(width), random(height), random(maxSize - minSize) + minSize, c));  
 }  
}  


void bezier(int i) {
 // bezierpoints of a circle, change when it bounces
 Circle[] bp= new Circle[6];
 bp[0]= (Circle) circles.get(i);
 bp[1]= (Circle) circles.get((bp[0].to[0])%circleCount);
 bp[2]= (Circle) circles.get((bp[0].to[1])%circleCount);  
 bp[3]= (Circle) circles.get((bp[0].to[2])%circleCount);
 stroke(bp[0].c,20);
 strokeWeight(4);
 bezier(bp[0].x,bp[0].y,bp[1].x,bp[1].y,bp[2].x,bp[2].y,bp[3].x,bp[3].y);
}

void draw() {  
 //fill(0,10);  
 noStroke();  
 rect(0,0,width,height);  

 if (glow) {  
   glowStuff();  
 }  

 // draw lines connecting circles
 for (int i=1; i<circleCount; i++) {
   bezier(i);
 }  

 // loop through the circle objects in circles list  
 for (int i=0; i<circles.size(); i++) {  
   Circle circle = (Circle) circles.get(i);  
   // move circles  
   if(randomBool){
circle.moveRandomly();
   }  
   else {
circle.move();  
   }
   // draw circles  
   circle.display();  
 }


}  

class Circle {  
 // x and y position, radius, x and y velocities  
 float x, y, r, vx, vy;  
 float ax, ay; //just acceleration variables
 float damp; //this will act as friction  
 int step; //maximum step in random movement
 // circle color  
 color c;  
 int[] to = {  
   2,4,6   };  

 Circle(float _x, float _y, float _r, color _c) {  
   x = _x;    
   y = _y;    
   r = _r;    
   c = _c;  
   float z = random(10);
   if (z < 5) {
vx = random(-speed, speed);  
vy = random(-speed, speed);  
   }
   else {
vx = vxc;  
vy = vyc;  
x=width/2;
y=height/2;
   }

   damp = 0.9;
   step = 1;
 }  

 // draw circle  
 void display() {  
   fill(c,10);  
   ellipse(x, y, r*2, r*2);  
 }  

 // move circle  
 void move() {  
   // bounce against the sides  
   if (x - r < 0) {  
x = r + 1;  
vx *= -1;  
to[(int)random(3)]+=1;
   }    
   else if (x + r > width) {  
x = width - r - 1;  
vx *= -1;  
to[(int)random(3)]+=circleCount-1;
   }  
   if (y - r < 0) {  
y = r + 1;  
vy *= -1;  
to[(int)random(3)]+=2;
   }    
   else if (y + r > height) {  
y = height - r - 1;  
vy *= -1;  
to[(int)random(3)]+=circleCount-1;
   }  
  vy += .1;
 
   // add velocities to position  
   x += vx;  
   y += vy;  
 }  

 void moveRandomly(){
   //this is a slight variation from a Keith Peter's tutorial for Flash,  
   //wich ca be found here:http://www.bit-101.com/tutorials/elasticiy.html
   ax = random(-step, step);
   vx += ax;
   vx *= damp;
   x += vx;

   ay = random(-step, step);
   vy += ay;
   vy *= damp;
   y += vy;
 
   //i just copied this bit from the "move" function
   // bounce against the sides  
   if (x - r < 0) {  
x = r + 1;  
vx *= -1;  
to[(int)random(3)]+=1;
   }    
   else if (x + r > width) {  
x = width - r - 1;  
vx *= -1;  
to[(int)random(3)]+=circleCount-1;
   }  
   if (y - r < 0) {  
y = r + 1;  
vy *= -1;  
to[(int)random(3)]+=2;
   }    
   else if (y + r > height) {  
y = height - r - 1;  
vy *= -1;  
to[(int)random(3)]+=circleCount-1;

   }  
 
 }

}  
// opengl blending as seen here http://www.rui-m.com/p5/Boids/ ;)  
// Rui: i think i forgot to put in that Boids source that the additive blend is taken from a Robert Hodgin tutorial  
// wich can be found here: http://www.flight404.com/blog/?p=71 :)
void glowStuff(){  
 pgl = (PGraphicsOpenGL) g;  
 gl = pgl.beginGL();  
 gl.glDisable(GL.GL_DEPTH_TEST);  
 gl.glEnable(GL.GL_BLEND);  
 gl.glBlendFunc(GL.GL_SRC_ALPHA,GL.GL_ONE);  
 pgl.endGL();  
}  

void keyPressed() {  
 if (key == 'd'){  
   glow = !glow;  
 }  
 if(key == 'r' || key == 'R'){ // R is for Random
   randomBool = !randomBool;
 }
}
Re: 'Remix' my sketch
Reply #16 - Jul 15th, 2008, 9:07pm
 
just had a look at the first, last, and a few random ones in between... brilliant evolution and concept! It is a bit hard to follow the thread with such long posts! would be ace to have an archive where they can be viewed with downloadable source!
Re: 'Remix' my sketch
Reply #17 - Jul 15th, 2008, 9:31pm
 
I will put them up on a website soon so they'll be much easier to view. This turned out pretty cool!
Re: 'Remix' my sketch
Reply #18 - Jul 23rd, 2008, 6:54pm
 
Hi,

i'm a beginner in the world of processing with a bit of c++ background. The thing i'm wondering about in most of these sketches is de trail or afterglow that follows the balls and lines. I looked trough the code but i just can't find where this is handled. Could anyone explain this maybe?

I'm positive it is not the OPENGL.

thanks.
Re: 'Remix' my sketch
Reply #19 - Jul 23rd, 2008, 7:41pm
 
Hi Gungir. You will notice in the draw() function there is no background() call. So the framebuffer is never being fully cleared. Instead a rectangle is being drawn over the entire screen with an alpha of 10 (i.e. semitransparent, so fading out the a bit the previous frame).
Re: 'Remix' my sketch
Reply #20 - Jul 23rd, 2008, 7:56pm
 
Hi memo,

i indeed noticed the absence of the background(); that but i think i missed the part of the 10 alpha. It's a smart trick, thanks for clearing that up Smiley

Re: 'Remix' my sketch
Reply #21 - Jul 24th, 2008, 9:20am
 
Gungir,

I experimented with this effect, but what I noticed is that this technique will always leave a ghost image behind and never fades completely to black.  Does anyone know of a good technique to achieve a fading trail effect? My guess is that you'd have to check each pixel and set it closer to black (perhaps by decrementing the brightness?) until it reaches black.  In fact I think I'll try this and report back. Wink
Re: 'Remix' my sketch
Reply #22 - Jul 24th, 2008, 10:28am
 
I answered my own question.  Here is how to achieve a fading trail. Check out fade() in this sketch. Hope this helps someone. Sorry for interrupting the remixing!

class Ball{
 int x,y,diameter;
 float x_speed,y_speed;

 Ball(int x_in, int y_in, int diameter_in, float x_speed_in, float y_speed_in) {
   x = x_in;
   y = y_in;
   diameter = diameter_in;
   x_speed = x_speed_in;
   y_speed = y_speed_in;
 }

 void draw_ball() {
   ellipse(x, y, diameter ,diameter);
 }

 void move() {
   x += x_speed;
   y += y_speed;
   if ( (x + diameter/2 >= width) || (x - diameter/2 <= 0) )
     x_bounce();
   if ( (y + diameter/2 >= height) || (y - diameter/2 <= 0) )
     y_bounce();
 }

 void x_bounce() {
   x_speed = -x_speed;
 }

 void y_bounce() {
   y_speed = -y_speed;
 }
}

Ball my_ball;

void setup() {
 size(600,400);
 colorMode(HSB,255);
 noStroke();
 my_ball = new Ball(int(random(20,width-20)), int(random(20,height-20)),20, 2, 1);
 background(0);
}

void draw() {
 my_ball.draw_ball();
 my_ball.move();
 fade();
}

void fade() {
 float bright;
 loadPixels();
 for(int i=0; i < width*height; i++){
     //      bright = brightness(pixels[y*width+x]);
     bright = brightness(pixels[i]);
     if (bright > 0)
       bright-=5;
     pixels[i] = color( hue(pixels[i]), saturation(pixels[i]), bright);
 }
 updatePixels();
}
Pages: 1 2