-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboid.js
44 lines (38 loc) · 1.1 KB
/
boid.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
class Boid {
/*
* each of position, velocity, and acceleration must have the following
* structure:
*
* [x_component: Number, y_component: Number, z_component: Number]
*/
constructor(id, position, velocity) {
this.id = id;
this.position = position;
this.velocity = velocity;
this.initialVelocity = length(this.velocity);
this.mostRecentCellId = undefined;
// removed: color is now same for all boids
/*
this.modelColor = [
Math.random(),
Math.random(),
Math.random(),
1.0];
*/
}
doTimeStep() {
// keeps the boids moving. Not strictly necessary (`separation()` adds
// enough entropy to the system that it can keep moving) but it keeps
// things at a good pace
let speed = length(this.velocity);
if (speed < this.initialVelocity) {
this.velocity = scalarMultiply(this.velocity, 1.2);
}
this.position[0] += this.velocity[0];
this.position[1] += this.velocity[1];
this.position[2] += this.velocity[2];
}
applyForce(forceVector) {
this.velocity = add(this.velocity, forceVector);
}
}