-
Notifications
You must be signed in to change notification settings - Fork 0
/
boid.js
163 lines (146 loc) · 5.13 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
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
$(document).ready(function() {
cfg = {
'loop_interval': 10,
'debug': true
}
create_control = function(name) {
return $('<div>').attr('id','control').append(
$('<div>').attr('id','label').text(controlled[name].label)
).append(
$('<input>').attr({
'id': name,
'placeholder': controlled[name].n
}).keypress(function(e) {
if (e.which == 13) {
controlled[name].n = $('#'+name).val()
}
})
)
}
filters = {
'unique': function(me, we) {
return we.filter(function(i) {
return !vector.equal(me.pos, i.pos)
})
},
'neighbors': function(me, we) {
return we.filter(function(i) {
return vector.distance(me.pos, i.pos) < controlled.neighbor_distance.n
})
}
}
rules = {
'huddle': function(me, we) {
center = vector.mean(we.map(function(i) {
return i.pos
}))
adjust = vector.diff(center, me.pos)
adjust = vector.product(adjust,controlled.huddle_adjust.n)
return adjust
},
'avoid': function(me, we) {
we = filters.neighbors(me, we)
// displacements = [i.pos - me.pos for i in we]
displacements = we.map(function(i) {
return vector.diff(me.pos, i.pos)
})
adjust = vector.diff(vector.zero(), vector.sum(displacements))
//adjust = vector.quotient(adjust, controlled.avoid_adjust.n)
return adjust
},
'speed': function(me, we) {
match_speed = vector.mean(we.map(function(i) {
return i.vel
}))
adjust = vector.diff(match_speed, me.vel)
adjust = vector.product(adjust, controlled.speed_adjust.n)
return adjust
}
}
apply_rules = function(me, we) {
// return a vector that is the sum of all applied rules
return vector.sum(Object.keys(rules).map(function(key) {
rule = rules[key]
weight = controlled[key].n
return vector.product(rule(me, we), weight)
}))
}
boid = {
'make': function() {
/* represents a single boid */
return {
'color': rand.color(),
'pos': {
'x': rand.integer(0,canvas.width),
'y': rand.integer(0,canvas.height),
},
'vel': {
'x': rand.integer(1,controlled.max_velocity.n) * rand.choice([-1,1]),
'y': rand.integer(1,controlled.max_velocity.n) * rand.choice([-1,1])
}
}
},
'makeall' : function(count) {
/* create an array of `count` boids */
return Array.apply(null,Array(count)).map(this.make)
},
'check': function(pos) {
/* adjust a boids position if it hits a wall */
pos.x = bound(pos.x, 0, canvas.width)
pos.y = bound(pos.y, 0, canvas.height)
return pos
},
'speed_limit': function(boid) {
mag = vector.magnitude(boid.vel)
lim = controlled.max_velocity.n
if ( mag > lim ) {
boid.vel = vector.product( vector.quotient(boid.vel, mag), lim)
}
return boid
},
'draw': function(boid) {
ctx.fillStyle = controlled.color.n || boid.color
ctx.fillRect(boid.pos.x,boid.pos.y,controlled.size.n,controlled.size.n)
ctx.fill();
return boid
},
'move': function(boid) {
resultant = apply_rules(boid,boids)
boid.vel = vector.add(boid.vel, resultant)
boid.pos = this.check(vector.add(boid.pos, boid.vel))
return boid
}
}
boids = [];
canvas = {};
$('body').append(
$('<div>').attr('id','page-wrap').append(
$('<div>').attr('id','canvas-wrap').append(
$('<canvas>').attr('id','canvas')
)
).append(
$('<div>').attr('id','controls').append(
$('<div>').attr('id','boxes').append(
Object.keys(controlled).map(create_control)
)
).append($('<div>').attr('id','toggle'))
)
)
ctx = $('#canvas')[0].getContext('2d')
main = {
'loop': function() {
canvas.width = $('#canvas-wrap').width()
canvas.height = $('#canvas-wrap').height()
ctx.canvas.width = canvas.width
ctx.canvas.height = canvas.height
if (controlled.count.n != boids.length) {
boids = boid.makeall(parseInt(controlled.count.n));
}
boids.map(function(b) {return boid.draw(boid.speed_limit(boid.move(b)))})
},
'init': function() {
setInterval(this.loop, cfg.loop_interval)
}
}
main.init()
})