Skip to content

Instantly share code, notes, and snippets.

@bytezen
Created July 26, 2018 20:19
Show Gist options
  • Save bytezen/389959e4bb289aca61dde730e9dd66fc to your computer and use it in GitHub Desktop.
Save bytezen/389959e4bb289aca61dde730e9dd66fc to your computer and use it in GitHub Desktop.
function for a boid to calculate a force to avoid walls in the world
def calculate_wall_avoidance(self, walls):
self.whiskers = util._create_whiskers(self.pos, self.heading)
#variables for keep track of tallys
distance_to_intersection_point = 0
distance_to_closest_intersection_point = sys.float_info.max
closest_wall = None
force = Vector2()
point = Vector2()
closest_point = Vector2()
# for each whisker find the closest wall
for whisker in self.whiskers:
for wall in self.world.walls:
intersects,distance,intersecting_point = util.line_intersection_get_distance_point(Vector2(self.pos),
whisker,
wall.point1,
wall.point2)
if intersects:
if distance < distance_to_closest_intersection_point:
distance_closest_intersection_point = distance
closest_wall = wall
closest_point = intersecting_point
# if we found a wall then calculate a steering force based on how far
# the whisker penetrated the wall
if closest_wall != None:
over_shoot = whisker - closest_point
force = wall.normal * over_shoot.length() * WALL_REPEL_FORCE
return force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment