forked from hughsk/boids
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
170 lines (151 loc) · 5.37 KB
/
index.js
File metadata and controls
170 lines (151 loc) · 5.37 KB
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
var EventEmitter = require('events').EventEmitter
, inherits = require('inherits')
, POSITIONX = 0
, POSITIONY = 1
, SPEEDX = 2
, SPEEDY = 3
, ACCELERATIONX = 4
, ACCELERATIONY = 5
module.exports = Boids
function Boids(opts, callback) {
if (!(this instanceof Boids)) return new Boids(opts, callback)
EventEmitter.call(this)
opts = opts || {}
callback = callback || function(){}
this.speedLimitRoot = opts.speedLimit || 0
this.accelerationLimitRoot = opts.accelerationLimit || 1
this.speedLimit = Math.pow(this.speedLimitRoot, 2)
this.accelerationLimit = Math.pow(this.accelerationLimitRoot, 2)
this.separationDistance = Math.pow(opts.separationDistance || 60, 2)
this.alignmentDistance = Math.pow(opts.alignmentDistance || 180, 2)
this.cohesionDistance = Math.pow(opts.cohesionDistance || 180, 2)
this.separationForce = opts.separationForce || 0.15
this.cohesionForce = opts.cohesionForce || 0.1
this.alignmentForce = opts.alignmentForce || opts.alignment || 0.25
this.attractors = opts.attractors || []
var boids = this.boids = []
for (var i = 0, l = opts.boids === undefined ? 50 : opts.boids; i < l; i += 1) {
boids[i] = [
Math.random()*25, Math.random()*25 // position
, 0, 0 // speed
, 0, 0 // acceleration
]
}
this.on('tick', function() {
callback(boids)
})
}
inherits(Boids, EventEmitter)
Boids.prototype.tick = function() {
var boids = this.boids
, sepDist = this.separationDistance
, sepForce = this.separationForce
, cohDist = this.cohesionDistance
, cohForce = this.cohesionForce
, aliDist = this.alignmentDistance
, aliForce = this.alignmentForce
, speedLimit = this.speedLimit
, accelerationLimit = this.accelerationLimit
, accelerationLimitRoot = this.accelerationLimitRoot
, speedLimitRoot = this.speedLimitRoot
, size = boids.length
, current = size
, sforceX, sforceY
, cforceX, cforceY
, aforceX, aforceY
, spareX, spareY
, attractors = this.attractors
, attractorCount = attractors.length
, attractor
, distSquared
, currPos
, length
, target
, ratio
while (current--) {
sforceX = 0; sforceY = 0
cforceX = 0; cforceY = 0
aforceX = 0; aforceY = 0
currPos = boids[current]
// Attractors
target = attractorCount
while (target--) {
attractor = attractors[target]
spareX = currPos[0] - attractor[0]
spareY = currPos[1] - attractor[1]
distSquared = spareX*spareX + spareY*spareY
if (distSquared < attractor[2]*attractor[2]) {
length = hypot(spareX, spareY)
boids[current][SPEEDX] -= (attractor[3] * spareX / length) || 0
boids[current][SPEEDY] -= (attractor[3] * spareY / length) || 0
}
}
target = size
while (target--) {
if (target === current) continue
spareX = currPos[0] - boids[target][0]
spareY = currPos[1] - boids[target][1]
distSquared = spareX*spareX + spareY*spareY
if (distSquared < sepDist) {
sforceX += spareX
sforceY += spareY
} else {
if (distSquared < cohDist) {
cforceX += spareX
cforceY += spareY
}
if (distSquared < aliDist) {
aforceX += boids[target][SPEEDX]
aforceY += boids[target][SPEEDY]
}
}
}
// Separation
length = hypot(sforceX, sforceY)
boids[current][ACCELERATIONX] += (sepForce * sforceX / length) || 0
boids[current][ACCELERATIONY] += (sepForce * sforceY / length) || 0
// Cohesion
length = hypot(cforceX, cforceY)
boids[current][ACCELERATIONX] -= (cohForce * cforceX / length) || 0
boids[current][ACCELERATIONY] -= (cohForce * cforceY / length) || 0
// Alignment
length = hypot(aforceX, aforceY)
boids[current][ACCELERATIONX] -= (aliForce * aforceX / length) || 0
boids[current][ACCELERATIONY] -= (aliForce * aforceY / length) || 0
}
current = size
// Apply speed/acceleration for
// this tick
while (current--) {
if (accelerationLimit) {
distSquared = boids[current][ACCELERATIONX]*boids[current][ACCELERATIONX] + boids[current][ACCELERATIONY]*boids[current][ACCELERATIONY]
if (distSquared > accelerationLimit) {
ratio = accelerationLimitRoot / hypot(boids[current][ACCELERATIONX], boids[current][ACCELERATIONY])
boids[current][ACCELERATIONX] *= ratio
boids[current][ACCELERATIONY] *= ratio
}
}
boids[current][SPEEDX] += boids[current][ACCELERATIONX]
boids[current][SPEEDY] += boids[current][ACCELERATIONY]
if (speedLimit) {
distSquared = boids[current][SPEEDX]*boids[current][SPEEDX] + boids[current][SPEEDY]*boids[current][SPEEDY]
if (distSquared > speedLimit) {
ratio = speedLimitRoot / hypot(boids[current][SPEEDX], boids[current][SPEEDY])
boids[current][SPEEDX] *= ratio
boids[current][SPEEDY] *= ratio
}
}
boids[current][POSITIONX] += boids[current][SPEEDX]
boids[current][POSITIONY] += boids[current][SPEEDY]
}
this.emit('tick', boids)
}
// double-dog-leg hypothenuse approximation
// http://forums.parallax.com/discussion/147522/dog-leg-hypotenuse-approximation
function hypot(a, b) {
a = Math.abs(a)
b = Math.abs(b)
var lo = Math.min(a, b)
var hi = Math.max(a, b)
return hi + 3 * lo / 32 + Math.max(0, 2 * lo - hi) / 8 + Math.max(0, 4 * lo - hi) / 16
}