-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlockingSimulation.py
More file actions
202 lines (169 loc) · 6.01 KB
/
Copy pathFlockingSimulation.py
File metadata and controls
202 lines (169 loc) · 6.01 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
198
199
200
201
202
from PygameHaze import QuadTree
from typing import List, Tuple
import itertools
import random
import pygame
import time
import math
import sys
import os
W: int = 1200
H: int = 750
def randomVector():
return pygame.math.Vector2(random.uniform(-1, 1), random.uniform(-1, 1))
setattr( # needed because the QuadTree is broken atm (-_-)
QuadTree,
"get_items",
lambda self: self.storage
+ list(itertools.chain.from_iterable([ch.get_items() for ch in self.children])),
)
class Ship:
max_speed = 5
max_force = 0.5
def __init__(self) -> None:
self.pos: pygame.math.Vector2 = pygame.math.Vector2(
random.randint(0, W), random.randint(0, H)
)
self.vel: pygame.math.Vector2 = randomVector()
self.vel.scale_to_length(random.choice([2, 4]))
self.acc: pygame.math.Vector2 = pygame.math.Vector2()
def edges(self) -> None:
self.pos.x = self.pos.x % W
self.pos.y = self.pos.y % H
def align(self, others: List["Ship"]) -> pygame.math.Vector2:
perceptionRadius = 25 * 25
steering = pygame.math.Vector2()
total = 0
for other in others:
d = self.pos.distance_squared_to(other.pos)
if other != self and d < perceptionRadius:
steering += other.vel
total += 1
if total > 0:
steering /= total
steering.scale_to_length(self.max_speed)
steering -= self.vel
if steering.magnitude_squared() > self.max_force * self.max_force:
steering.scale_to_length(self.max_force)
return steering
def separation(self, others: List["Ship"]) -> pygame.math.Vector2:
perceptionRadius = 24 * 24
steering = pygame.math.Vector2()
total = 0
for other in others:
d = self.pos.distance_squared_to(other.pos)
if other != self and d < perceptionRadius:
diff = self.pos - other.pos
diff /= d or 1
steering += diff
total += 1
if total > 0:
steering /= total
try:
steering.scale_to_length(self.max_speed)
except ValueError:
pass
steering -= self.vel
if steering.magnitude_squared() > self.max_force * self.max_force:
steering.scale_to_length(self.max_force)
return steering
def cohesion(self, others: List["Ship"]) -> pygame.math.Vector2:
perceptionRadius = 50 * 50
steering = pygame.math.Vector2()
total = 0
for other in others:
d = self.pos.distance_squared_to(other.pos)
if other != self and d < perceptionRadius:
steering += other.pos
total += 1
if total > 0:
steering /= total
steering -= self.pos
try:
steering.scale_to_length(self.max_speed)
except ValueError:
pass
steering -= self.vel
if steering.magnitude_squared() > self.max_force * self.max_force:
steering.scale_to_length(self.max_force)
return steering
def flock(self, others: QuadTree) -> "Ship":
al = self.align(
others.query(
pygame.Rect(
self.pos - pygame.math.Vector2(25, 25),
pygame.math.Vector2(25 * 2, 25 * 2),
)
)
)
se = self.separation(
others.query(
pygame.Rect(
self.pos - pygame.math.Vector2(24, 24),
pygame.math.Vector2(24 * 2, 24 * 2),
)
)
)
co = self.cohesion(
others.query(
pygame.Rect(
self.pos - pygame.math.Vector2(50, 50),
pygame.math.Vector2(50 * 2, 50 * 2),
)
)
)
self.acc += al + se + co
return self
def update(self, dt: float = 1) -> "Ship":
self.vel += self.acc
self.pos += self.vel * dt
if self.vel.magnitude_squared() > self.max_force * self.max_force:
self.vel.scale_to_length(self.max_force)
self.acc.update(0, 0)
return self
def draw(self, surface: pygame.surface.Surface) -> None:
pygame.draw.circle(surface, (255, 255, 255), self.pos, 4)
class Game:
def __init__(self):
self.WIN: pygame.surface.Surface = pygame.display.set_mode((W, H))
self.running: bool = True
self.clock: pygame.time.Clock = pygame.time.Clock()
self.FPS: int = 60
self.qt = QuadTree(self.WIN.get_rect(), 5)
self.ships: List[Ship] = [Ship() for _ in range(200)]
self.last_time: float = time.time()
self.dt = (time.time() - self.last_time) * self.FPS
self.last_time = time.time()
pygame.display.set_caption("Flocking Simulation with pygame")
def event_handler(self) -> None:
for event in pygame.event.get():
if event.type == pygame.QUIT or (
event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE
):
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
self.ships.append(Ship())
self.ships[~0].pos = pygame.math.Vector2(event.pos)
def draw(self) -> None:
self.WIN.fill((30, 30, 30))
self.qt = QuadTree(self.WIN.get_rect(), 5)
self.qt.list_insert(self.ships)
for ship in self.ships:
ship.edges()
ship.flock(self.qt)
ship.update(self.dt)
ship.draw(self.WIN)
pygame.display.update()
def run(self) -> None:
while self.running:
self.dt = (time.time() - self.last_time) * self.FPS
self.last_time = time.time()
self.clock.tick(self.FPS)
self.event_handler()
self.draw()
def run():
game = Game()
game.run()
if __name__ == "__main__":
run()