This commit is contained in:
赵鑫 2022-12-11 03:19:59 +08:00
parent d1d5aeacfe
commit 2784cac35b

View File

@ -51,7 +51,7 @@ function 小车追逐() {
if (vehicle2.turn()) vehicle2.velocity = randomVector(random(2, 5)); if (vehicle2.turn()) vehicle2.velocity = randomVector(random(2, 5));
vehicle1.turn(); vehicle1.turn();
vehicle1.drawPath(); vehicle1.drawPath();
vehicle2.show(); vehicle2.show("point");
vehicle1.show(); vehicle1.show();
} }
@ -67,15 +67,14 @@ function randomVector(magnitude) {
class Vehicle { class Vehicle {
constructor(x, y, color) { constructor(x, y, color) {
this.path = [];
this.color = color; this.color = color;
this.position = createVector(x, y); this.position = createVector(x, y);
this.path = [];
this.pathes = [];
this.velocity = createVector(0, 0); this.velocity = createVector(0, 0);
this.heading = this.velocity.heading(); this.heading = this.velocity.heading();
this.acceleration = createVector(0, 0); this.acceleration = createVector(0, 0);
this.maxVelocity = 5; this.maxVelocity = 5;
this.maxAcceleration = 1; this.maxAcceleration = 0.25;
} }
seek(target, slowdownDistance = 100) { seek(target, slowdownDistance = 100) {
@ -144,12 +143,12 @@ class Vehicle {
while (x < 0 || x >= width) x = (x + width) % width; while (x < 0 || x >= width) x = (x + width) % width;
while (y < 0 || y >= height) y = (y + height) % height; while (y < 0 || y >= height) y = (y + height) % height;
const edgeCrossed = x != this.position.x || y != this.position.y; const edgeCrossed = x != this.position.x || y != this.position.y;
if (edgeCrossed) {
this.pathes.push(this.path);
this.path = [];
}
this.position.x = x; this.position.x = x;
this.position.y = y; this.position.y = y;
if (edgeCrossed) {
this.path.pop();
this.path.push(null);
}
return edgeCrossed; return edgeCrossed;
} }
@ -180,23 +179,28 @@ class Vehicle {
noFill(); noFill();
stroke(50); stroke(50);
strokeWeight(1); strokeWeight(1);
if (this.pathes.length >= 64) this.pathes.shift(); if (this.path.length >= 1024) this.path.shift();
[...this.pathes, this.path].forEach((path) => { beginShape();
if (path.length >= 1024) path.shift(); this.path.forEach((v) => {
beginShape(); if (!v) {
path.forEach((v) => vertex(v.x, v.y)); endShape();
endShape(); beginShape();
} else {
vertex(v.x, v.y);
}
}); });
endShape();
pop(); pop();
} }
show() { show(mode = "triangle") {
push(); push();
translate(this.position.x, this.position.y); translate(this.position.x, this.position.y);
rotate(this.heading); rotate(this.heading);
stroke(this.color); stroke(this.color);
fill(this.color); fill(this.color);
triangle(0, 0, -10, 2.5, -10, -2.5); if (mode == "point") circle(0, 0, 5);
else triangle(0, 0, -10, 2.5, -10, -2.5);
pop(); pop();
} }
} }