This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FriendlyBoidOptimised.js
160 lines (138 loc) · 5.68 KB
/
FriendlyBoidOptimised.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
class FriendlyBoidOptimised {
constructor(x, y) {
this.position = createVector(x, y);
this.velocity = createVector(random(0, 1), random(0, 1));
this.trail = [];
for (let i = 0; i < 100; i++) {
this.trail.push(this.position.copy());
}
}
update(trail = false, update = true) {
if (!update) {
return;
}
let close_dx = 0;
let close_dy = 0;
let avg_dx = 0;
let avg_dy = 0;
let avg_x = 0;
let avg_y = 0;
let total_neighbours = 0;
let poi_avoid_dx = 0;
let poi_avoid_dy = 0;
let poi_attract_dx = 0;
let poi_attract_dy = 0;
let enemy_close_dx = 0;
let enemy_close_dy = 0;
for (let boid of friendly_boids) {
if (boid != this) {
if (dist(this.position.x, this.position.y, boid.position.x, boid.position.y) < avoidance_range) {
close_dx += this.position.x - boid.position.x;
close_dy += this.position.y - boid.position.y;
}
if (dist(this.position.x, this.position.y, boid.position.x, boid.position.y) < visible_range) {
avg_dx += boid.velocity.x;
avg_dy += boid.velocity.y;
avg_x += boid.position.x;
avg_y += boid.position.y;
total_neighbours++;
}
}
}
for (let poi of pointsOfInterest) {
if (dist(this.position.x, this.position.y, poi.position.x, poi.position.y) < poi_attraction_range) {
poi_attract_dx += poi.position.x - this.position.x;
poi_attract_dy += poi.position.y - this.position.y;
}
if (dist(this.position.x, this.position.y, poi.position.x, poi.position.y) < poi_protection_range) {
poi_avoid_dx += this.position.x - poi.position.x;
poi_avoid_dy += this.position.y - poi.position.y;
}
}
for (let boid of enemy_boids) {
if (dist(this.position.x, this.position.y, boid.position.x, boid.position.y) < enemy_avoidance_range) {
enemy_close_dx += (this.position.x - boid.position.x);
enemy_close_dy += (this.position.y - boid.position.y);
}
}
if (total_neighbours > 0) {
avg_dx /= total_neighbours;
avg_dy /= total_neighbours;
avg_x /= total_neighbours;
avg_y /= total_neighbours;
}
this.velocity.add(createVector(close_dx, close_dy).normalize().mult(avoidance_constant));
this.velocity.add(createVector(avg_dx, avg_dy).normalize().mult(alignment_constant));
this.velocity.add(createVector(avg_x - this.position.x, avg_y - this.position.y).normalize().mult(cohesion_constant));
this.velocity.add(createVector(poi_attract_dx, poi_attract_dy).normalize().mult(interest_constant));
this.velocity.add(createVector(poi_avoid_dx, poi_avoid_dy).normalize().mult(poi_avoidance_constant));
this.velocity.add(createVector(enemy_close_dx, enemy_close_dy).normalize().mult(enemy_avoidance_constant));
// Turn around if it's going out of bounds
if (this.position.x < margin) {
this.velocity.x += turn_constant;
} else if (this.position.x > width - margin) {
this.velocity.x -= turn_constant;
}
if (this.position.y < margin) {
this.velocity.y += turn_constant;
} else if (this.position.y > height - margin) {
this.velocity.y -= turn_constant;
}
// Limit speed
if (this.velocity.mag() > max_speed) {
this.velocity.normalize().mult(max_speed);
} else if (this.velocity.mag() < min_speed) {
this.velocity.normalize().mult(min_speed);
}
// Update the trail
if (trail) {
this.trail.shift();
this.trail.push(this.position.copy());
}
// Move the boid
this.position.add(this.velocity);
}
drawBoid(shape = "Triangle") {
// Draw the boid as a sphere with a line indicating its direction
if (shape == "Circle") {
stroke(0, 255, 0);
strokeWeight(15);
point(this.position.x, this.position.y);
} else {
// Draw the boid as a triangle
strokeWeight(0);
fill(0, 255, 0);
let angle = this.velocity.heading();
let a = createVector(0, -30).rotate(angle + PI / 2).add(this.position);
let b = createVector(10, 0).rotate(angle + PI / 2).add(this.position);
let c = createVector(-10, 0).rotate(angle + PI / 2).add(this.position);
triangle(a.x, a.y, b.x, b.y, c.x, c.y);
}
}
drawTrail() {
// Add a trail behind the boid
strokeWeight(5);
for (let i = 0; i < this.trail.length - 1; i++) {
let alpha = map(i, 0, this.trail.length, 0, 255);
stroke(alpha);
line(this.trail[i].x, this.trail[i].y, this.trail[i + 1].x, this.trail[i + 1].y);
}
}
drawAvoidanceRange() {
noFill();
stroke(255, 0, 0, 50);
strokeWeight(2);
ellipse(this.position.x, this.position.y, avoidance_range * 2);
}
drawVelocity() {
stroke(255, 255, 255);
strokeWeight(3);
line(this.position.x, this.position.y, this.position.x + (this.velocity.x * 5), this.position.y + (this.velocity.y * 5));
}
drawVisibleRange() {
noFill();
stroke(0, 255, 0, 50);
strokeWeight(2);
ellipse(this.position.x, this.position.y, visible_range * 2);
}
}