-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
executable file
·279 lines (208 loc) · 7.22 KB
/
application.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import sys
import random
import settings
import utility
import sfml as sf
from statistics import Statistics
from entity import Entity
from collision_grid import CollisionGrid
time_per_update = settings.TIME_PER_UPDATE
width = settings.WIDTH
height = settings.HEIGHT
cell_size = settings.CELL_SIZE
class Application(object):
def __init__(self):
font = sf.Font.from_file("media/fonts/UbuntuMono-R.ttf")
self.statistics = Statistics(font)
window_settings = sf.window.ContextSettings()
window_settings.antialiasing_level = 8
self.window = sf.RenderWindow(
sf.VideoMode(width, height),
"Steering Behaviors For Autonomous Characters",
sf.Style.DEFAULT, window_settings)
self.window.vertical_synchronization = False
self.entities = []
self.grid = CollisionGrid(width, height, cell_size)
self.collision = 0
def run(self):
clock = sf.Clock()
time_since_last_update = sf.seconds(0)
for i in range(settings.INITIAL_ENTITIES):
self.entities.append(Entity(sf.Vector2(
random.randrange(width),
random.randrange(height))))
while self.window.is_open:
dt = clock.restart()
time_since_last_update += dt
while time_since_last_update > time_per_update:
time_since_last_update -= time_per_update
t = clock.elapsed_time
self.update(time_per_update)
t = clock.elapsed_time - t
self.statistics.t_update.append(t.microseconds)
self.process_events()
self.statistics.update_texts(dt)
t = clock.elapsed_time
self.render()
self.statistics.num_frames += 1
t = clock.elapsed_time - t
self.statistics.t_render.append(t.microseconds)
def process_events(self):
def close():
self.window.close()
def toggle_help():
self.statistics.help ^= True
def delete_entities():
del self.entities[:]
def toggle_attractive_mouse():
settings.attractive_mouse = not settings.attractive_mouse
def toggle_scary_mouse():
settings.scary_mouse = not settings.scary_mouse
def scatter_boids():
for e in self.entities:
e.scatter()
def increase_boid_sight_radius():
settings.boid_sight_radius += 10
def decrease_boid_sight_radius():
settings.boid_sight_radius -= 10
def increase_desired_separation():
settings.desired_separation += 10
def decrease_desired_separation():
settings.desired_separation -= 10
def increase_max_steering_force():
settings.max_steering_force += 10
def decrease_max_steering_force():
settings.max_steering_force -= 10
def increase_separation_factor():
settings.separation += 0.1
def decrease_separation_factor():
settings.separation -= 0.1
def increase_alignment_factor():
settings.alignment += 0.01
def decrease_alignment_factor():
settings.alignment -= 0.01
def increase_cohesion_factor():
settings.cohesion += 0.01
def decrease_cohesion_factor():
settings.cohesion -= 0.01
actions = {
sf.Keyboard.ESCAPE : close,
settings.toggle_help : toggle_help,
settings.delete_entities : delete_entities,
settings.toggle_attractive_mouse : toggle_attractive_mouse,
settings.toggle_scary_mouse : toggle_scary_mouse,
settings.scatter_boids : scatter_boids,
settings.increase_boid_sight_radius : increase_boid_sight_radius,
settings.decrease_boid_sight_radius : decrease_boid_sight_radius,
settings.increase_desired_separation : increase_desired_separation,
settings.decrease_desired_separation : decrease_desired_separation,
settings.increase_max_steering_force : increase_max_steering_force,
settings.decrease_max_steering_force : decrease_max_steering_force,
settings.increase_separation_factor : increase_separation_factor,
settings.decrease_separation_factor : decrease_separation_factor,
settings.increase_alignment_factor : increase_alignment_factor,
settings.decrease_alignment_factor : decrease_alignment_factor,
settings.increase_cohesion_factor : increase_cohesion_factor,
settings.decrease_cohesion_factor : decrease_cohesion_factor
}
for event in self.window.events:
if (type(event) is sf.CloseEvent):
close()
elif (type(event) is sf.MouseButtonEvent and event.pressed):
self.entities.append(Entity(event.position))
elif (type(event) is sf.KeyEvent and event.pressed):
try:
actions.get(event.code)()
except TypeError:
pass
def update(self, dt):
if self.collision == 5:
self.update_grid()
self.handle_collision()
self.grid.clear()
self.collision = 0
self.collision += 1
for e in self.entities:
e.update(dt)
if (e.position.x < 0):
e.position.x += width
elif (e.position.x > width):
e.position.x -= width
if (e.position.y < 0):
e.position.y += height
elif (e.position.y > height):
e.position.y -= height
def render(self):
self.window.clear()
for e in self.entities:
self.window.draw(e)
self.window.draw(self.grid)
self.window.draw(self.statistics)
self.window.display()
def update_grid(self):
for e in self.entities:
self.grid.add_entity(e)
def handle_collision(self):
self.statistics.num_entities = len(self.entities)
self.statistics.collision_checks = 0
for f in self.entities:
self.grid.remove_entity(f)
nearby_boids = self.grid.get_nearby_entities(f,
settings.boid_sight_radius)
# SCARY MOUSE
if (settings.scary_mouse):
d = sf.Mouse.get_position(self.window) - f.position
if (not d.x and not d.y):
d.x = 1
distance = utility.length(d)
if (distance < settings.ENTITY_SIZE * 7):
steer = d * (settings.ENTITY_SIZE * 7/distance - 1)
if (utility.length(steer) > settings.max_steering_force):
steer = utility.unit_vector(steer) * settings.max_steering_force
f.velocity -= steer
# ATTRACTIVE MOUSE
if (settings.attractive_mouse):
f.centre_of_mass += sf.Mouse.get_position(self.window)
f.num_nearby_entities += 1
# NEARBY BOIDS
for s in nearby_boids:
self.statistics.collision_checks += 1
d = s.position - f.position
if (not d.x and not d.y):
d.x = 1
distance = utility.length(d)
if (distance < settings.boid_sight_radius):
f.centre_of_mass += s.position
s.centre_of_mass += f.position
f.average_velocity += s.velocity
s.average_velocity += f.velocity
f.num_nearby_entities += 1
s.num_nearby_entities += 1
# SEPARATE
if (distance < settings.desired_separation):
steer = d * (settings.desired_separation/distance - 1) * settings.separation
if (utility.length(steer) > settings.max_steering_force):
steer = utility.unit_vector(steer) * settings.max_steering_force
f.velocity -= steer
s.velocity += steer
if (not f.num_nearby_entities):
continue
# COHERE
desired = f.centre_of_mass / f.num_nearby_entities - f.position
d = utility.length(desired)
if (d < 65):
desired *= d/15
else:
desired *= 65/15
steer = desired-f.velocity
if (utility.length(steer) > settings.max_steering_force):
steer = utility.unit_vector(steer) * settings.max_steering_force
f.velocity += steer * settings.cohesion
# ALIGN
steer = f.average_velocity / f.num_nearby_entities
if (utility.length(steer) > settings.max_steering_force):
steer = utility.unit_vector(steer) * settings.max_steering_force
f.velocity += steer * settings.alignment
if __name__ == "__main__":
app = Application()
app.run()