xxxxxxxxxx
205
// Wander (Displacement - From Paper)
// The Nature of Code
// The Coding Train / Daniel Shiffman
// https://youtu.be/ujsR2vcJlLk
// https://thecodingtrain.com/learning/nature-of-code/5.5-wander.html
// Main: https://editor.p5js.org/codingtrain/sketches/LVtVlS52Q
// With Sliders: https://editor.p5js.org/codingtrain/sketches/uxemh7FGc
// Deleting Positions: https://editor.p5js.org/codingtrain/sketches/EWHjy--Os
// 3D: https://editor.p5js.org/codingtrain/sketches/t6sFXmVrk
// Displacement: https://editor.p5js.org/codingtrain/sketches/VdHUvgHkm
// Perlin Noise: https://editor.p5js.org/codingtrain/sketches/XH2DtikuI
class Vehicle {
constructor(x, y) {
this.pos = createVector(x, y);
this.vel = createVector(1, 0);
this.acc = createVector(0, 0);
this.maxSpeed = 4;
this.maxForce = 0.2;
this.r = 16;
this.wanderTheta = PI / 2;
this.wanderDistance = 100;
this.wanderRadius = 50;
this.wanderPoint = this.vel.copy().setMag(this.wanderDistance);
this.wanderPoint.add(this.pos);
this.target = this.wanderPoint.copy();
this.target.add(this.wanderRadius, 0);
this.currentPath = [];
this.paths = [this.currentPath];
}
wander() {
// Move wander point
this.wanderPoint = this.vel.copy().setMag(this.wanderDistance);
this.wanderPoint.add(this.pos);
// Move target
let offset = 32;
let vOffset = p5.Vector.random2D().setMag(offset * 0.2);
this.target.add(vOffset);
let v = p5.Vector.sub(this.target, this.wanderPoint);
v.setMag(this.wanderRadius);
this.target = p5.Vector.add(this.wanderPoint, v);
let center = this.wanderPoint.copy();
stroke(255);
noFill();
circle(center.x, center.y, this.wanderRadius * 2);
stroke(255);
line(this.pos.x, this.pos.y, center.x, center.y);
stroke(0, 255, 0);
fill(0, 255, 0, 100);
circle(this.target.x, this.target.y, offset);
fill(0, 255, 0);
circle(this.target.x, this.target.y, 8);
stroke(255);
line(this.pos.x, this.pos.y, this.target.x, this.target.y);
noStroke();
fill(255, 0, 0);
circle(center.x, center.y, 8);
let force = p5.Vector.sub(this.target, this.pos);
force.setMag(this.maxForce);
this.applyForce(force);
//noLoop();
}
evade(vehicle) {
let pursuit = this.pursue(vehicle);
pursuit.mult(-1);
return pursuit;
}
pursue(vehicle) {
let target = vehicle.pos.copy();
let prediction = vehicle.vel.copy();
prediction.mult(10);
target.add(prediction);
fill(0, 255, 0);
circle(target.x, target.y, 16);
return this.seek(target);
}
arrive(target) {
// 2nd argument true enables the arrival behavior
return this.seek(target, true);
}
flee(target) {
return this.seek(target).mult(-1);
}
seek(target, arrival = false) {
let force = p5.Vector.sub(target, this.pos);
let desiredSpeed = this.maxSpeed;
if (arrival) {
let slowRadius = 100;
let distance = force.mag();
if (distance < slowRadius) {
desiredSpeed = map(distance, 0, slowRadius, 0, this.maxSpeed);
}
}
force.setMag(desiredSpeed);
force.sub(this.vel);
force.limit(this.maxForce);
return force;
}
applyForce(force) {
this.acc.add(force);
}
update() {
this.vel.add(this.acc);
this.vel.limit(this.maxSpeed);
this.pos.add(this.vel);
this.acc.set(0, 0);
this.currentPath.push(this.pos.copy());
// Count positions
let total = 0;
for (let path of this.paths) {
total += path.length;
}
if (total > 200 || (total > 10 && millis() > 3000)) {
this.paths[0].shift();
if (this.paths[0].length === 0) {
this.paths.shift();
}
}
}
show() {
stroke(255);
strokeWeight(2);
fill(255);
push();
translate(this.pos.x, this.pos.y);
rotate(this.vel.heading());
triangle(-this.r, -this.r / 2, -this.r, this.r / 2, this.r, 0);
pop();
for (let path of this.paths) {
beginShape();
noFill();
for (let v of path) {
vertex(v.x, v.y);
}
endShape();
}
}
edges() {
let hitEdge = false;
if (this.pos.x > width + this.r) {
this.pos.x = -this.r;
hitEdge = true;
} else if (this.pos.x < -this.r) {
this.pos.x = width + this.r;
hitEdge = true;
}
if (this.pos.y > height + this.r) {
this.pos.y = -this.r;
hitEdge = true;
} else if (this.pos.y < -this.r) {
this.pos.y = height + this.r;
hitEdge = true;
}
if (hitEdge) {
this.currentPath = [];
this.paths.push(this.currentPath);
}
}
}
class Target extends Vehicle {
constructor(x, y) {
super(x, y);
this.vel = p5.Vector.random2D();
this.vel.mult(5);
}
show() {
stroke(255);
strokeWeight(2);
fill("#F063A4");
push();
translate(this.pos.x, this.pos.y);
circle(0, 0, this.r * 2);
pop();
}
}