def _calc_projectile_path_avoidance(self): rocket_location = self.history.rocket.location turret_location = self.parameters.turret.location projectile_path_avoidance = [] for projectile in self.history.active_projectiles: gradient = math.tan(projectile.firing_angle) y_intercept = turret_location.y - gradient * turret_location.x y_value = gradient * rocket_location.x + y_intercept avoidance_angle = normalise_angle( projectile.firing_angle + (1 if rocket_location.y > y_value else -1) * math.pi / 2) min_dist_rocket2path = self.calc_minimum_distance_from_location2line( rocket_location, gradient, y_intercept) projectile_location = self.helpers.calc_projectile_location( projectile) dist_rockt2projectile = rocket_location.distance2( projectile_location) try: avoidance_strength = 1 / ( min_dist_rocket2path * (dist_rockt2projectile - self.parameters.rocket.target_radius)) except ZeroDivisionError: avoidance_strength = float("inf") projectile_path_avoidance.append( PolarCoordinate(avoidance_strength, avoidance_angle).pol2cart()) return (average(projectile_path_avoidance) if projectile_path_avoidance else Coordinate(0.0, 0.0))
def _calc_within_buffer_projectile_avoidance(self, safety_factor=2.0): rocket_location = self.history.rocket.location rocket_velocity = self.physics.calc_rocket_velocity() within_buffer_projectile_avoidance = [] for projectile in self.history.active_projectiles: projectile_location = self.helpers.calc_projectile_location( projectile) projectile_velocity = self.helpers.calc_projectile_velocity( projectile) rocket2projectile = RelativeObjects( rocket_location, projectile_location, rocket_velocity, projectile_velocity, ) ( min_dist, _, (rocket_location_min_dist, projectile_location_min_dist), ) = rocket2projectile.minimum_distance_between_objects() buffer = safety_factor * self.parameters.rocket.target_radius if min_dist > buffer: continue output = rocket2projectile.time_objects_first_within_distance( buffer) if output: # Rocket is currently outside buffer ( time_rocket_enters_projectile_buffer, (rocket_location_enter_buffer, projectile_location_enter_buffer), ) = output # Check if the collision is out of bounds if not self.helpers.is_within_bounds( rocket_location_enter_buffer ) or not self.helpers.is_within_bounds( projectile_location_enter_buffer): continue current_dist = ( self.controller_helpers. calc_dist_between_rocket_and_projectile(projectile)) try: avoidance_strength = 1 / ( min_dist * (current_dist - self.parameters.rocket.target_radius)) except ZeroDivisionError: avoidance_strength = float("inf") avoidance_angle = (rocket_location_min_dist - projectile_location_min_dist).angle within_buffer_projectile_avoidance.append( PolarCoordinate(avoidance_strength, avoidance_angle).pol2cart()) return (average(within_buffer_projectile_avoidance) if within_buffer_projectile_avoidance else Coordinate(0.0, 0.0))
def _calc_obstacle_shadow_attraction(self): obstacle_shadow_attraction = [] for obstacle in self.parameters.environment.obstacles: if not self._is_closest_point_in_shadow(obstacle.location): continue obstacle_shadow_attraction.append( self._calc_single_obstacle_shadow_attraction(obstacle)) return (average(obstacle_shadow_attraction) if obstacle_shadow_attraction else Coordinate(0.0, 0.0))
def _calc_intersecting_projectile_avoidance(self): rocket_location = self.history.rocket.location rocket_velocity = self.physics.calc_rocket_velocity() intersecting_projectile_avoidance = [] for projectile in self.history.active_projectiles: projectile_location = self.helpers.calc_projectile_location( projectile) projectile_velocity = self.helpers.calc_projectile_velocity( projectile) rocket2projectile = RelativeObjects( rocket_location, projectile_location, rocket_velocity, projectile_velocity, ) ( min_dist, _, (rocket_location_min_dist, projectile_location_min_dist), ) = rocket2projectile.minimum_distance_between_objects() # Check if the projectile will ever get close enough threshold = self.parameters.rocket.target_radius if min_dist > threshold: continue ( time_projectile_hits_rocket, (rocket_location_hit, projectile_location_hit), ) = rocket2projectile.time_objects_first_within_distance(threshold) # Check if the collision is out of bounds if not self.helpers.is_within_bounds( rocket_location_hit) or not self.helpers.is_within_bounds( projectile_location_hit): continue dist2collision = rocket_location.distance2(rocket_location_hit) try: avoidance_strength = 1 / (min_dist * dist2collision) except ZeroDivisionError: avoidance_strength = float("inf") avoidance_angle = (rocket_location_min_dist - projectile_location_min_dist).angle intersecting_projectile_avoidance.append( PolarCoordinate(avoidance_strength, avoidance_angle).pol2cart()) return (average(intersecting_projectile_avoidance) if intersecting_projectile_avoidance else Coordinate(0.0, 0.0))
def _calc_projectile_avoidance(self): rocket_location = self.history.rocket.location projectile_avoidance = [] for projectile in self.history.active_projectiles: delta = rocket_location - self.helpers.calc_projectile_location( projectile) avoidance_strength = 1 / (delta.magnitude - self.parameters.rocket.target_radius) projectile_avoidance.append( PolarCoordinate(avoidance_strength, delta.angle).pol2cart()) return (average(projectile_avoidance) if projectile_avoidance else Coordinate(0.0, 0.0))
def _calc_obstacle_avoidance(self): rocket_location = self.history.rocket.location obstacle_avoidance = [] for obstacle in self.parameters.environment.obstacles: delta = rocket_location - obstacle.location avoidance_strength = 1 / (delta.magnitude - obstacle.radius - self.parameters.rocket.target_radius) obstacle_avoidance.append( PolarCoordinate( avoidance_strength, delta.angle, ).pol2cart()) return (average(obstacle_avoidance) if obstacle_avoidance else Coordinate(0.0, 0.0))
def _calc_within_buffer_obstacle_avoidance(self, safety_factor=2.0): rocket_location = self.history.rocket.location rocket_velocity = self.physics.calc_rocket_velocity() within_buffer_obstacle_avoidance = [] for obstacle in self.parameters.environment.obstacles: rocket2obstacle = RelativeObjects(rocket_location, obstacle.location, rocket_velocity) ( min_dist, _, (rocket_location_min_dist, _), ) = rocket2obstacle.minimum_distance_between_objects() buffer = safety_factor * (self.parameters.rocket.target_radius + obstacle.radius) if min_dist > buffer: continue # Rocket doesn't come within buffer output = rocket2obstacle.time_objects_first_within_distance(buffer) if output: # Rocket is currently outside buffer time_rocket_enter_buffer, (rocket_location_enter_buffer, _) = output if not self.helpers.is_within_bounds( rocket_location_enter_buffer): continue # Rocket will be out of bounds before it enters buffer try: avoidance_strength = 1 / min_dist except ZeroDivisionError: avoidance_strength = float("inf") avoidance_angle = (rocket_location_min_dist - obstacle.location).angle within_buffer_obstacle_avoidance.append( PolarCoordinate(avoidance_strength, avoidance_angle).pol2cart()) return (average(within_buffer_obstacle_avoidance) if within_buffer_obstacle_avoidance else Coordinate(0.0, 0.0))
def _calc_intersecting_obstacle_avoidance(self): rocket_location = self.history.rocket.location rocket_velocity = self.physics.calc_rocket_velocity() intersecting_obstacle_avoidance = [] for obstacle in self.parameters.environment.obstacles: rocket2obstacle = RelativeObjects(rocket_location, obstacle.location, rocket_velocity) ( min_dist, _, (rocket_location_min_dist, _), ) = rocket2obstacle.minimum_distance_between_objects() threshold = self.parameters.rocket.target_radius + obstacle.radius if min_dist <= threshold and self.helpers.is_within_bounds( rocket_location_min_dist): _, ( rocket_location_thresh_dist, _, ) = rocket2obstacle.time_objects_first_within_distance( threshold) dist2threshold = rocket_location.distance2( rocket_location_thresh_dist) try: avoidance_strength = obstacle.radius / (min_dist * dist2threshold) except ZeroDivisionError: avoidance_strength = float("inf") avoidance_angle = (rocket_location_min_dist - obstacle.location).angle intersecting_obstacle_avoidance.append( PolarCoordinate(avoidance_strength, avoidance_angle).pol2cart()) return (average(intersecting_obstacle_avoidance) if intersecting_obstacle_avoidance else Coordinate(0.0, 0.0))