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.
IndexProgramming Questions & HelpSyntax Questions › Controlling individual objects
Page Index Toggle Pages: 1
Controlling individual objects (Read 593 times)
Controlling individual objects
Apr 20th, 2007, 1:13am
 
Hey all, I'm a newbie to processing and was wondering if you could help me with something...
I am looking to create a new randomly sized triangle every 60 frames or so (on top of those that are there) and have them drop their opacity gradually to 0 while new ones continue to be created.  Any suggestions? Thanks.
Re: Controlling individual objects
Reply #1 - Apr 20th, 2007, 2:55pm
 
Code:

Triangle [] t;
int playhead = 0;
void setup(){
size(200, 200);
smooth();
t = new Triangle[10];
for(int i = 0; i < t.length; i++){
t[i] = new Triangle(random(width), random(height), random(width), random(height), random(width), random(height));
t[i].alpha = random(255);
}
}
void draw(){
background(200);
for(int i = 0; i < t.length; i++){
t[i].draw();
if(t[i].alpha <= 0){
t[i] = new Triangle(random(width), random(height), random(width), random(height), random(width), random(height));
}
}
}
class Triangle{
float x0,y0,x1,y1,x2,y2;
float r,g,b,alpha;
Triangle(float x0, float y0, float x1, float y1, float x2, float y2){
this.x0 = x0;
this.y0 = y0;
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
alpha = 255f;
r = random(200, 255);
g = random(200, 255);
b = random(200, 255);
}
void draw(){
fill(r, g, b, alpha);
stroke(0, alpha);
triangle(x0, y0, x1, y1, x2, y2);
alpha -= 0.5;
}
}


I'm leaving it to you to figure out a way to get the new triangles to come in on top of the triangles on screen.

You're going to have to either manually move the new triangles to the beginning of the list, or perhaps sort the list of triangles based on their alpha value.
Re: Controlling individual objects
Reply #2 - Apr 25th, 2007, 12:53am
 
Works perfectly.  Thanks a ton.

Page Index Toggle Pages: 1