maxro
YaBB Newbies
Offline
Posts: 14
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; } }