Noise Walker Edge Detection

edited March 2016 in Questions about Code

Hey guys,

i've created a little random walker-sketch, but i have no idea how to tell the dot to stay inside the canvas. Any suggestions? Thank you !!!

PS: I'm also happy about any kind of advices for better syntax. :-)

Dot dot;

void setup() {
  size(100, 100);
  background(#222222);
  noStroke();
  dot = new Dot();
  fill(#eeeeee);
}

void draw() {
  dot.move();
  dot.display();
}



class Dot {

  int x = width/2;
  int y = height/2;
  float xoff =100;
  float yoff = 200;
  int xspeed;
  int yspeed;


  void move() {

    float thenoise1 = noise(xoff);  
    float thenoise2 = noise(yoff);  
    int stepx = int(map(thenoise1, 0, 1, 0, 3))-1; 
    int stepy = int(map(thenoise2, 0, 1, 0, 3))-1; 

    x = x + stepx;
    y = y + stepy;
    xoff = xoff + 0.1;
    yoff = yoff + 0.1;
  }

  void display() {
    ellipse(x, y, 2, 2);
  }
}

Answers

  • edited March 2016

    Is constrain() a good solution? Some thing like

    constrain(x,0,width);
    constrain(y,0,height);
    

    ?

    I could not make it work anyway...

  • Answer ✓

    Constrain will certainly do it. But there's a trick to using constrain - it doesn't actually reassign your variable's value! Instead, it returns a constrained value. Thus, you need to do the assignment yourself. Try adding these lines to your class, after changing the Dot's position:

    x = constrain(x,0,width);
    y = constrain(y,0,height);
    
  • Works perfect. Thank you!!!

  • Oh i see, there's another thing:

    My walker walks to the top left of the canvas, but i want it to have no walking direction-tendency. Any suggestions?

  • Make sure your stepx and stepy values are being generated so that, on average, they sum to 0. One way to ensure this is to generate them between 0 and some maximum, and then negate them half the time.

    Is there a reason you are using noise() to generate these values, instead of good-old random()?

  • Make sure your stepx and stepy values are being generated so that, on average, they sum to 0

    i just said this in another thread...multiple questions just confuses things.

Sign In or Register to comment.