Can anyone help make my rain splash and puddle?

im new to coding and i cant quite figure it out. any sort of help would be awesome! :slight_smile:

int [] x = new int [1000];
float [] y = new float [1000];
float [] speed = new float [1000];
float [] size = new float [1000];
int shift = 15;
int start = 0;
PImage pic;
boolean click = false;

void setup(){
  size (1200, 800);
  surface.setResizable (true);
  noStroke ();
  
  import processing.sound.*;
  
  SoundFile file;
  
  file = new SoundFile(this, "rain-03.mp3");
  file.play();
  
  pic = loadImage ("treepic.jpg");
  for (int i = 0; i < x.length; i++){
    x [i] = i * shift;
    y [i] = random (start - 10, start + 10);
    speed [i] = random (1, 30);
    size [i] = random (5, 10);
  }
}

void draw(){
  pic.resize(width, height);
  image (pic, 0, 0);
  
  //fill (200);
  rainDrop();
  for(int i = 0; i < x.length; i++){
    y [i] = y [i] + speed [i];
    
    if (y [i] > height){
      y [i] = random (start - 10, start + 10);
    }
  }
}
  
 void rainDrop(){
   fill(200);
   for(int i = 0; i < x.length; i++){
     arc (x [i], y [i], size [i], size [i], 0, PI);
     triangle (x[i] - size [i]/2, y[i], x[i], y[i] - size[i], x[i] + size[i]/2, y[i]);
   }
 }
 
 void mouseClicked(){
   if (!click){
     for (int i = 0; i < x.length; i++){
       speed [i] = 0;
     }
   click = true;
 } else if (click){
   for(int i = 0; i < x.length; i++){
     speed [i] = size [i];
   }
   click = false;
 }
 }
1 Like

Hi,

  • Please format your code by pressing Ctrl+T in the Processing IDE code editor and use the </> button in the message editor on the forum.

  • What is your question? What is the problem your are facing at?

1 Like

it would probably be easier if you packed the raindrop into a class. as an example i’ve included a basic version with splashes below i didn’t add in the pooling of water but it shouldn’t be to difficult to do.

int rainDropCount;
RainDrop[] rainDrops;

void setup() {
  size(640, 480, P2D);
  noStroke();

  rainDropCount = 300;
  rainDrops = new RainDrop[rainDropCount];

  for (int i = 0; i < rainDropCount; i++) {
    RainDrop rd = new RainDrop();
    rd.pos.y = random(height);//cheat a bit here
    rd.addForce(0, rd.size);
    rainDrops[i] = rd;
  }
}

void draw() {
  background(51);//choo choo
  for (int i = 0; i < rainDropCount; i++) {
    RainDrop rd = rainDrops[i];
    rd.update();
    rd.present();
  }
}

//you could just use another raindrop instead of a new particle 
//but for clarity i think this is better
class Particle {
  PVector pos, vel, acc;
  float life, size;

  Particle(float x, float y) {
    this.pos = new PVector(x, y);
    this.vel = new PVector();
    this.acc = new PVector();
    this.life = random(.1, 1);
    this.size = random(1, 5);
  }

  void addForce(float x, float y) {
    acc.x += x;
    acc.y += y;
  }

  void update() {
    vel.add(acc);
    acc.mult(0);
    pos.add(vel);
    life *= .9;
  }

  void present() {
    fill(255, 255, 255, (int)(life * 255));
    //ellipse(pos.x, pos.y, size, size);
    rect(pos.x, pos.y, size, size);
  }
}

class RainDrop {
  PVector pos, vel, acc;
  ArrayList<Particle> particles;
  float size;
  int state;

  RainDrop() {
    this.pos = new PVector(random(width), random(-50, 50));
    this.vel = new PVector();
    this.acc = new PVector();
    this.size = random(5, 10);

    this.particles = new ArrayList<Particle>();
    this.state = 0;
  }

  void addForce(float x, float y) {
    acc.x += x;
    acc.y += y;
  }

  void update() {
    switch(state) {
    case 0:
      vel.add(acc);
      acc.mult(0);
      pos.add(vel);

      if (pos.y >= height) {
        state = 1;
        //generate some particles for splash
        int particleCount = (int)random(2, 5);
        for (int i = 0; i < particleCount; i++) {
          Particle p = new Particle(pos.x + random(-4, 4), height);
          p.addForce(random(-1, 1), random(-1, -.1));
          particles.add(p);
        }
      }
      break;
    case 1:
      for (int i = particles.size() - 1; i >= 0; i--) {
        Particle p = particles.get(i);

        p.update();

        if (p.life <= 0.1) {
          //this is dirty
          //should really mark dead and reuse etc
          particles.remove(i);
        }
      }

      if (particles.size() == 0) {
        //the raindrop has finished reset it
        this.pos.x = random(width);
        this.pos.y = random(-50, 0);
        this.vel.mult(0);
        this.acc.mult(0);
        this.size = random(5, 10);
    
        this.state = 0;  
        this.addForce(0, this.size);
      }
      break;
    }
  }

  void present() {
    switch(state) {
    case 0:
      fill(255, 255, 255, 64);
      arc(pos.x, pos.y, size, size, 0, PI);
      triangle (pos.x - size / 2, pos.y, pos.x, pos.y - size, pos.x + size / 2, pos.y);
      break;
    case 1:
      for (int i = 0; i < particles.size(); i++) {
        particles.get(i).present();
      }
      break;
    }
  }
}
3 Likes

thank you for being willing to help!
so would I have to make a class for both the raindrops and the particles (splashes)? or am I able to make a particle class and tie it into how I organized the code I currently have? it is for a project that I am doing and i’m just trying to understand the process of all the code and functions so I know how to adjust correctly.

Well, it’s better to do like this because Java is an object-oriented programming language so everything you code is much likely going to have classes inside.
So did you write the code or it was on internet? If so, what don’t you understand (functions, datatypes, algorithm…) ?

no i wrote the code. the only part that i got from the internet was how to incorporate sound. also, in my class we hadn’t learned how to organize stuff into a class, so I don’t really understand the background to that and what it does.