Using Perlin Noise to follow my mouse

Hello,

I created a random walk using Perlin noise. Now when I click on my mouse I use the motion acceleration, but my goal is to let the 'line' come to the position of my mouse using the Perlin noise algorithm ( so it looks like the motion acceleration that I have now ). I've tried several things but none of them worked. I hope you guys can help me fix this.

My code so far:

float xoff1 = 0.0;
float xoff2 = 500;

ArrayList history = new ArrayList();
ArrayList<PVector> d = new ArrayList();

boolean isSpacePressed = false;

Mover mover;

void setup() {
  size(640, 360);
  background(255);
  stroke(0, 3);
  strokeWeight(1);
  mover = new Mover();
}

void draw() {
  if (mousePressed && (mouseButton == RIGHT)) {
    mover.followMouse();
  } else {
    mover.display();
  }
}

void keyPressed() {
  if (key == ' ') {
    isSpacePressed = true;
    stroke(255, 80);
    strokeWeight(1.5);
  }
}

void keyReleased() {
  isSpacePressed = false;
  stroke(0, 3);
  strokeWeight(1);
}

class Mover {
  PVector location;
  PVector velocity;
  PVector acceleration;
  float topspeed;
  float dist = 10;

  Mover() {
    location = new PVector (width/2, height/2);
    velocity = new PVector(0, 0);
    topspeed = 1;
  }
  void display() {
    if (isSpacePressed) {
      xoff1 -= 0.005;
      xoff2 -= 0.005;
    } else {
      xoff1 += 0.003;
      xoff2 += 0.003;
    }

    location.x = map(noise(xoff1), 0, 1, 0, width);
    location.y = map(noise(xoff2), 0, 1, 0, height);
    PVector d = new PVector(location.x, location.y);
    history.add(d);
    for (int i=0; i<history.size(); i++) {
      PVector v = (PVector) history.get(i);
      float join = i/history.size() + d.dist(v)/dist;
      if (join < random(5)) {
        line(d.x, d.y, v.x, v.y);
      }
    }
  }

  void followMouse() {
    PVector mouse = new PVector(mouseX, mouseY);
    PVector acceleration = PVector.sub(mouse, location);
    acceleration.normalize();
    acceleration.mult(0.2);
    velocity.add(acceleration);
    velocity.limit(topspeed);
    location.add(velocity);
    PVector d = new PVector(location.x, location.y);
    history.add(d);

    for (int i=0; i<history.size(); i++) {
      PVector v = (PVector) history.get(i);
      float join = i/history.size() + d.dist(v)/dist;
      if (join < random(2.9)) {
        line(d.x, d.y, v.x, v.y);
      }
    }
  }
}
Tagged:
Sign In or Register to comment.