-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathflocking2.py
executable file
·238 lines (167 loc) · 6.76 KB
/
flocking2.py
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/env python
# - coding: utf-8 -
# Copyright (C) 2010 Toms Bauģis <toms.baugis at gmail.com>
"""
Flocking 2 - based on flocking and added the bin-latice spatial clustering
with all the optimizations we are still way behind the processing version.
Help me fixing the slow parts!
* An implementation of Craig Reynold's Boids program to simulate
* the flocking behavior of birds. Each boid steers itself based on
* rules of avoidance, alignment, and coherence.
*
Parts of code ported from opensteer (http://sourceforge.net/projects/opensteer/)
Other parts ported from processing (http://processing.org)
"""
from gi.repository import Gtk as gtk
from lib import graphics
import math
from random import random
from contrib.euclid import Vector2, Point2
from contrib.proximity import LQProximityStore
class Boid(object):
radius = 2 # boid radius
# distances are squared to avoid roots (slower)
neighbour_distance = float(50**2)
desired_separation = float(25**2)
braking_distance = float(100**2)
def __init__(self, location, max_speed, max_force):
self.acceleration = Vector2()
self.velocity = Vector2(random() * 2 - 1, random() * 2 - 1)
self.location = location;
self.max_speed = max_speed
self.max_force = max_force
def run(self, flock_boids):
self.flock(flock_boids)
self.velocity += self.acceleration
self.velocity.limit(self.max_speed)
self.location += self.velocity
def flock(self, boids):
if not boids:
return
# We accumulate a new acceleration each time based on three rules
# and weight them
separation = self.separate(boids) * 2
alignment = self.align(boids) * 1
cohesion = self.cohesion(boids) * 1
# The sum is the wanted acceleration
self.acceleration = separation + alignment + cohesion
def separate(self, boids):
sum = Vector2()
in_zone = 0.0
for boid, d in boids:
if 0 < d < self.desired_separation:
diff = self.location - boid.location
diff.normalize()
diff = diff / math.sqrt(d) # Weight by distance
sum += diff
in_zone += 1
if in_zone:
sum = sum / in_zone
return sum
def align(self, boids):
sum = Vector2()
in_zone = 0.0
for boid, d in boids:
if 0 < d < self.neighbour_distance:
sum += boid.velocity
in_zone += 1
if in_zone:
sum = sum / in_zone # weight by neighbour count
sum.limit(self.max_force)
return sum
def cohesion(self, boids,):
""" For the average location (i.e. center) of all nearby boids,
calculate steering vector towards that location"""
sum = Vector2()
in_zone = 0
for boid, d in boids:
if 0 < d < self.neighbour_distance:
sum = sum + boid.location
in_zone +=1
if in_zone:
sum = sum / float(in_zone)
return self.steer(sum, True)
return sum
def seek(target):
self.acceleration += self.steer(target, False)
def arrive(target):
self.acceleration += self.steer(target, True)
def steer(self, target, slow_down):
desired = target - self.location # A vector pointing from the location to the target
d = desired.magnitude_squared()
if d > 0: # this means that we have a target
desired.normalize()
# Two options for desired vector magnitude (1 -- based on distance, 2 -- maxspeed)
if slow_down and d > self.braking_distance:
desired *= self.max_speed * d / self.braking_distance # This damping is somewhat arbitrary
else:
desired *= self.max_speed
steer = desired - self.velocity # Steering = Desired minus Velocity
steer.limit(self.max_force) # Limit to maximum steering force
return steer
else:
return Vector2()
class Canvas(graphics.Scene):
def __init__(self):
graphics.Scene.__init__(self)
self.segments = []
# we should redo the boxes when window gets resized
self.proximity_radius = 10
self.proximities = LQProximityStore(Vector2(0,0), Vector2(600,400), self.proximity_radius)
self.flock = []
self.frame = 0
self.connect("on-click", self.on_mouse_click)
self.connect("on-enter-frame", self.on_enter_frame)
def on_enter_frame(self, scene, context):
c_graphics = graphics.Graphics(context)
if len(self.flock) < 80:
for i in range(2):
self.flock.append(Boid(Vector2(self.width / 2, self.height / 2), 2.0, 0.05))
# main loop (i should rename this to something more obvious)
c_graphics.set_line_style(width = 0.8)
c_graphics.set_color("#666")
for boid in self.flock:
neighbours = []
if self.frame % 2 == 0: #recalculate direction every second frame
neighbours = self.proximities.find_neighbours(boid, 40)
boid.run(neighbours)
self.wrap(boid)
self.proximities.update_position(boid)
self.draw_boid(context, boid)
self.frame +=1
context.stroke()
self.redraw()
def wrap(self, boid):
"wraps boid around the edges (teleportation)"
if boid.location.x < -boid.radius:
boid.location.x = self.width + boid.radius
if boid.location.y < -boid.radius:
boid.location.y = self.height + boid.radius
if boid.location.x > self.width + boid.radius:
boid.location.x = -boid.radius
if boid.location.y > self.height + boid.radius:
boid.location.y = -boid.radius
def draw_boid(self, context, boid):
context.save()
context.translate(boid.location.x, boid.location.y)
theta = boid.velocity.heading() + math.pi / 2
context.rotate(theta)
context.move_to(0, -boid.radius*2)
context.line_to(-boid.radius, boid.radius*2)
context.line_to(boid.radius, boid.radius*2)
context.line_to(0, -boid.radius*2)
context.restore()
def on_mouse_click(self, widget, event, target):
self.flock.append(Boid(Vector2(event.x, event.y), 2.0, 0.05))
class BasicWindow:
def __init__(self):
window = gtk.Window()
window.set_size_request(600, 400)
window.connect("delete_event", lambda *args: gtk.main_quit())
window.add(Canvas())
window.show_all()
if __name__ == "__main__":
example = BasicWindow()
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL) # gtk3 screws up ctrl+c
gtk.main()