Created
July 26, 2018 20:19
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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