please note that this is only a small part of the code
full code is here:
see GitHub - nature-of-code/noc-examples-processing: Repository for example code from The Nature of Code book
please note that the examples are in folders and sub-folders and that some of the examples have three files (tabs) that belong together.
I made a small version for you:
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Flow Field Following
// Flow Field Following
// Via Reynolds: http://www.red3d.com/cwr/steer/FlowFollow.html
// Flowfield object
FlowField flowfield;
void setup() {
size(640, 360);
// Make a new flow field
flowfield = new FlowField();
}
void draw() {
background(255);
// Display the flowfield
flowfield.display();
}
// =========================
class FlowField {
PVector[][] field;
int cols, rows;
int resolution;
FlowField() {
resolution = 20;
cols = width / resolution;
rows = height / resolution;
field = new PVector[cols][rows];
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
field[i][j] = new PVector(1, 0);
}
}
}//constr
// Draw every vector
void display() {
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
drawVector(field[i][j], i*resolution, j*resolution, resolution-4);
}
}
}//
// Renders a vector object 'v' as an arrow and a position 'x,y'
void drawVector(PVector v, float x, float y, float scayl) {
pushMatrix();
float arrowsize = 4;
// Translate to position to render vector
translate(x, y);
stroke(0, 100);
// Call vector heading function to get direction (note that pointing to the right is a heading of 0) and rotate
rotate(v.heading2D());
// Calculate length of vector & scale it to be bigger or smaller if necessary
float len = v.mag()*scayl;
// Draw three lines to make an arrow (draw pointing up since we've rotate to the proper direction)
line(0, 0, len, 0);
//line(len,0,len-arrowsize,+arrowsize/2);
//line(len,0,len-arrowsize,-arrowsize/2);
popMatrix();
}
}
//
The LONG version from github
I think it’s noc-examples-processing/chp06_agents/NOC_6_04_Flow_Figures at master · nature-of-code/noc-examples-processing · GitHub or so
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Flow Field Following
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Flow Field Following
// Via Reynolds: http://www.red3d.com/cwr/steer/FlowFollow.html
// Using this variable to decide whether to draw all the stuff
boolean debug = true;
// Flowfield object
FlowField flowfield;
// An ArrayList of vehicles
ArrayList<Vehicle> vehicles;
void setup() {
size(640, 360);
// Make a new flow field with "resolution" of 16
flowfield = new FlowField(20);
vehicles = new ArrayList<Vehicle>();
// Make a whole bunch of vehicles with random maxspeed and maxforce values
for (int i = 0; i < 120; i++) {
vehicles.add(new Vehicle(new PVector(random(width), random(height)), random(2, 5), random(0.1, 0.5)));
}
}
void draw() {
background(255);
// Display the flowfield in "debug" mode
if (debug) flowfield.display();
// Tell all the vehicles to follow the flow field
for (Vehicle v : vehicles) {
v.follow(flowfield);
v.run();
}
// Instructions
fill(0);
text("Hit space bar to toggle debugging lines.\nClick the mouse to generate a new flow field.", 10, height-20);
}
void keyPressed() {
if (key == ' ') {
debug = !debug;
}
}
// Make a new flowfield
void mousePressed() {
flowfield.init();
}
//============================
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Flow Field Following
class Vehicle {
// The usual stuff
PVector position;
PVector velocity;
PVector acceleration;
float r;
float maxforce; // Maximum steering force
float maxspeed; // Maximum speed
Vehicle(PVector l, float ms, float mf) {
position = l.get();
r = 3.0;
maxspeed = ms;
maxforce = mf;
acceleration = new PVector(0, 0);
velocity = new PVector(0, 0);
}
public void run() {
update();
borders();
display();
}
// Implementing Reynolds' flow field following algorithm
// http://www.red3d.com/cwr/steer/FlowFollow.html
void follow(FlowField flow) {
// What is the vector at that spot in the flow field?
PVector desired = flow.lookup(position);
// Scale it up by maxspeed
desired.mult(maxspeed);
// Steering is desired minus velocity
PVector steer = PVector.sub(desired, velocity);
steer.limit(maxforce); // Limit to maximum steering force
applyForce(steer);
}
void applyForce(PVector force) {
// We could add mass here if we want A = F / M
acceleration.add(force);
}
// Method to update position
void update() {
// Update velocity
velocity.add(acceleration);
// Limit speed
velocity.limit(maxspeed);
position.add(velocity);
// Reset accelertion to 0 each cycle
acceleration.mult(0);
}
void display() {
// Draw a triangle rotated in the direction of velocity
float theta = velocity.heading2D() + radians(90);
fill(175);
stroke(0);
pushMatrix();
translate(position.x, position.y);
rotate(theta);
beginShape(TRIANGLES);
vertex(0, -r*2);
vertex(-r, r*2);
vertex(r, r*2);
endShape();
popMatrix();
}
// Wraparound
void borders() {
if (position.x < -r) position.x = width+r;
if (position.y < -r) position.y = height+r;
if (position.x > width+r) position.x = -r;
if (position.y > height+r) position.y = -r;
}
}
// =========================
class FlowField {
// A flow field is a two dimensional array of PVectors
PVector[][] field;
int cols, rows; // Columns and Rows
int resolution; // How large is each "cell" of the flow field
FlowField(int r) {
resolution = r;
// Determine the number of columns and rows based on sketch's width and height
cols = width/resolution;
rows = height/resolution;
field = new PVector[cols][rows];
init();
}
void init() {
// Reseed noise so we get a new flow field every time
noiseSeed((int)random(10000));
float xoff = 0;
for (int i = 0; i < cols; i++) {
float yoff = 0;
for (int j = 0; j < rows; j++) {
float theta = map(noise(xoff, yoff), 0, 1, 0, TWO_PI);
// Polar to cartesian coordinate transformation to get x and y components of the vector
field[i][j] = new PVector(cos(theta), sin(theta));
yoff += 0.1;
}
xoff += 0.1;
}
}
// Draw every vector
void display() {
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
drawVector(field[i][j], i*resolution, j*resolution, resolution-2);
}
}
}
// Renders a vector object 'v' as an arrow and a position 'x,y'
void drawVector(PVector v, float x, float y, float scayl) {
pushMatrix();
float arrowsize = 4;
// Translate to position to render vector
translate(x, y);
stroke(0, 100);
// Call vector heading function to get direction (note that pointing to the right is a heading of 0) and rotate
rotate(v.heading2D());
// Calculate length of vector & scale it to be bigger or smaller if necessary
float len = v.mag()*scayl;
// Draw three lines to make an arrow (draw pointing up since we've rotate to the proper direction)
line(0, 0, len, 0);
//line(len,0,len-arrowsize,+arrowsize/2);
//line(len,0,len-arrowsize,-arrowsize/2);
popMatrix();
}
PVector lookup(PVector lookup) {
int column = int(constrain(lookup.x/resolution, 0, cols-1));
int row = int(constrain(lookup.y/resolution, 0, rows-1));
return field[column][row].get();
}
}