xxxxxxxxxx
102
class Planes {
constructor(x, y, r) {
this.pos = createVector(x, y);
this.vel = p5.Vector.random2D();
this.acc = createVector(0, 0);
this.maxspeed = 4;
this.maxforce = 0.1;
this.r = r;
this.col = color(random(0, 360), 100, 100);
this.a = 0;
this.velA = 0.5;
this.accA = 0;
}
show() {
noStroke();
fill(this.col);
push();
translate(this.pos.x, this.pos.y);
this.a = this.vel.heading();
rotate(this.a);
triangle(0, 0, this.r * 2, this.r / 2, 0, this.r);
pop();
}
update() {
this.vel.add(this.acc);
this.pos.add(this.vel);
this.acc.set(0, 0);
}
edge() {
if (this.pos.x > width) {
this.pos.x = 0;
} else if (this.pos.x < 0) {
this.pos.x = width;
}
if (this.pos.y > height) {
this.pos.y = 0;
} else if (this.pos.y < 0) {
this.pos.y = height;
}
}
applyForce(force) {
this.acc.add(force);
}
/* chasing the target */
seek(target) {
let seeking = p5.Vector.sub(target, this.pos);
seeking.setMag(this.maxspeed);
seeking.sub(this.vel);
seeking.limit(this.maxforce);
return seeking;
}
/* get away from the target */
flee(target) {
return this.seek(target).mult(-0.02);
}
/* predict future plane position*/
pursue(plane) {
let target = plane.pos.copy();
let prediction = plane.vel.copy();
prediction.mult(10);
target.add(prediction);
fill(180, 100, 100);
circle(target.x, target.y, this.r);
return this.seek(target);
}
evade(plane) {
let pursuit = this.pursue(plane);
pursuit.mult(-0.5);
return pursuit;
}
}
class Target extends Planes {
constructor(x, y, r) {
super(x, y, r);
this.vel = p5.Vector.random2D();
this.vel.mult(5);
}
show() {
noStroke();
fill(350, 100, 100);
push();
translate(this.pos.x, this.pos.y);
circle(0, 0, this.r * 2);
pop();
}
}