def updateAlignment(self, mates): """Alignment: Steer towards the average heading of local flockmates.""" #mates = self.getVisibleMates(mates, self.alignment_perception) if len(mates) == 0: return Vector.null() v = Vector.average([m.velocity for m in mates]) v.norm = self.max_velocity v -= self.velocity v.limit(self.max_acceleration) return v
def updateCohesion(self, mates): """Cohesion: steer to move toward the average position of local flockmates.""" #mates = self.getVisibleMates(mates, self.cohesion_perception) if len(mates) == 0: return Vector.null() p = Vector.average([m.position for m in mates]) v = p - self.position v.norm = self.max_velocity v -= self.velocity v.limit(self.max_acceleration) return v
def updateSeparation(self, mates): """Separation: Avoid crowding local flockmates.""" #mates = self.getVisibleMates(mates, self.separation_perception) if len(mates) == 0: return Vector.null() vs = [] for mate in mates: v = self.position - mate.position v.norm = 1 / v.norm vs.append(v) v = Vector.average(vs) v.norm = self.max_velocity v -= self.velocity v.limit(self.max_acceleration) return v
def getAcceleration(self): """Return the acceleration of the material form.""" v = Vector.average([point.acceleration for point in self.points]) v.color = mycolors.RED return v
def getVelocity(self): """Return the velocity of the center of the material form.""" v = Vector.average([point.velocity for point in self.points]) v.color = mycolors.BLUE return v
def getPosition(self): """Return the position of the center of the material form.""" v = Vector.average([point.position for point in self.points]) v.color = mycolors.GREEN return v