xxxxxxxxxx
69
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Crowd Path Following
// Via Reynolds: http://www.red3d.com/cwr/steer/CrowdPath.html
// Using this variable to decide whether to draw all the stuff
let debug = true;
// A path object (series of connected points)
let path;
// Two vehicles
let vehicles = [];
function setup() {
createCanvas(640, 360);
// Call a function to generate new Path object
newPath();
// We are now making random vehicles and storing them in an ArrayList
for (let i = 0; i < 2; i++) {
newVehicle(random(width), random(height));
}
createP("Hit 'd' to toggle debugging lines.<br/>Click the mouse to generate new vehicles.");
}
function draw() {
background(240);
// Display the path
path.display();
for (let v of vehicles) {
// Path following and separation are worked on in this function
v.applyBehaviors(vehicles, path);
// Call the generic run method (update, borders, display, etc.)
v.run();
}
}
function newPath() {
// A path is a series of connected points
// A more sophisticated path might be a curve
path = new Path();
let offset = 30;
path.addPoint(offset, offset);
path.addPoint(width - offset, offset);
path.addPoint(width - offset, height - offset);
path.addPoint(width / 2, height - offset * 3);
path.addPoint(offset, height - offset);
}
function newVehicle(x,y) {
let maxspeed = random(4, 8);
let maxforce = 0.2;
vehicles.push(new Vehicle(x, y, maxspeed, maxforce));
}
function keyPressed() {
if (key == 'd') {
debug = !debug;
}
}
function mousePressed() {
newVehicle(mouseX, mouseY);
}