-
Notifications
You must be signed in to change notification settings - Fork 0
/
boid.py
201 lines (158 loc) · 6.4 KB
/
boid.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
import pygame
import random
from vector2d import Vector2D
import math
class Boid(pygame.sprite.Sprite):
def __init__(self, position):
# initialize the super sprite class
super(Boid, self).__init__()
# Setup the movement vectors.
self.velocity = Vector2D(random.randint(-2, 2), random.randint(-2, 2))
self.surf = self.makeSurface()
self.rect = self.surf.get_rect(center=position)
self.max_velocity = 3
# Flocking Charactoristics
self.sightRange = 150
self.max_acceleration = 0.1
self.alignWeight = 1/36
self.avoidWeight = 0.5
self.apprachWeight = 1/100
def update(self, neighborBoids):
# Loop Screen
margin = 200
factor = 0.1
if(self.rect.right < margin):
self.velocity.add(Vector2D(factor, 0))
if(self.rect.left > 1000-margin):
self.velocity.add(Vector2D(-factor, 0))
if(self.rect.top > 1000-margin):
self.velocity.add(Vector2D(0, -factor))
if(self.rect.bottom < margin):
self.velocity.add(Vector2D(0, factor))
# Get accerlation
acceleration = self.get_acceleration(neighborBoids)
# Apply steering accerlation to velocity
self.velocity.add(acceleration)
# Scale back the velocity to normal speed
self.velocity.unitize()
self.velocity.scale(self.max_velocity)
# Rotate to align with the new velocity
theta = self.velocity.calc_angle()
self.rotate(theta)
# Move following the velocity vector
self.rect.move_ip(self.velocity.to_tuple())
def rotate(self, angle):
# save the old center postion
oldCenter = self.rect.center
# sets the current surface to the enemy surface rotated to the
# indicated angle
self.surf = pygame.transform.rotate(self.makeSurface(), angle+90)
# get the rect of the rotated surf and set it's center to the saved
self.rect = self.surf.get_rect()
self.rect.center = oldCenter
def makeSurface(self):
# Create a surface that will represent the enemy
boidSurf = pygame.Surface((8, 16))
# blit the image onto the Surface
boidSurf.fill((255, 0, 255))
boidSurf.set_colorkey((255, 0, 255))
triangle_points = ((4, 0), (0, 16), (8, 16))
pygame.draw.polygon(boidSurf, (143, 200, 207), triangle_points)
return boidSurf
# ===============================
# Flocking Methods
# ===============================
def calc_avoid(self, boids):
"""Returns the avoid accerlation"""
avoidAccel = Vector2D(0, 0)
position = self.rect.center
# Add up all the separation vectors
for boid in boids:
xdiff = position[0]-boid.rect.center[0]
ydiff = position[1]-boid.rect.center[1]
diff = Vector2D(xdiff, ydiff)
diff.scale(1/(diff.calc_magnitude()**2))
avoidAccel.add(diff)
avoidAccel.scale(self.avoidWeight)
return avoidAccel
#
def calc_align(self, boids):
"""Returns the acceleration vector to align velocity direction with the
average velocity direction of nearby boids."""
velocities = Vector2D(0, 0)
velocity = self.velocity
# No change if there are no other boids around.
if not(len(boids)):
return Vector2D(0, 0)
# Accumulates velocities
for boid in boids:
velocities.add_values(boid.velocity.x, boid.velocity.y)
# Averages velocities
if len(boids) > 1:
velocities.scale(1/(len(boids)-1))
xdiff = velocities.x - velocity.x
ydiff = velocities.y - velocity.y
alignAccel = Vector2D(xdiff, ydiff)
alignAccel.scale(self.alignWeight)
return alignAccel
def calc_approach(self, boids):
"""Returns the approach accerlation"""
approachAccel = Vector2D(0, 0)
position = self.rect.center
# Add up all the separation vectors
for boid in boids:
xdiff = boid.rect.center[0]-position[0]
ydiff = boid.rect.center[1]-position[1]
approachAccel.add_values(xdiff, ydiff)
# Makes accleration based on average position
if len(boids) > 0:
approachAccel.scale(1/len(boids))
approachAccel.scale(self.apprachWeight)
return approachAccel
def calc_target(self, targetPos):
position = self.rect.center
# Ignore (-1, -1) for menu animations
if(targetPos == (-1, -1)):
return(Vector2D(0, 0))
weight = 1/100
"""Returns the acceleration towards the player."""
xdiff = targetPos[0]-position[0]
ydiff = targetPos[1]-position[1]
playerAccel = Vector2D(xdiff, ydiff)
playerAccel.scale(weight)
return playerAccel
def get_acceleration(self, boids):
"""Returns a single acceleration vector in response to nearby boids."""
# Find the neighboring boids
neighbors = self.find_neighbors(boids, self.sightRange)
# Acceleration acccumulator
# Add acceleration requests in order of importance
accelRequests = [
self.calc_avoid(neighbors),
self.calc_align(neighbors),
self.calc_approach(neighbors)]
# Add up requests untill max acceleration is reached
acceptedRequests = Vector2D(0, 0)
for request in accelRequests:
# Add if room
if acceptedRequests.calc_magnitude() < self.max_acceleration:
acceptedRequests.add(request)
# Trim tail if over
if acceptedRequests.calc_magnitude() > self.max_acceleration:
tail = acceptedRequests.calc_magnitude()-self.max_acceleration
request.unitize()
request.scale(-tail)
acceptedRequests.add(request)
return acceptedRequests
def find_neighbors(self, boids, sightRange):
"""Finds all the boids in a given list within the given distance of the
indicated position."""
position = self.rect.center
# List to hold nearby boids
nearbyBoids = []
# Keep boids within distance
for boid in boids:
d = math.dist(position, boid.rect.center)
if((d < sightRange) and (d > 0)):
nearbyBoids.append(boid)
return nearbyBoids