def __init__(self, x, y): self.velocity = Vector(*((np.random.rand(2)) * 3)) self.position = Vector(x, y) self.view_radius = 100 random_hue = randint(0, 360) self.color = Color(0, 0, 0) self.color.hsla = (random_hue, 100, 75, 1) self.original_color = Color(0, 0, 0) self.original_color.hsla = (random_hue, 100, 75, 1)
def get_alignment(self): sumVector = Vector(0, 0) count = 0 for b in self.friends: d = np.linalg.norm(self.position - b.position) if d > 0 and d < NEARBY_RADIUS: copy = Vector(b.velocity.x, b.velocity.y) copy.normalize() copy /= d sumVector += copy count += 1 return sumVector
def flock(self): allign = self.get_alignment() avoidDir = self.get_separation() noise = Vector(random() * 2 - 1, random() * 2 - 1) cohese = self.get_cohesion() # Change vector multipliers here: allign *= 1.3 avoidDir *= 0.4 noise *= 0.1 cohese *= 8 # Increaseing a multiplier will give that rule more control over the # general direction of the boid. Change at your own risk!d self.velocity += allign self.velocity += avoidDir self.velocity += noise self.velocity += cohese # Restricts velocity to the max speed so boids don't reach infinite speed magnitude = np.hypot(self.velocity.y, self.velocity.x) if magnitude > MAX_SPEED: vector_set_magnitude(self.velocity, MAX_SPEED)
def get_cohesion(self): neighbordist = 50 sumVector = Vector(0, 0) count = 0 for b in self.friends: d = np.linalg.norm(self.position - b.position) if d > 0 and d < COHESION_RADIUS: sumVector += b.position count += 1 count = 0 if (count > 0): sumVector /= count desired = sumVector - self.position vector_set_magnitude(desired, 0.05) return desired else: return Vector(0, 0)
def cohesion(self, boids): pcj = Vector(0, 0) for b in boids: if b != self: pcj += b.position if len(boids) - 1 > 0: pcj /= len(boids) - 1 return (pcj - self.position) / 100
def separation(self, boids): c = Vector(0, 0) for b in boids: if b != self: if np.linalg.norm(b.position - self.position) < self.view_radius * 0.6: c -= (b.position - self.position) return c
def alignment(self, boids): # perceived velocity pvj = Vector(0, 0) total = 1 for b in boids: if b != self: pvj += b.velocity total += 1 pvj /= total return (pvj - self.velocity) / 8
def get_separation(self): steer = Vector(0, 0) count = 0 for b in self.friends: d = np.linalg.norm(self.position - b.position) if d > 0 and d < ALIGNMENT_RADIUS: diff = self.position - b.position diff.normalize() diff /= d steer += diff count += 1 if count > 0: return steer / count return steer
def __init__(self,start,end): self.start = Vector(start) self.end = Vector(end) self.vector = self.start-self.end
def __init__(self, x, y): self.position = Vector(x, y) self.velocity = Vector(random() * 2 - 1, random() * 2 - 1) self.friends = []