forked from chid/boids
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boids.coffee
180 lines (146 loc) · 5.19 KB
/
boids.coffee
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
171
172
173
174
175
176
177
178
179
180
# Represents a single boid
class Boid
constructor: (x = 0, y = 0) ->
@position = new Vector2(x, y)
@velocity = new Vector2(0, 0)
window.dist = (a, b) -> Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2))
# Boids simulation class
class @Boids
# Instance variables
loopInterval = 20 # Aim for 50 frames per second
lastRun = null
center = null
totalPosition = null
totalVelocity = null
tree = null
options =
simulationSpeed: 15
boidsNumber: 100
acceleration: 2
perceivedCenterWeight: 10
perceivedVelocityWeight: 10
collisionAvoidanceWeight: 5
stayInBoundsWeight: 8
flockSize: 10
minCollisionAvoidanceDistance: 50
stayInBoundsPower: 2
# Private methods
randomUpTo = (limit) -> Math.floor(Math.random() * limit) + 1
time = ->
new Date().getTime()
velocitySum: ->
sum = new Vector2
sum.add b.velocity for b in @boids
sum
positionSum: ->
sum = new Vector2
sum.add b.position for b in @boids
sum
perceivedFlockVelocity: (boid) ->
sum = new Vector2
points = tree.nearest({x: boid.position.x(), y: boid.position.y()}, Math.min(@boids.length, options['flockSize']))
for p in points
pos = new Vector2(p.x, p.y)
sum.add p.velocity unless pos.x() == boid.position.x() and pos.y() == boid.position.y()
sum.scalarDivide(points.length - 1)
perceivedCenter: (boid) ->
sum = new Vector2
points = tree.nearest({x: boid.position.x(), y: boid.position.y()}, Math.min(@boids.length, options['flockSize']))
for p in points
pos = new Vector2(p.x, p.y)
sum.add pos unless pos.x() == boid.position.x() and pos.y() == boid.position.y()
sum.scalarDivide(points.length - 1)
averagePosition: ->
sum = new Vector2
for b in @boids
sum.add b.position
sum.scalarDivide(@boids.length)
sum
distance: (a, b) -> Math.sqrt(Math.pow(a.x() - b.x(), 2) + Math.pow(a.y() - b.y(), 2))
avoidCollisions: (boid) ->
vel = new Vector2
points = tree.nearest({x: boid.position.x(), y: boid.position.y()}, Math.min(@boids.length, options['flockSize']))
for p in points
b = new Vector2(p.x, p.y)
if b.x() != boid.position.x() and b.y() != boid.position.y()
dist = @distance(b, boid.position)
if dist < options['minCollisionAvoidanceDistance']
vel.substract @direction(boid.position, b)
vel
stayInBounds: (boid, lx, ly, hx, hy, p = 2) ->
vel = new Vector2
vel.addX Math.pow(lx - boid.position.x(), p) if boid.position.x() < lx
vel.addY Math.pow(ly - boid.position.y(), p) if boid.position.y() < ly
vel.addX -Math.pow(hx - boid.position.x(), p) if boid.position.x() > hx
vel.addY -Math.pow(hy - boid.position.y(), p) if boid.position.y() > hy
vel
direction: (from, to) ->
goal = new Vector2(to.x(), to.y())
goal.substract(from)
initialize: ->
lastRun = time()
@boids = []
@goal = null
@setBoidsCount(options['boidsNumber'])
setBoidsCount: (number) ->
return if number == @boids.length
if number > @boids.length
until number == @boids.length
@boids.push new Boid(randomUpTo(@renderer.width()), randomUpTo(@renderer.height()))
if number < @boids.length
until number == @boids.length
@boids.pop()
update: (delta) ->
center = @averagePosition()
totalPosition = @positionSum()
totalVelocity = @velocitySum()
tree = new KDTree(@boids.map( (b) ->
{x: b.position.x(), y: b.position.y(), velocity: b.velocity}
))
# Update velocities
for b in @boids
vel = new Vector2
vel.add @direction(b.position, @perceivedCenter(b)).scalarMultiply(options['perceivedCenterWeight'])
vel.add @avoidCollisions(b).scalarMultiply(options['collisionAvoidanceWeight'])
vel.add @perceivedFlockVelocity(b).scalarMultiply(options['perceivedVelocityWeight'])
vel.add @stayInBounds(b, 50, 50, @renderer.width() - 50, @renderer.height() - 50, options['stayInBoundsPower']).scalarMultiply(options['stayInBoundsWeight'])
vel.add @direction(b.position, @goal).scalarMultiply(5) if @goal
vel.limit(1)
b.velocity.add vel.scalarMultiply(options['acceleration']/100)
b.velocity.limit(1)
# Update position
for b in @boids
vel = b.velocity.clone()
#vel.limit(0.4)
vel.scalarMultiply(delta/(20-options['simulationSpeed']+1))
b.position.add vel
run: =>
delta = time() - lastRun
lastRun = time()
@setBoidsCount(options['boidsNumber'])
@update(delta)
@renderer.render(@boids, center, @goal)
# Public API
constructor: (render_class) ->
console.log("Initializing")
@boids = []
@renderer = render_class
@status = "stopped"
start: ->
lastRun = time() if @status == "paused"
@initialize() if @status == "stopped"
@intervalHandle = setInterval(@run, loopInterval)
@status = "running"
pause: ->
window.clearInterval(@intervalHandle)
@status = "paused"
stop: ->
window.clearInterval(@intervalHandle)
@renderer.clearScreen()
@status = "stopped"
setGoal: (x, y) ->
@goal = new Vector2(x, y)
unsetGoal: () ->
@goal = null
get: (option) -> options[option]
set: (option, value) -> options[option] = value