xxxxxxxxxx
46
let vehicles = [];
function getRandomElementDifferentFromCurrent(arr, currentIndex) {
// Make a copy of the original array
const copiedArray = [arr];
// Remove the element at the current index from the copied array
copiedArray.splice(currentIndex, 1);
// Generate a random index within the bounds of the copied array
const randomIndex = Math.floor(Math.random() * copiedArray.length);
// Access the random element from the copied array
const randomElement = copiedArray[randomIndex];
return randomElement;
}
function setup() {
createCanvas(windowWidth, windowHeight);
for (let i=0; i<5; i++) {
let vehicle = new Vehicle(random(windowWidth), random(windowHeight));
vehicles.push(vehicle);
}
}
function draw() {
background(255, 255, 255);
for (let i=0; i<vehicles.length; i++) {
let vehicle = vehicles[i];
let action = random(1, 6);
let target = getRandomElementDifferentFromCurrent(vehicles, i);
switch (action) {
case 1:
vehicle.wander();
break;
default:
let steering4 = vehicle.evade(target);
vehicle.applyForce(steering4);
break;
}
vehicle.update();
vehicle.show();
vehicle.edges();
}
}