def seek(self,target): desired=Pvector(target.x,target.y) desired.sub(self.location) mag=desired.magnitude() if mag<100: setmag=mag*self.maxspeed/100 desired.setMag(setmag) else: desired.setMag(self.maxspeed) desired.sub(self.velocity) desired.limit(self.maxforce) return desired
def seek_flow(self): if self.location.x<0: self.location.x=799 if self.location.x>799: self.location.x=0 if self.location.y<0: self.location.y=799 if self.location.y>799: self.location.y=0 desired=Pvector(self.lookup(self.location).x,self.lookup(self.location).y) desired.setMag(self.maxspeed) desired.sub(self.velocity) desired.limit(self.maxforce) self.applyForce(desired)
def draw(): global points,vehicles #gf.record() for v in vehicles: worldRecord=100000 bestPath=None if points.points[0].x<points.points[-1].x: if v.location.x>points.points[-1].x: v.location.x=points.points[0].x v.location.y=points.points[0].y+(v.location.y-points.points[-1].y) else: if v.location.x<points.points[-1].x: v.location.x=points.points[0].x v.location.y=points.points[0].y+(v.location.y-points.points[-1].y) for i in range(len(points.points)-1): a=points.points[i].get() b=points.points[i+1].get() if points.points[0].x<points.points[-1].x: if b.x<v.location.x: continue else: if v.location.x<b.x: continue norm=get_normal(a,b,v) dist=get_distance(norm.get(),v.location) if dist<worldRecord: worldRecord=dist bestNorm=norm bestPath=Pvector(b.x-a.x,b.y-a.y) bestPath.setMag(50) if worldRecord>50: bestNorm.add(bestPath) seek=v.seek(bestNorm) else: bestPath.add(v.location) seek=v.seek(bestPath) seperate=v.seperate(vehicles) seperate.mult(0.8) v.applyForce(seek) v.applyForce(seperate) v.update() v.display() background(255) points.display()