Exemple #1
0
 def cohesion(self, others):
     ''' Return acceleration towards center of mass of nearby birds
     '''
     count = 0
     steerx = 0
     steery = 0
     xavg = 0
     yavg = 0
     for n in range(0, len(others)):
         dist = self.distance(others[n])
         sx, sy = self.distanceVector(others[n])
         # Add acceleration for any birds within the cone of vision
         if dist < self.nd and dist > 0.0 and \
               (sx*self.vx+sy*self.vy)/(self.vx**2+self.vy**2) > self.alpha:
             count = count + 1
             xavg = xavg + others[n].x
             yavg = yavg + others[n].y
     if count > 0:
         xavg = xavg / count
         yavg = yavg / count
         # Create ghost boid representing the center of mass of the others
         ghost = Boid()
         ghost.x = xavg
         ghost.y = yavg
         steerx, steery = self.distanceVector(ghost)
         length = sqrt(steerx**2+steery**2)
         steerx = steerx/length*self.maxforce
         steery = steery/length*self.maxforce
         steerx = steerx - self.vx
         steery = steery - self.vy
     return steerx, steery
Exemple #2
0
 def cohesion(self, others):
     ''' Return acceleration towards center of mass of nearby birds
     '''
     count = 0
     steerx = 0
     steery = 0
     xavg = 0
     yavg = 0
     for n in range(0, len(others)):
         dist = self.distance(others[n])
         sx, sy = self.distanceVector(others[n])
         # Add acceleration for any birds within the cone of vision
         if dist < self.nd and dist > 0.0 and \
               (sx*self.vx+sy*self.vy)/(self.vx**2+self.vy**2) > self.alpha:
             count = count + 1
             xavg = xavg + others[n].x
             yavg = yavg + others[n].y
     if count > 0:
         xavg = xavg / count
         yavg = yavg / count
         # Create ghost boid representing the center of mass of the others
         ghost = Boid()
         ghost.x = xavg
         ghost.y = yavg
         steerx, steery = self.distanceVector(ghost)
         length = sqrt(steerx**2 + steery**2)
         steerx = steerx / length * self.maxforce
         steery = steery / length * self.maxforce
         steerx = steerx - self.vx
         steery = steery - self.vy
     return steerx, steery
Exemple #3
0
 def cohesion(self, others):
     ''' Returns acceleration towards center of mass of nearby others
     '''
     count = 0
     steerx = 0
     steery = 0
     xavg = 0
     yavg = 0
     for n in range(0, len(others)):
         dist = self.distance(others[n])
         if dist < self.nd and dist > 0.0:
             count = count + 1
             xavg = xavg + others[n].x
             yavg = yavg + others[n].y
     if count > 0:
         xavg = xavg / count
         yavg = yavg / count
         # Create ghost boid representing the others
         ghost = Boid()
         ghost.x = xavg
         ghost.y = yavg
         steerx, steery = self.distanceVector(ghost)
         length = sqrt(steerx**2+steery**2)
         steerx = steerx/length*self.maxforce
         steery = steery/length*self.maxforce
         steerx = steerx - self.vx
         steery = steery - self.vy
     return steerx, steery