xxxxxxxxxx
52
// 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
let debug = true;
// Flowfield object
let flowfield;
// An ArrayList of vehicles
let vehicles = [];
function setup() {
let text = createP("Hit space bar to toggle debugging lines.<br>Click the mouse to generate a new flow field.");
text.position(10, 365);
createCanvas(640, 360);
// Make a new flow field with "resolution" of 16
flowfield = new FlowField(20);
// Make a whole bunch of vehicles with random maxspeed and maxforce values
for (let i = 0; i < 100; i++) {
vehicles.push(new Vehicle(random(width), random(height), random(2, 5), random(0.1, 0.5)));
}
}
function draw() {
background(0);
// Display the flowfield in "debug" mode
if (debug) flowfield.display();
// Tell all the vehicles to follow the flow field
for (let i = 0; i < vehicles.length; i++) {
vehicles[i].follow(flowfield);
vehicles[i].run();
}
}
function keyPressed() {
if (key == ' ') {
debug = !debug;
}
}
// Make a new flowfield
function mousePressed() {
flowfield.init();
}