Need some help about my code (pollen)

Hello everybody. I need some help about my code. I've made some dots moving around randomly and now I want them to be attracted slowly and follow my mouse when mouse is over the screen. I tried to mix some code but it dosen't work. can anyone help me what kind of code I need? I know it's a kind of mouseOver or something, but I dont know where and how I should put it. :-) thank you!

ps. I got my code in 2 tabs:


ArrayList pollenluft;

void setup() {
  size(1500, 800);
  pollenluft = new ArrayList();

  // Hassel
  for (int i=0; i<20; i++) {
    pollenluft.add(new Pollen(#E6332A, 10.0, 2.0));
  }

  // El
  for (int i=0; i<35; i++) {
    pollenluft.add(new Pollen(#D17BFF, 14.0, 3.0));
  }


  // Elm
  for (int i=0; i<8; i++) {
    pollenluft.add(new Pollen(#71D7FF, 14.0, 3.0));
  }

  // birk
  for (int i=0; i<300; i++) {
    pollenluft.add(new Pollen(#FF9100, 12.0, 2.0));
  }

    // Græs
  for (int i=0; i<30; i++) {
    pollenluft.add(new Pollen(#A8FF00, 12.0, 3.0));
  }

    // punke
  for (int i=0; i<15; i++) {
    pollenluft.add(new Pollen(#FFF599, 12.0, 3.0));
  }
}

void draw() {
  //background(0);
  noStroke();
  fill(0, 85);
  rect(0, 0, width, height);
  for (int i=0; i<pollenluft.size(); i++) {
    Pollen p = (Pollen)pollenluft.get(i);
    p.display();
    p.update();
  }
}   






class Pollen {
  float x, y;
  float xspeed, yspeed, aspeed;
  float diameter;
  float r,g,b,a;
  Pollen(color f, float d, float s) {  // Pollen(farve, diameter, hastighed)
    x = random(width);
    y = random(height);
    r = red(f);
    g = green(f);
    b = blue(f);
    a = alpha(f);
    diameter = d;
    xspeed = random(-s, s);
    yspeed = random(-s, s);
    aspeed = random(0.01, 0.05);

  }
  void display() {
    noStroke();
    fill(r, g, b, a);
    ellipse(x, y, diameter, diameter);
  }
  void update() {
    x = x + xspeed;
    y = y + yspeed;
    if (x > width || x < 0) {
      xspeed = -xspeed;
    }
    if (y > height || y < 0) {
      yspeed = -yspeed;
    }
    a = map(sin(frameCount * aspeed), -1, 1, 0, 255);
  }
}
Tagged:

Answers

Sign In or Register to comment.