Current File : /home/jeshor13/11bsouth.com/VehiclesNOC/vehicle.js |
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// The "Vehicle" class
var Vehicle = function(x, y, c, z) {
this.acceleration = createVector(0, 0);
this.velocity = createVector(random(-5, 5), random(-5, 5));
this.position = createVector(x, y);
this.r = z+(2*noise(millis()));
this.maxspeed = 3;
this.maxforce = 0.15;
this.distantMate = round(random(0, v.length))
this.color = c * 85
// Method to update location
Vehicle.prototype.update = function() {
// 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);
};
Vehicle.prototype.applyForce = function(force) {
// We could add mass here if we want A = F / M
this.acceleration.add(force);
};
Vehicle.prototype.boundaries = function() {
var desired = null;
if (this.position.x < d) {
desired = createVector(this.maxspeed, this.velocity.y);
} else if (this.position.x > width - d) {
desired = createVector(-this.maxspeed, this.velocity.y);
}
if (this.position.y < d) {
desired = createVector(this.velocity.x, this.maxspeed);
} else if (this.position.y > height - d) {
desired = createVector(this.velocity.x, -this.maxspeed);
}
if (desired !== null) {
desired.normalize();
desired.mult(this.maxspeed);
var steer = p5.Vector.sub(desired, this.velocity);
steer.limit(this.maxforce);
this.applyForce(steer);
}
};
Vehicle.prototype.display = function() {
// Draw a triangle rotated in the direction of velocity
var theta = this.velocity.heading() + PI / 2;
fill(this.color);
stroke(200);
strokeWeight(1);
push();
translate(this.position.x, this.position.y);
rotate(theta);
beginShape();
vertex(0, -this.r * 2);
vertex(-this.r, this.r * 2);
vertex(this.r, this.r * 2);
endShape(CLOSE);
pop();
};
};