def obstacleAvoid(self, obstacles): self.avoidance = v2.vector2(0,0) tv = self.velocity tv.norm() tv = tv * MAXAVOIDAHEAD ahead = self.position + tv mostThreatening = None for i in range(len(obstacles)): obstacle = obstacles[i] if((obstacle.pos - self.position).mag()>96): continue else: if(kd.debug): PKFW.drln(self.position.x, self.position.y, obstacle.pos.x, obstacle.pos.y, 0,0,1) coll = self.lineIntersection(self.position, ahead, obstacle) if(coll and ((mostThreatening is None) or (self.position.distance(obstacle.pos) < self.position.distance(mostThreatening.pos)))): mostThreatening = obstacle if(mostThreatening is not None): self.avoidance = ahead - mostThreatening.pos self.avoidance.norm() self.avoidance = self.avoidance * AVOIDFOR else: self.wander()
def lineIntersection(self, pos, ahead, obstacle): tv = self.velocity tv.norm() tv = tv * MAXAVOIDAHEAD * 0.75 ahead2 = self.position + tv if(kd.debug): PKFW.drln(self.position.x, self.position.y, ahead.x, ahead.y, 1,0,1) PKFW.drln(self.position.x, self.position.y, ahead2.x, ahead2.y, 1,1,0) n = 52 if(kd.debug): for t in range(n)[::3]: th = 2* math.pi * t/n th1 = 2* math.pi * (t+1)/n v = v2.vector2(n*math.cos(th), n*math.sin(th)) v1 = v2.vector2(n*math.cos(th1), n*math.sin(th1)) if(obstacle.gpos['x']>0 and obstacle.gpos['y']>0): PKFW.drln(obstacle.pos.x+v.x, obstacle.pos.y+v.y, obstacle.pos.x+v1.x, obstacle.pos.y+v1.y, 1,1,1) return (obstacle.pos.distance(ahead)<=n) or (obstacle.pos.distance(ahead2)<=n)