We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
Is constrain() a good solution? Some thing like
?
I could not make it work anyway...
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:
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()?
i just said this in another thread...multiple questions just confuses things.