xxxxxxxxxx
187
let vehicles = [];
let numVehicles = 20;
let target;
let wanderTarget;
let image1;
function preload() {
image1 = loadImage("heart.jpeg");
}
function setup() {
createCanvas(400, 400);
for (let i = 0; i < numVehicles; i++) {
let vehicle = new Vehicle(random(width), random(height));
vehicles.push(vehicle);
}
target = createVector(width / 2, height / 2);
wanderTarget = createVector();
}
function draw() {
background(0);
// Update and display vehicles
for (let vehicle of vehicles) {
let seekForce = vehicle.seek(target);
let arrivalForce = vehicle.arrive(target);
let wanderForce = vehicle.wander();
let separationForce = vehicle.separate(vehicles);
// Apply forces to the vehicle
vehicle.applyForce(seekForce);
vehicle.applyForce(arrivalForce);
vehicle.applyForce(wanderForce);
vehicle.applyForce(separationForce);
vehicle.update();
vehicle.display();
vehicle.edges();
}
// Update the wander target for a smooth wandering motion
wanderTarget.add(p5.Vector.random2D().mult(10));
// Draw the target
fill(255, 255, 0);
noStroke();
image(image1, target.x - 50, target.y - 70, 100, 140);
}
class Vehicle {
constructor(x, y) {
this.position = createVector(x, y);
this.velocity = createVector();
this.acceleration = createVector();
this.maxSpeed = 3;
this.maxForce = 0.1;
this.size = 15; // Size of the diamond
this.fillColor = color(random(255), random(255), random(255));
}
//seeking
seek(target) {
let desired = p5.Vector.sub(target, this.position);
desired.setMag(this.maxSpeed);
let steer = p5.Vector.sub(desired, this.velocity);
steer.limit(this.maxForce);
return steer;
}
//arriving
arrive(target) {
let desired = p5.Vector.sub(target, this.position);
let d = desired.mag();
desired.setMag(this.maxSpeed);
// Modify the desired speed based on distance (slowing down as it gets closer)
if (d < 100) {
let m = map(d, 0, 100, 0, this.maxSpeed);
desired.setMag(m);
}
let steer = p5.Vector.sub(desired, this.velocity);
steer.limit(this.maxForce);
return steer;
}
//wandering
wander() {
let wanderR = 25; // Radius for the wander circle
let wanderD = 80; // Distance for the wander circle
let change = 0.3; // Rate of change of wander angle
// update wander angle
this.wanderTheta += random(-change, change);
// calculate the center of the image
let circleCenter = this.velocity.copy();
circleCenter.normalize();
circleCenter.mult(wanderD);
circleCenter.add(this.position);
let offset = p5.Vector.fromAngle(this.wanderTheta);
offset.mult(wanderR);
// calculate the target position
let target = p5.Vector.add(circleCenter, offset);
// seek the target position
let steer = p5.Vector.sub(target, this.position);
steer.setMag(this.maxForce);
return steer;
}
// separation
separate(vehicles) {
let desiredSeparation = 25; // Adjust as needed
let steer = createVector(0, 0);
let count = 0;
for (let other of vehicles) {
let d = p5.Vector.dist(this.position, other.position);
if (other !== this && d < desiredSeparation) {
let diff = p5.Vector.sub(this.position, other.position);
diff.normalize();
diff.div(d); // Weight by distance
steer.add(diff);
count++;
}
}
if (count > 0) {
steer.div(count);
}
if (steer.mag() > 0) {
steer.setMag(this.maxSpeed);
steer.sub(this.velocity);
steer.limit(this.maxForce);
}
return steer;
}
// Apply a force to the vehicle
applyForce(force) {
this.acceleration.add(force);
}
// Update the vehicle's position, velocity, and acceleration
update() {
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
this.acceleration.mult(0);
}
// Display the vehicle
display() {
let theta = this.velocity.heading() + PI / 2;
fill(this.fillColor);
stroke(0);
strokeWeight(1);
push();
translate(this.position.x, this.position.y);
rotate(theta);
beginShape();
vertex(0, -this.size * 3); // Top vertex
vertex(-this.size, 0); // Left vertex
vertex(0, this.size * 3); // Bottom vertex
vertex(this.size, 0); // Right vertex
endShape(CLOSE);
pop();
}
// Implement edge wrapping or other edge behaviors
edges() {
if (this.position.x > width + this.r) this.position.x = -this.r;
else if (this.position.x < -this.r) this.position.x = width + this.r;
if (this.position.y > height + this.r) this.position.y = -this.r;
else if (this.position.y < -this.r) this.position.y = height + this.r;
}
}