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 & HelpPrograms › Rain effect
Page Index Toggle Pages: 1
Rain effect (Read 3069 times)
Rain effect
Jun 8th, 2010, 4:46am
 
Hi, as you will probably be able to tell I'm quite new to processing but I was hoping someone might be able to help me out. I'm trying to create a rain effect but I have so far only successfully got one drop falling :/


class Rain {
float r = random(600);
float y = 0;


void fall() {
 fill(255,100);
 rect(0,0,600,600);
 y = y + 7;
 fill(0,10,200);
 ellipse(r, y, 10, 10);

}}

Rain r1;

void setup() {
 size(600,600);
 background(255);
 smooth();
 noStroke();
 r1 = new Rain();
}

void draw(){
 r1.fall();
}


I want to somehow have them fall many at once and at random intervals. I tried using an array to achieve this but they fell all at once and also the rectangle that I used to fade out old ellipses/create movement interferes with other drops. I do realize this is probably a very basic thing but I just can't figure it out, can anyone help me out? Smiley
Re: Rain effect
Reply #1 - Jun 8th, 2010, 4:56am
 
I would move the fading rectangle call to draw() instead, so you get it once per frame for all rain drops.
Also you can try and create the drops in draw(), adding a new one to the array only if random(10) < 1 for example (ie. roughly one new drop every 10 frames, adjust as you want).
Re: Rain effect
Reply #2 - Jun 8th, 2010, 6:56am
 
Wow thanks! That was really fast and helpful Cheesy
I now have multiple drops working together.


class Rain {
float r = random(600);
float y = 0;


void fall() {

 y = y + 7;
 fill(0,10,200,180);
 ellipse(r, y, 10, 10);

}}

Rain r1;

int numDrops = 10;
Rain[] drops = new Rain[numDrops]; // Declare and create the array

void setup() {
 size(600,600);
 background(255);
 smooth();
 noStroke();
 //Loop through array to create each object
 for (int i = 0; i < drops.length; i++) {

   drops[i] = new Rain(); // Create each object
   r1 = new Rain();
 }
}

void draw(){
  fill(255,80);
 rect(0,0,600,600);
 //Loop through array to use objects.
 for (int i = 0; i < drops.length; i++) {
 
   drops[i].fall();
   
 }

}


I'm still not sure though how you can use the random within draw though - it just gives me error messages. Basically what I want it to end up like is a few raindrops on screen falling at any given time, as this is eventually going to be used to make a kind of 'avoid the raindrops' game. Any more help would be greatly appreciated ;;
Re: Rain effect
Reply #3 - Jun 8th, 2010, 7:14am
 
You dont need to put random in draw.
I would keep it where it is.

So what you do right now, is create one row of raindrops but at the same height. I added another random for the height, so they enter the screen at different times.
And recall the random functions when they left the screen so they reappear at the beginning at a different position.




Code:


Rain r1;

int numDrops = 40;
Rain[] drops = new Rain[numDrops]; // Declare and create the array

void setup() {
 size(600,600);
 background(255);
 smooth();
 noStroke();
 //Loop through array to create each object
 for (int i = 0; i < drops.length; i++) {

   drops[i] = new Rain(); // Create each object
   r1 = new Rain();
 }
}

void draw(){
 fill(255,80);
 rect(0,0,600,600);
 //Loop through array to use objects.
 for (int i = 0; i < drops.length; i++) {
   drops[i].fall();
 }

}

class Rain {
 float r = random(600);
 float y = random(-height);

 void fall() {
   y = y + 7;
   fill(0,10,200,180);
   ellipse(r, y, 10, 10);

  if(y>height){
  r = random(600);
  y = random(-200);
  }

 }
}


Re: Rain effect
Reply #4 - Jun 8th, 2010, 7:58am
 
Ahh Thanks!! That's perfect.. makes so much sense now. I only wish I could've thought of it myself;; loll
Re: Rain effect
Reply #5 - Jun 8th, 2010, 8:07am
 
Illustrating my idea: ([EDIT] haven't seen Cedric gave a better answer Smiley I give it anyway...)
Code:
class Rain {
 
float r = random(600);
float y = 0;

void fall() {

y = y + 7;
fill(0,10,200,180);
ellipse(r, y, 10, 10);

}

}

int numDrops = 0;
Rain[] drops = new Rain[20]; // Declare and create the array

void setup() {
size(600, 600);
background(255);
smooth();
noStroke();
drops[0] = new Rain(); // Create first object
}

void draw() {
if (numDrops == drops.length - 1) exit();
fill(255, 80);
rect(0, 0, 600, 600);
if (random(10) < 1) {
  drops[++numDrops] = new Rain(); // Create each object
}
//Loop through array to use objects.
for (int i = 0; i < numDrops; i++) {
  drops[i].fall();
}
}

Indeed, it ends brutally, and doesn't handle when the drops go out of the screen.
Re: Rain effect
Reply #6 - Jun 8th, 2010, 3:23pm
 
You can have a look at my code here...
http://www.openprocessing.org/visuals/?visualID=9299
Hope that helps...
Re: Rain effect
Reply #7 - Jun 8th, 2010, 7:40pm
 
Ohh actually I have been looking at it, its amazing haha. Just a bit beyond my level though unfortunately. Thanks for your help guys.. saved me a lot of stressing.
Page Index Toggle Pages: 1