-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.py
304 lines (254 loc) · 9.62 KB
/
utils.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
from constants import *
import pygame as pg
from math import atan2, pi, exp, floor
import random
import copy
import numpy as np
vec = pg.math.Vector2
def normalFunction(omega,center,position):
f = exp( -omega*((position.x - center.x) + (position.y - center.y)))
return f
def bivariateFunction(alpha,beta,center,position):
'''
Calculates the bivariate function
position: (x,y)
center of the function: (xc,yc)
control variables: Alpha and Beta will control the stringthof the vectors in x and y directions
return: point in the bivariate function
'''
#k = 100000000 # parameter
k = 10
f = exp( -alpha*(position.x - center.x)/k**2 - beta*(position.y - center.y)/k**2 )
#print(f)
return f
def derivativeBivariate(alpha,beta,center,position):
'''
Calculates the bivariate function
position: (x,y)
center of the function: (xc,yc)
control variables: Alpha and Beta will control the stringthof the vectors in x and y directions
return: point in the bivariate function
'''
f = bivariateFunction(alpha,beta,center,position)
dx = f * (-2*alpha*(position.x-center.x))
dy = f * (-2*beta*(position.y-center.y))
return vec(dx,dy)
def constrain_ang(ang,min,max):
if ang > max:
ang = max
if ang < min:
ang = min
return ang
def random_color():
""""
Picks a random color R,G or B
:return: color picked
:rtype : tuple
"""
a = random.uniform(0,255)
rgbl=[a,0,0]
random.shuffle(rgbl)
return tuple(rgbl)
def limit(v2, max):
"""
Limits magnitude of vector2
:param v2: Vector2 to be normalized
:type v2: pygame.Vector2
:param max: maximum length of vector
:type max: int
:return v: returns vector
:rtype v: vector2
"""
v = copy.deepcopy(v2)
if v.length() > max:
v.scale_to_length(max)
return v
def limit3d(v3, max):
"""
Limits magnitude of vector2
:param v2: Vector2 to be normalized
:type v2: pygame.Vector2
:param max: maximum length of vector
:type max: int
:return v: returns vector
:rtype v: vector2
"""
v = copy.deepcopy(v3)
if v.length() > max:
v /= v.length()
v *= max
return v
def constrain(v2,w,h):
"""
Constrains movement of drone inside the canvas
:param v2: Vector2 to be constrained
:type v2: pygame.Vector2
:param w: maximum width
:type w: int
:param h: maximum height
:type h: int
:return v2: returns vector within the limits
:rtype v2: vector2
"""
if v2.x > w:
v2.x = w
if v2.x < 0:
v2.x = 0
if v2.y > h:
v2.y = h
if v2.y < 0:
v2.y = 0
return v2
def constrain3d(v3,w,h,alt):
"""
Constrains movement of drone inside the canvas
:param v2: Vector2 to be constrained
:type v2: pygame.Vector2
:param w: maximum width
:type w: int
:param h: maximum height
:type h: int
:return v2: returns vector within the limits
:rtype v2: vector2
"""
if v3.x > w:
v3.x = w
if v3.x < -w:
v3.x = -w
if v3.y > alt:
v3.y = alt
if v3.y < 0.2:
v3.y = 0.2
if v3.z > h:
v3.z = h
if v3.z < -h:
v3.z = -h
return v3
class Aircraft(pg.sprite.Sprite):
"""
Represents a simple visual animated drone
Can load sprites, rotate and update animation
"""
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.sprites = []
for i in range(0,4):
self.sprites.append(pg.image.load(f'models/Drone_hd/sprite_{i}.png').convert())
self.atual = 0
# inherited from the pygame sprite class it is the first element of the drone
self.image = self.sprites[self.atual]
# scales down drone sprites to (70,70)
self.image = pg.transform.scale(self.image,(SIZE_DRONE*2,SIZE_DRONE*2))
# rect is inherited from Sprite
# defines the sprite's position on the screen
# take the image size
self.rect = self.image.get_rect()
# pega o canto superior esquerdo, posição qualquer
#self.rect.topleft = 100,100
def colorize(self, newColor=(40,40,40)):
"""
Create a "colorized" copy of a surface (replaces RGB values with the given color, preserving the per-pixel alphas of
original).
:param image: Surface to create a colorized copy of
:param newColor: RGB color to use (original alpha values are preserved)
:return: New colorized Surface instance
"""
image = self.image.copy()
# zero out RGB values
#image.fill((0, 0, 0, 255), None, pg.BLEND_RGBA_MULT)
# add in new RGB values
image.fill(newColor[0:3] + (0,), None, pg.BLEND_RGBA_ADD)
self.image = image
def update(self, position, angle, size = SIZE_DRONE* PIX2M):
# animation update speed is controle by this parameter
self.atual += .1
if self.atual >= len(self.sprites):
self.atual = 0
self.image = self.sprites[floor(self.atual)]
# Rotates image -> angle should be in degrees
# rotozoom(Surface, angle, scale) -> Surface
self.image = pg.transform.rotozoom(self.image, -angle*180/pi - 90, .12)
self.rect = self.image.get_rect()
# positions center of rect in acual drone position
self.rect.center = position.x,position.y
class FlowField():
def __init__(self, resolution):
self.cols =int(SCREEN_WIDTH/resolution) # Columns of the grid
self.rows = int(SCREEN_HEIGHT/resolution) # Rows of the grid
self.resolution = resolution # Resolution of grid relative to window width and height in pixels
self.field = [[vec(random.uniform(0,1),random.uniform(0,1)) for col in range(self.cols)] for row in range(self.rows)] # create matrix
def draw(self, screen):
blockSize = self.resolution #Set the size of the grid block
#print(self.cols,self.rows)
for x in range(0, SCREEN_WIDTH, blockSize):
for y in range(0, SCREEN_HEIGHT, blockSize):
rect = pg.Rect(x, y, blockSize, blockSize)
pg.draw.rect(screen, (200,200,200), rect, 1)
class Npc_target(pg.sprite.Sprite):
"""
Represents a simple visual animated tree
Can load sprites, rotate and update animation
"""
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.sprites = []
#for i in range(1,4):
self.sprites.append(pg.image.load(f'models/texture/wandering_trader1.png').convert())
self.sprites[-1] = pg.transform.rotozoom(self.sprites[-1], 0, 1)
self.atual = 0
# inherited from the pygame sprite class it is the first element of the drone
self.image = self.sprites[self.atual]
# scales down drone sprites to (70,70)
#self.image = pg.transform.scale(self.image,(RADIUS_OBSTACLES,RADIUS_OBSTACLES))
# rect is inherited from Sprite
# defines the sprite's position on the screen
# take the image size
self.rect = self.image.get_rect()
# pega o canto superior esquerdo, posição qualquer
#self.rect.topleft = 100,100
def update(self, position, angle, size = SIZE_DRONE* PIX2M):
# animation update speed is controle by this parameter
self.atual += .001
if self.atual >= len(self.sprites)-1:
self.atual = 0
self.image = self.sprites[round(self.atual)]
# Rotates image -> angle should be in degrees
# rotozoom(Surface, angle, scale) -> Surface
#self.image = pg.transform.rotozoom(self.image, 0, .2)
self.rect = self.image.get_rect()
# positions center of rect in acual drone position
self.rect.midbottom = position.x,position.y+20
class Tree(pg.sprite.Sprite):
"""
Represents a simple visual animated tree
Can load sprites, rotate and update animation
"""
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.sprites = []
for i in range(1,4):
self.sprites.append(pg.image.load(f'models/tree3/tree_{i}.png').convert())
self.sprites[i-1] = pg.transform.rotozoom(self.sprites[i-1], 0, .3)
self.atual = 0
# inherited from the pygame sprite class it is the first element of the drone
self.image = self.sprites[self.atual]
# scales down drone sprites to (70,70)
#self.image = pg.transform.scale(self.image,(RADIUS_OBSTACLES,RADIUS_OBSTACLES))
# rect is inherited from Sprite
# defines the sprite's position on the screen
# take the image size
self.rect = self.image.get_rect()
# pega o canto superior esquerdo, posição qualquer
#self.rect.topleft = 100,100
def update(self, position, angle, size = SIZE_DRONE* PIX2M):
# animation update speed is controle by this parameter
self.atual += .001
if self.atual >= len(self.sprites)-1:
self.atual = 0
self.image = self.sprites[round(self.atual)]
# Rotates image -> angle should be in degrees
# rotozoom(Surface, angle, scale) -> Surface
#self.image = pg.transform.rotozoom(self.image, 0, .2)
self.rect = self.image.get_rect()
# positions center of rect in acual drone position
self.rect.midbottom = position.x,position.y+20