-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.js
117 lines (103 loc) · 3.63 KB
/
example.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
/**
* A .js script taken from chen0040 ( https://github.com/chen0040/js-simulator )
* our goal here is to mimic the behavior of a Physarum polycephalum using his multi agent system.
*/
var jssim = require('js-simulator');
var Boid = function(id, initial_x, initial_y, space, isPredator) {
var rank = 1;
jssim.SimEvent.call(this, rank);
this.id = id;
this.space = space;
this.space.updateAgent(this, initial_x, initial_y);
this.sight = 75;
this.speed = 12;
this.separation_space = 30;
this.velocity = new jssim.Vector2D(Math.random(), Math.random());
this.isPredator = isPredator;
this.border = 100;
};
Boid.prototype = Object.create(jssim.SimEvent);
Boid.prototype.update = function(deltaTime) {
var boids = this.space.findAllAgents();
var pos = this.space.getLocation(this.id);
if(this.isPredator) {
var prey = null;
var min_distance = 10000000;
for (var boidId in boids)
{
var boid = boids[boidId];
if(!boid.isPredator) {
var boid_pos = this.space.getLocation(boid.id);
var distance = pos.distance(boid_pos);
if(min_distance > distance){
min_distance = distance;
prey = boid;
}
}
}
if(prey != null) {
var prey_position = this.space.getLocation(prey.id);
this.velocity.x += prey_position.x - pos.x;
this.velocity.y += prey_position.y - pos.y;
}
} else {
for (var boidId in boids)
{
var boid = boids[boidId];
var boid_pos = this.space.getLocation(boid.id);
var distance = pos.distance(boid_pos);
if (boid != this && !boid.isPredator)
{
if (distance < this.separation_space)
{
// Separation
this.velocity.x += pos.x - boid_pos.x;
this.velocity.y += pos.y - boid_pos.y;
}
else if (distance < this.sight)
{
// Cohesion
this.velocity.x += (boid_pos.x - pos.x) * 0.05;
this.velocity.y += (boid_pos.y - pos.y) * 0.05;
}
if (distance < this.sight)
{
// Alignment
this.velocity.x += boid.velocity.x * 0.5;
this.velocity.y += boid.velocity.y * 0.5;
}
}
if (boid.isPredator && distance < this.sight)
{
// Avoid predators.
this.velocity.x += pos.x - boid_pos.x;
this.velocity.y += pos.y - boid_pos.y;
}
}
}
// check speed
var speed = this.velocity.length();
if(speed > this.speed) {
this.velocity.resize(this.speed);
}
pos.x += this.velocity.x;
pos.y += this.velocity.y;
// check boundary
var val = this.boundary - this.border;
if (pos.x < this.border) pos.x = this.boundary - this.border;
if (pos.y < this.border) pos.y = this.boundary - this.border;
if (pos.x > val) pos.x = this.border;
if (pos.y > val) pos.y = this.border;
console.log("boid [ " + this.id + "] is at (" + pos.x + ", " + pos.y + ") at time " + this.time);
};
var scheduler = new jssim.Scheduler();
scheduler.reset();
var space = new jssim.Space2D();
for(var i = 0; i < 15; ++i) {
var is_predator = i > 12;
var boid = new Boid(i, 0, 0, space, is_predator);
scheduler.scheduleRepeatingIn(boid, 1);
}
while(scheduler.current_time < 20) {
scheduler.update();
}