xxxxxxxxxx
63
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Path Following
// Path is a just a straight line in this example
// Via Reynolds: // http://www.red3d.com/cwr/steer/PathFollow.html
// Using this variable to decide whether to draw all the stuff
let debug = false;
// A path object (series of connected points)
let path;
// Two vehicles
let vehicles = [];
function setup() {
let text = createP(
"Hit space bar to toggle debugging lines.<br>Click the mouse to generate a new path."
);
createCanvas(640, 240);
newPath();
// Each vehicle has different maxspeed and maxforce for demo purposes
for (let i = 0; i < 100; i++) {
vehicles.push(new Vehicle(random(width),height/2, random(2,8), random(0.1,1)))
}
}
function draw() {
background(255);
// Display the path
path.show();
for (let v of vehicles) {
// The boids follow the path
v.follow(path);
v.run();
v.borders(path);
}
}
function newPath() {
// A path is a series of connected points
// A more sophisticated path might be a curve
path = new Path();
path.addPoint(-20, height / 2);
path.addPoint(random(0, width / 2), random(0, height));
path.addPoint(random(width / 2, width), random(0, height));
path.addPoint(width + 20, height / 2);
}
function keyPressed() {
if (key == " ") {
debug = !debug;
}
}
function mousePressed() {
newPath();
}