xxxxxxxxxx
71
class Vehicle {
constructor(x, y) {
this.acceleration = createVector(0, 0);
this.velocity = createVector(3, 3);
this.position = createVector(x, y);
this.r = 6;
this.maxspeed = 5;
this.maxforce = 0.35;
}
// Method to update location
update() {
// Update velocity
this.velocity.add(this.acceleration);
// Limit speed
this.velocity.limit(this.maxspeed);
this.position.add(this.velocity);
// Reset accelerationelertion to 0 each cycle
this.acceleration.mult(0);
}
boundaries(offset){
let desired=null;
if (this.position.x < offset) {
desired = createVector(this.maxspeed, this.velocity.y);
}
else if (this.position > width - offset) {
desired = createVector(-this.maxspeed, this.velocity.y)
}
if (this.position.y < offset) {
desired = createVector(this.velocity.x, this.maxspeed);
}
else if (this.position.y > height - offset) {
desired = createVector(this.velocity.x, -this.maxspeed)
}
if (desired !== null) {
desired.normalize();
desired.mult(this.maxspeed);
let steer = p5.Vector.sub (desired, this.velocity);
steer.limit (this.maxforce);
this.applyForce(steer);
}
}
applyForce(force) {
// We could add mass here if we want A = F / M
this.acceleration.add(force);
}
show() {
// Draw a triangle rotated in the direction of velocity
let angle = this.velocity.heading();
fill(127);
stroke(0);
strokeWeight(2);
push();
translate(this.position.x, this.position.y);
rotate(angle);
beginShape();
vertex(this.r * 2, 0);
vertex(-this.r * 2, -this.r);
vertex(-this.r * 2, this.r);
endShape(CLOSE);
pop();
}
}