-
Notifications
You must be signed in to change notification settings - Fork 1
/
vehicle.js
185 lines (156 loc) · 4.58 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// The "Vehicle" class
class Vehicle {
constructor(x, y, dna = null) {
if (dna) {
this.maxspeed = dna.gene[0];
this.maxforce = dna.gene[1];
this.seekgood = dna.gene[2];
this.seekbad = dna.gene[3];
this.generation = dna.gene[4];
} else {
this.maxspeed = random(0.01, 0.3); //0.2;
this.maxforce = random(0.01, 0.1); //0.05;
this.seekgood = random(-2, 2);
this.seekbad = random(-2, 2);
this.generation = 1;
}
this.counter = 0;
this.velocity = p5.Vector.random2D();
this.obj_type = 'vehicle';
this.acceleration = createVector(0, 0);
this.pos = createVector(x, y);
this.r = 6;
this.health = 255;
this.sensors = this.assingSensors(100);
}
assingSensors(dist, ang = 3, freq = 21) {
let sensors = [];
// let ang = 3;
for (let i = -PI / ang; i < PI / ang; i += PI / freq) {
//Outhest loop
let v3 = p5.Vector.fromAngle(this.velocity.heading() + i);
v3.mult(1.5 * dist);
v3.add(this.pos);
point(v3.x, v3.y);
let s3 = new Sensor(createVector(v3.x, v3.y), 0, this, 1.5 * dist);
s3.angle = i;
sensors.push(s3);
//Outher loop
let v = p5.Vector.fromAngle(this.velocity.heading() + i);
v.mult(dist);
v.add(this.pos);
point(v.x, v.y);
let s = new Sensor(createVector(v.x, v.y), 0, this, dist);
s.angle = i;
sensors.push(s);
// Inner loop
let v2 = p5.Vector.fromAngle(this.velocity.heading() + i);
v2.mult(dist / 2);
v2.add(this.pos);
point(v2.x, v2.y);
let s2 = new Sensor(createVector(v2.x, v2.y), 0, this, dist / 2);
s2.angle = i;
sensors.push(s2);
}
return sensors;
}
// Method to update location
update(aging) {
constrain(this.maxforce, 0, 1);
constrain(this.maxspeed, 0, 1);
// Counter increase
this.counter++;
// Update velocity
this.velocity.add(this.acceleration);
// Limit speed
this.velocity.limit(this.maxspeed);
this.pos.add(this.velocity);
// Reset accelerationelertion to 0 each cycle
this.acceleration.mult(0);
// Slowdown in time
// this.velocity.mult(0.98);
// Die slowly
this.health -= aging;
// constrain(this.health, 0 , 255);
}
applyForce(force) {
// We could add mass here if we want A = F / M
this.acceleration.add(force);
}
// A method that calculates a steering force towards a target
// STEER = DESIRED MINUS VELOCITY
search(target) {
let found = false;
for (let sensor of this.sensors) {
if (sensor.state == 1) {
found = true;
if (target.typ == 'good') {
let seekforce = this.seek(sensor);
seekforce.mult(this.seekgood);
this.applyForce(seekforce);
} else {
let seekforce = this.seek(sensor);
seekforce.mult(this.seekbad);
this.applyForce(seekforce);
}
}
}
if (!found) {
// this.move(PI/3, random(0.1));
}
}
seek(target) {
// A vector pointing from the location to the target
var desired = p5.Vector.sub(target.pos, this.pos);
// Scale to maximum speed
desired.setMag(this.maxspeed);
// Steering = Desired minus velocity
var steer = p5.Vector.sub(desired, this.velocity);
steer.add(target.velocity); // Arrive Motion
steer.limit(this.maxforce); // Limit to maximum steering force
return steer;
// this.applyForce(steer);
}
display(target) {
// Draw a triangle rotated in the direction of velocity
var theta = this.velocity.heading() + PI / 2;
let clr = lerpColor(color(255, 0, 0), color(0, 255, 0), this.health / 255);
fill(clr);
// stroke(200);
// strokeWeight(2);
push();
translate(this.pos.x, this.pos.y);
rotate(theta);
beginShape();
vertex(0, -this.r * 2);
vertex(-this.r, this.r * 2);
vertex(this.r, this.r * 2);
endShape(CLOSE);
pop();
//Also show attached sensors
this.showSensors(target);
}
showSensors(target) {
for (let s of this.sensors) {
s.update(target);
if (showsensor_switch.checked()) {
s.display();
}
}
}
displayDNA() {
fill(200);
text('Max Speed: ' + this.maxspeed.toFixed(3), 10, 90);
text('Max Force: ' + this.maxforce.toFixed(3), 10, 105);
text('Seed Good: ' + this.seekgood.toFixed(3), 10, 120);
text('Seed Bad: ' + this.seekbad.toFixed(3), 10, 135);
}
move(direction, force) {
let dir = p5.Vector.fromAngle(direction);
dir.mult(force);
this.applyForce(dir);
}
copyVec(dna) {
return new Vehicle(this.pos.x, this.pos.y, dna);
}
}