-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboids.js
134 lines (105 loc) · 3.42 KB
/
boids.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
// Javascript implementation of Boids
// Requires http://raphaeljs.com/raphael.js
// Ben Dowling - Oct 2008
// www.coderholic.com
// Modified slightly for proceduralgraphics.blogspot.com
var Boid = function(x, y, size, maxVelocity) {
this.x = x;
this.y = y;
this.maxVelocity = maxVelocity;
this.xVelocity = 1;
this.yVelocity = -1;
}
Boid.prototype.move = function() {
this.x += this.xVelocity;
this.y += this.yVelocity;
}
Boid.prototype.constrain = function(width,height) {
var border = -100; // negative will allow boids to fly a bit offscreen
if(this.x <= border || this.x >= width - border) {
this.x -= this.xVelocity;
this.x = Math.max(this.x, border);
this.x = Math.min(this.x, width - border);
this.xVelocity = -this.xVelocity;
this.x += this.xVelocity;
}
if(this.y <= border || this.y >= height - border) {
this.y -= this.yVelocity;
this.y = Math.max(this.y, border);
this.y = Math.min(this.y, height - border);
this.yVelocity = -this.yVelocity;
this.y += this.yVelocity;
}
}
Boid.prototype.distance = function(boid) {
var distX = this.x - boid.x;
var distY = this.y - boid.y;
return Math.sqrt(distX * distX + distY * distY);
}
Boid.prototype.moveAway = function(boids, minDistance) {
var distanceX = 0;
var distanceY = 0;
var numClose = 0;
for(var i = 0; i < boids.length; i++) {
var boid = boids[i];
if(boid.x == this.x && boid.y == this.y) continue;
var distance = this.distance(boid)
if(distance < minDistance) {
numClose++;
var xdiff = (this.x - boid.x);
var ydiff = (this.y - boid.y);
if(xdiff >= 0) xdiff = Math.sqrt(minDistance) - xdiff;
else if(xdiff < 0) xdiff = -Math.sqrt(minDistance) - xdiff;
if(ydiff >= 0) ydiff = Math.sqrt(minDistance) - ydiff;
else if(ydiff < 0) ydiff = -Math.sqrt(minDistance) - ydiff;
distanceX += xdiff;
distanceY += ydiff;
boid = null;
}
}
if(numClose == 0) return;
this.xVelocity -= distanceX / 5;
this.yVelocity -= distanceY / 5;
}
Boid.prototype.moveCloser = function(boids, distance) {
if(boids.length < 1) return
var avgX = 0;
var avgY = 0;
for(var i = 0; i < boids.length; i++) {
var boid = boids[i];
if(boid.x == this.x && boid.y == this.y) continue;
if(this.distance(boid) > distance) continue;
avgX += (this.x - boid.x);
avgY += (this.y - boid.y);
boid = null;
}
avgX /= boids.length;
avgY /= boids.length;
distance = Math.sqrt((avgX * avgX) + (avgY * avgY)) * -1.0
if(distance == 0) return;
this.xVelocity= Math.min(this.xVelocity + (avgX / distance) * 0.15, this.maxVelocity)
this.yVelocity = Math.min(this.yVelocity + (avgY / distance) * 0.15, this.maxVelocity)
}
Boid.prototype.moveWith = function(boids, distance) {
if(boids.length < 1) return
// calculate the average velocity of the other boids
var avgX = 0;
var avgY = 0;
for(var i = 0; i < boids.length; i++) {
var boid = boids[i];
if(boid.x == this.x && boid.y == this.y) continue;
if(this.distance(boid) > distance) continue;
avgX += boid.xVelocity;
avgY += boid.yVelocity;
boid = null;
}
avgX /= boids.length;
avgY /= boids.length;
distance = Math.sqrt((avgX * avgX) + (avgY * avgY)) * 1.0
if(distance == 0) return;
this.xVelocity= Math.min(this.xVelocity + (avgX / distance) * 0.05, this.maxVelocity)
this.yVelocity = Math.min(this.yVelocity + (avgY / distance) * 0.05, this.maxVelocity)
}
Boid.prototype.getHeading = function() {
return Math.atan2(this.yVelocity, this.xVelocity);
}