/
vehicle.js
117 lines (102 loc) · 3.49 KB
/
vehicle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Daniel Shiffman
// https://www.kadenze.com/courses/the-nature-of-code
// http://natureofcode.com/
// Session 3: Separation and Seek
// The "Vehicle" constructor
// Create an Audio input
function Vehicle(x, y) {
// All the usual stuff
this.position = createVector(x, y);
this.r = 20;
this.maxspeed = 3; // Maximum speed
this.maxforce = 0.2; // Maximum steering force
this.acceleration = createVector(0, 0);
this.velocity = createVector(0, 0);
this.applyBehaviors = function(vehicles) {
var separateForce = this.separate(vehicles);
var seekForce = this.seek(createVector(mouseX,mouseY));
separateForce.mult(2);
seekForce.mult(1);
this.applyForce(separateForce);
this.applyForce(seekForce);
}
this.applyForce = function(force) {
// We could add mass here if we want A = F / M
this.acceleration.add(force);
}
// Separation
// Method checks for nearby vehicles and steers away
this.separate = function(vehicles) {
var vol = mic.getLevel();
if (vol < 0.01) {
var desiredseparation = 30 ;
} else {
var desiredseparation = 20 * (vol*300);
}
var sum = createVector();
var count = 0;
// For every boid in the system, check if it's too close
for (var i = 0; i < vehicles.length; i++) {
var d = p5.Vector.dist(this.position, vehicles[i].position);
// If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
if ((d > 0) && (d < desiredseparation)) {
// Calculate vector pointing away from neighbor
var diff = p5.Vector.sub(this.position, vehicles[i].position);
diff.normalize();
diff.div(d); // Weight by distance
sum.add(diff);
count++; // Keep track of how many
}
}
// Average -- divide by how many
if (count > 0) {
sum.div(count);
// Our desired vector is the average scaled to maximum speed
sum.normalize();
sum.mult(this.maxspeed);
// Implement Reynolds: Steering = Desired - Velocity
sum.sub(this.velocity);
sum.limit(this.maxforce);
}
return sum;
}
// A method that calculates a steering force towards a target
// STEER = DESIRED MINUS VELOCITY
this.seek = function(target) {
var desired = p5.Vector.sub(target,this.position); // A vector pointing from the location to the target
// Normalize desired and scale to maximum speed
desired.normalize();
desired.mult(this.maxspeed);
// Steering = Desired minus velocity
var steer = p5.Vector.sub(desired,this.velocity);
steer.limit(this.maxforce); // Limit to maximum steering force
return steer;
}
// Method to update location
this.update = function() {
// Update velocity
this.velocity.add(this.acceleration);
// Limit speed
this.velocity.limit(this.maxspeed);
this.position.add(this.velocity);
// Reset accelertion to 0 each cycle
this.acceleration.mult(0);
}
this.display = function() {
// fill(random(255), random(255), random(255));
noFill()
stroke(255);
strokeWeight(2);
push();
translate(this.position.x, this.position.y);
ellipse(0, 0, this.r, this.r);
pop();
}
// Wraparound
this.borders = function() {
if (this.position.x < -this.r) this.position.x = width+this.r;
if (this.position.y < -this.r) this.position.y = height+this.r;
if (this.position.x > width+this.r) this.position.x = -this.r;
if (this.position.y > height+this.r) this.position.y = -this.r;
}
}