def flee(self, others): #find average point of all other Boids centerPoint = others.pop(0).position for other in others: centerPoint = Point3d.Add(centerPoint, other.position) centerPoint = Point3d.Divide(centerPoint, numBoids - 1) #find direction from Boid to average point of all other Boids direction = Point3d.Subtract(centerPoint, self.position) #remove magnitude from average-seeking vector direction = Vector3d.Divide(direction, direction.Length) #adapt heading to new direction self.heading = Vector3d.Add(-direction, self.heading) self.heading = Vector3d.Add(-direction, self.heading)
def update(self, eddies, crv): self.gettarget_2d(eddies) self.getorient(eddies) self.getacc_z() self.boundarycheck() self.getvel() self.loc = PVector.Add(self.loc, self.vel)
def align(self, others): #generate list of other Boid headings headings = [other.heading for other in others] #empty vector creation uberVector = Vector3d(0.0, 0.0, 0.0) #add up all headings for heading in headings: uberVector = Vector3d.Add(uberVector, heading) #remove magnitude from sum of all headings uberVector = Vector3d.Divide(uberVector, uberVector.Length) #set Boids heading to aligned vector self.heading = uberVector
def gettarget_2d(self, eddies): target_2d = PVector(0, 0, 0) for i in xrange(len(eddies)): v = PVector.Subtract(eddies[i].loc, self.loc) v2d = PVector( v.X, v.Y, 0) # 2d planer target, X Y are the most important factors dist2d = v2d.Length # compute the sum of the arraction vector / distance v2d.Unitize() v2d = PVector.Multiply(v2d, float(eddies[i].gravity / pow(dist2d, 1.0))) # add vector to attraction vector collection target_2d = PVector.Add(target_2d, v2d) #Limit the target length target_2d = toplimit(target_2d, self.toplimit) target_2d = lowerlimit(target_2d, self.lowerlimit) self.target = target_2d
def getvel(self): self.vel = PVector.Add(self.vel, self.orient) self.vel = toplimit(self.vel, self.toplimit) self.vel = PVector.Add(self.vel, self.acc)
def maintain(self): newDirection = Vector3d(random.uniform(-.5, .5), random.uniform(-.5, .5), random.uniform(-.5, .5)) self.heading = Vector3d.Add(newDirection, self.heading)