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 › Issue with raindrops eroding
Page Index Toggle Pages: 1
Issue with raindrops eroding (Read 177 times)
Issue with raindrops eroding
Mar 5th, 2009, 7:16am
 
I am creating a small piece of code where i will be able to make raindrops fall on a building and when a raindrop hits the building i want to make a holes in the building. so i want it to erode. If you can see if i have started on the first building but i am stuck as i cant go past the first line.

test.pde

Drop[] drops = new Drop[1000];  // An array of drops
int totalDrops = 0;            // New variable to keep track of total number of drops we want to use!

int x = 600;
int y = 75;
int w = 100;
int h = 100;
boolean button = false;


void setup() {                       // this is called once at the start of the program
 size(700,330);                     // make a window 400,400
 smooth();                          // make the drawing smooth (anti-aliased)
}

void draw() {                        // this is called repeatedly as the program runs (e.g. 25 times per second)

frameRate(100);
if (button) {
  background(0);
  building();
  windows();
  rain();
  stroke(255);
} else {
  background(255);
  building();
  windows();  
  stroke(0);
}
  fill(175);
  ellipse(x,y,w,h);
  //ellipse(340,5,10,10);
}

void rain() {
 drops[totalDrops] = new Drop(); // Initialize one drop
 totalDrops++ ;                  // Increment totalDrops

// If we hit the end of the array
 if (totalDrops >= drops.length) {
   totalDrops = 0; //Start over
 }
 // Move and display drops
 for (int i = 0; i < totalDrops; i++ ) { // New! We no longer move and display all drops, but rather only the “totalDropsâ€&#65533; that are currently present in the game.
   drops[i].move();
   drops[i].display();
 }
}

void building() {

 fill(150);
 rect(38,166,154,168); //1st
 rect(248,150,20,180); //2nd left
 rect(408,150,20,180); //2nd right
 rect(256,70,162,80); //2nd mid-top
 rect(266,132,142,198); //2nd middle
 rect(266,58,142,12);  //2nd top thin
 triangle(408, 58, 340, 5, 268, 58); //2nd top
 rect(476,230,185,102); //3rd top
 rect(488,168,159,62); //3rd bottom
 //ellipse(340,5,10,10);
}

// When the mouse is pressed, the state of the button is toggled.  
// Try moving this code to draw() like in the rollover example.  What goes wrong?
void mousePressed() {
 if (mouseX > x-50 && mouseX < x+w-50 && mouseY > y-50 && mouseY < y+h-50) {
   button = !button;
}  
}
void windows() {

 for (int i=42; i < 178; i +=15){
   for (int j=315; j > 155; j -=20){
     fill(random(0),random(255),random(255));
     rect(i,j,10,15);
   }
 }
 for (int i=272; i < 400; i +=15){
   for (int j=315; j > 115; j -=20){
     fill(random(0),random(255),random(255));
     rect(i,j,10,15);
   }
 }
 
 for (int i=260; i < 402; i +=14){
   for (int j=113; j > 60; j -=20){
     fill(random(0),random(255),random(255));
     rect(i,j,10,15);
   }
 }
 
 for (int i=252; i < 415; i +=161){
   for (int j=315; j > 135; j -=20){
     fill(random(0),random(255),random(255));
     rect(i,j,10,15);
   }
 }
 
 for (int i=495; i < 645; i +=15){
   for (int j=210; j > 150; j -=20){
     fill(random(0),random(255),random(255));
     rect(i,j,10,15);
   }
 }
 for (int i=480; i < 650; i +=15){
   for (int j=315; j > 215; j -=20){
     fill(random(0),random(255),random(255));
     rect(i,j,10,15);
   }
 }
}

Drop.pde

class Drop {

 float x,y;   // Variables for location of raindrop
 float speed; // Speed of raindrop
 float r;     // Radius of raindrop

 Drop() {
   r = 4;                 // All raindrops are the same size
   x = random(width);     // Start with a random x location
   y = -r*2;              // Start a little above the window
   speed = random(1,5);   // Pick a random speed
 }

 // Move the raindrop down
 void move() {
   // Increment by speed
   y += speed;
 }

 // Check if it hits the bottom
 boolean reachedBottom() {
   // If we go a little beyond the bottom
   if (y > height + r*4) {
     return true;
   } else {
     return false;
   }
 }

 // Display the raindrop
 void display() {
   fill(50,100,150);
   noStroke();
   for (int i = 1; i < r; i++ ) {
     ellipse(x,y + i*4,i*2,i*2);
     // rect(38,166,154,168); //1st
   }
   if ((x > 38) && (x < 190) && (y > 166)) {
     y=166;
     fill(0);
     rect(x,y,7,10);
   }
 }
}

Thanks for any help in advance!
Re: Issue with raindrops eroding
Reply #1 - Mar 6th, 2009, 5:21pm
 
The problem, that you can't go past the first line results from this condition

if ((x > 38) && (x < 190) && (y > 166)) {
 y=166;
 ...
}

because as soon as y gets greater than 166 it cannot go further.

You need something additional to check if your building is already black at this point (with get()).

I've done a quick modification of the Drop class that might help you:

class Drop {

 float x,y;   // Variables for location of raindrop
 float speed; // Speed of raindrop
 float r;     // Radius of raindrop
 boolean fixed = false;

 Drop() {
   r = 4;       // All raindrops are the same size
   x = random(width);     // Start with a random x location
   y = -r*2;    // Start a little above the window
   speed = random(1,5);   // Pick a random speed
 }

 // Move the raindrop down
 void move() {
   // Increment by speed
   if (!fixed) {
     y += speed;  
   }
 }

 // Check if it hits the bottom
 boolean reachedBottom() {
   // If we go a little beyond the bottom
   if (y > height + r*4) {  
     return true;
   }
   else {
     return false;
   }
 }

 // Display the raindrop
 void display() {
   fill(50,100,150);
   noStroke();
   if (!fixed) {
     for (int i = 1; i < r; i++ ) {
       ellipse(x,y + i*4,i*2,i*2);
       // rect(38,166,154,168); //1st
     }
     if ((x > 38) && (x < 190) && (y > 166) && get(int(x), int(y)) > color(0)) {
       y -= 5;
       fixed = true;
     }
   }
   else {
       fill(0);
       rect(x,y,7,10);

   }
 }
}
Page Index Toggle Pages: 1