Files
processing4/java/examples/Books/Nature of Code/chp6_agents/FlowfieldImage/FlowfieldImage.pde
2013-04-30 22:03:23 -04:00

60 lines
1.3 KiB
Plaintext

// 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;
PImage img;
// Flowfield object
FlowField flowfield;
// An ArrayList of vehicles
ArrayList<Vehicle> vehicles;
void setup() {
size(600, 568);
img = loadImage("sil.jpg");
// 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);
image(img,0,0);
// 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();
}