Skip to content

Commit

Permalink
exercise I.3
Browse files Browse the repository at this point in the history
  • Loading branch information
shiffman committed Jul 23, 2014
1 parent e2c3239 commit 2fb959f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
6 changes: 2 additions & 4 deletions chp6_agents/NOC_6_07_Separation/Vehicle.pde
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ class Vehicle {
}
// Average -- divide by how many
if (count > 0) {
sum.div(count);
// Our desired vector is the average scaled to maximum speed
sum.normalize();
sum.mult(maxspeed);
// Our desired vector is moving away maximum speed
sum.setMag(maxspeed);
// Implement Reynolds: Steering = Desired - Velocity
PVector steer = PVector.sub(sum, velocity);
steer.limit(maxforce);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com

Walker w;

void setup() {
size(640,360);
// Create a walker object
w = new Walker();
background(255);
}

void draw() {
// Run the walker object
w.step();
w.render();
}


49 changes: 49 additions & 0 deletions introduction/Exercise_I_3_Walker_To_Mouse/Walker.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com

// A random walker object!

class Walker {
int x, y;

Walker() {
x = width/2;
y = height/2;
}

void render() {
stroke(0);
strokeWeight(2);
point(x, y);
}

// Randomly move up, down, left, right, or stay in one place
void step() {

float r = random(1);
// A 50% of moving towards the mouse
if (r < 0.5) {
int xdir = (mouseX-x);
int ydir = (mouseY-y);
if (xdir != 0) {
xdir /= abs(xdir);
}
if (ydir != 0) {
ydir /= abs(ydir);
}
x += xdir;
y += ydir;
} else {
int xdir = int(random(-2, 2));
int ydir = int(random(-2, 2));
println(xdir);
x += xdir;
y += ydir;
}

x = constrain(x, 0, width-1);
y = constrain(y, 0, height-1);
}
}

0 comments on commit 2fb959f

Please sign in to comment.