// Random Walker (No Vectors) // Daniel Shiffman // The Nature of Code, Spring 2009 // A random walker class! class Walker { PVector loc; float objW=10; float objH=10; int mouseTrack = 0; int[] prevMo = new int[4]; float tx = 0; float ty = 0; Walker() { loc = new PVector(width,0); loc.x = random(0,width); loc.y= random(0,-height); } void render() { stroke(3); fill(255); ellipse(loc.x,loc.y,objW,objH); } // Randomly move up, down, left, right, or stay in one place void walk() { float velx = 3*(noise(tx) - 0.5); float vely = 2*(noise(ty)); ////get mouse input prevMo[mouseTrack] = mouseX; mouseTrack++; prevMo[mouseTrack] = mouseY; mouseTrack++; if (mouseTrack == 4){ mouseTrack = 0; } int xAxis = prevMo[2]-prevMo[0]; int yAxis = prevMo[3]-prevMo[1]; if(yAxis != 0&&xAxis != 0){ if(abs(loc.y-mouseY)<30&&abs(loc.x-mouseX)<30){ if(loc.ymouseX){ vely -= abs((mouseY-vely))/50.0; velx += abs((mouseX-velx))/50.0; } } } if(yAxis != 0&&xAxis != 0){ if(abs(loc.y-mouseY)<30&&abs(loc.x-mouseX)<30){ if(loc.y>mouseY&&loc.xmouseY&&loc.x>mouseX){ vely += abs((mouseY-vely))/50.0; velx += abs((mouseX-velx))/50.0; } } } if(xAxis != 0){ if(abs(loc.y-mouseY)<30&&abs(loc.x-mouseX)<30){ if(loc.y==mouseY&&loc.x>mouseX){ println("6"); velx += abs((mouseX-velx))/50.0; } } } if(yAxis != 0){ if(abs(loc.y-mouseY)<30&&abs(loc.x-mouseX)<30){ if(loc.y>mouseY&&loc.x==mouseX){ println("7"); vely += abs((mouseY-vely))/50.0; } } } PVector vel = new PVector(velx,vely); loc.add(vel); // Stay on the screen loc.x = constrain(loc.x,0,width-1); // loc.y = constrain(loc.y,0,height-1); if(loc.y>(height+objH)){ loc.y = 0-objH; loc.x = random(0,width); } tx += 0.02; ty += 0.01; } }