xxxxxxxxxx
132
let mapImg;
let flights = [];
let numFlights = 4; // 4 flights each way
let planeImg;
let paths = [];
// Coordinates for Bucharest, Dubai, and Abu Dhabi based on the map image
let bucharest = { x: 200, y: 180 };
let dubai = { x: 500, y: 430 };
let abuDhabi = { x: 500, y: 430 };
function preload() {
mapImg = loadImage("map.jpg");
planeImg = loadImage("plane.png");
}
function setup() {
createCanvas(windowWidth, windowHeight);
// Create flights from Bucharest -> Dubai -> Abu Dhabi and reverse
for (let i = 0; i < numFlights; i++) {
flights.push(
new Flight(
bucharest.x,
bucharest.y,
dubai.x,
dubai.y,
random(200, 800),
true
)
);
flights.push(
new Flight(
dubai.x,
dubai.y,
abuDhabi.x,
abuDhabi.y,
random(200, 800),
false
)
);
// For the return route (Abu Dhabi -> Dubai -> Bucharest)
flights.push(
new Flight(
abuDhabi.x,
abuDhabi.y,
dubai.x,
dubai.y,
random(200, 800),
true
)
);
flights.push(
new Flight(
dubai.x,
dubai.y,
bucharest.x,
bucharest.y,
random(200, 800),
false
)
);
}
}
function draw() {
// Display the map background image
image(mapImg, 0, 0, width, height);
// Draw traces (paths)
stroke(255, 100);
noFill();
for (let path of paths) {
beginShape();
for (let pos of path) {
vertex(pos.x, pos.y);
}
endShape();
}
// Update and display all the flights
for (let flight of flights) {
flight.update();
flight.display();
}
}
// Flight class to manage each flight's movement
class Flight {
constructor(x1, y1, x2, y2, delay, isFirstLeg) {
this.start = createVector(x1, y1);
this.end = createVector(x2, y2);
this.pos = createVector(x1, y1);
this.speed = random(0.001, 0.002); // Slower speed for each flight
this.t = 0; // Parameter for lerp
this.angle = atan2(this.end.y - this.start.y, this.end.x - this.start.x); // Calculate angle for rotation
this.path = []; // Store the trace of the plane
this.delay = delay; // Random delay for each flight
this.hasStarted = false; // Control when flight starts
this.isFirstLeg = isFirstLeg; // To determine if this is Bucharest -> Dubai or Dubai -> Abu Dhabi
}
update() {
// Only start moving after the delay has passed
if (frameCount > this.delay) {
this.hasStarted = true;
}
if (this.hasStarted) {
this.t += this.speed;
if (this.t > 1) {
this.t = 0; // Reset when reaching the end
}
this.pos = p5.Vector.lerp(this.start, this.end, this.t);
this.path.push(this.pos.copy()); // Add current position to path
paths.push(this.path);
}
}
display() {
if (this.hasStarted) {
push();
translate(this.pos.x, this.pos.y);
rotate(this.angle); // Rotate to face the destination
imageMode(CENTER);
image(planeImg, 0, 0, 40, 40); // Draw the plane image
pop();
}
}
}