-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathflocking.py
More file actions
197 lines (169 loc) · 5.93 KB
/
Copy pathflocking.py
File metadata and controls
197 lines (169 loc) · 5.93 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
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
# Flocking
#
# Ported from Dan Schiffman's Flocking
#
# 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.
#
# Click the mouse to add a new boid.
from mewnala import *
flock = None
def setup():
global flock
size(640, 360)
flock = Flock()
# Add an initial set of boids into the system
for i in range(150):
flock.add_boid(Boid(width / 2, height / 2))
def draw():
background(50)
flock.run()
# Add a new boid into the System
def mouse_pressed():
flock.add_boid(Boid(mouse_x, mouse_y))
# The Flock (a list of Boid objects)
class Flock:
def __init__(self):
self.boids = [] # A list for all the boids
def run(self):
for b in self.boids:
b.run(self.boids) # Passing the entire list of boids to each boid individually
def add_boid(self, b):
self.boids.append(b)
# The Boid class
class Boid:
def __init__(self, x, y):
self.acceleration = Vec2(0, 0)
self.velocity = Vec2.random()
self.position = Vec2(x, y)
self.r = 2.0
self.maxspeed = 2.0 # Maximum speed
self.maxforce = 0.03 # Maximum steering force
def run(self, boids):
self.flock(boids)
self.update()
self.borders()
self.render()
def apply_force(self, force):
# We could add mass here if we want A = F / M
self.acceleration.add(force)
# We accumulate a new acceleration each time based on three rules
def flock(self, boids):
sep = self.separate(boids) # Separation
ali = self.align(boids) # Alignment
coh = self.cohesion(boids) # Cohesion
# Arbitrarily weight these forces
sep.mult(1.5)
ali.mult(1.0)
coh.mult(1.0)
# Add the force vectors to acceleration
self.apply_force(sep)
self.apply_force(ali)
self.apply_force(coh)
# Method to update position
def update(self):
# Update velocity
self.velocity.add(self.acceleration)
# Limit speed
self.velocity.limit(self.maxspeed)
self.position.add(self.velocity)
# Reset acceleration to 0 each cycle
self.acceleration.mult(0)
# A method that calculates and applies a steering force towards a target
# STEER = DESIRED MINUS VELOCITY
def seek(self, target):
desired = target - self.position # A vector pointing from the position to the target
# Scale to maximum speed
desired.set_mag(self.maxspeed)
# Steering = Desired minus Velocity
steer = desired - self.velocity
steer.limit(self.maxforce) # Limit to maximum steering force
return steer
def render(self):
# Draw a triangle rotated in the direction of velocity
theta = self.velocity.heading() + HALF_PI
fill(200, 100)
stroke(255)
push_matrix()
translate(self.position.x, self.position.y)
rotate(theta)
begin_shape(TRIANGLES)
vertex(0, -self.r * 2)
vertex(-self.r, self.r * 2)
vertex(self.r, self.r * 2)
end_shape()
pop_matrix()
# Wraparound
def borders(self):
if self.position.x < -self.r:
self.position.x = width + self.r
if self.position.y < -self.r:
self.position.y = height + self.r
if self.position.x > width + self.r:
self.position.x = -self.r
if self.position.y > height + self.r:
self.position.y = -self.r
# Separation
# Method checks for nearby boids and steers away
def separate(self, boids):
desired_separation = 25.0
steer = Vec2(0, 0)
count = 0
# For every boid in the system, check if it's too close
for other in boids:
d = self.position.dist(other.position)
# If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
if 0 < d < desired_separation:
# Calculate vector pointing away from neighbor
diff = (self.position - other.position).normalize()
diff.div(d) # Weight by distance
steer.add(diff)
count += 1 # Keep track of how many
# Average -- divide by how many
if count > 0:
steer.div(count)
# As long as the vector is greater than 0
if steer.mag() > 0:
# Implement Reynolds: Steering = Desired - Velocity
steer.set_mag(self.maxspeed)
steer.sub(self.velocity)
steer.limit(self.maxforce)
return steer
# Alignment
# For every nearby boid in the system, calculate the average velocity
def align(self, boids):
neighbor_dist = 50.0
sum = Vec2(0, 0)
count = 0
for other in boids:
d = self.position.dist(other.position)
if 0 < d < neighbor_dist:
sum.add(other.velocity)
count += 1
if count > 0:
sum.div(count)
# Implement Reynolds: Steering = Desired - Velocity
sum.set_mag(self.maxspeed)
steer = sum - self.velocity
steer.limit(self.maxforce)
return steer
else:
return Vec2(0, 0)
# Cohesion
# For the average position (i.e. center) of all nearby boids, calculate steering vector towards that position
def cohesion(self, boids):
neighbor_dist = 50.0
sum = Vec2(0, 0) # Start with empty vector to accumulate all positions
count = 0
for other in boids:
d = self.position.dist(other.position)
if 0 < d < neighbor_dist:
sum.add(other.position) # Add position
count += 1
if count > 0:
sum.div(count)
return self.seek(sum) # Steer towards the position
else:
return Vec2(0, 0)
run()