def check_velocity_constraint(entity1, entity2, max_speed): """ :param entity1: :type entity1: Entity :param entity2: :type entity2: Entity :return: :rtype: """ origin = Position.from_entity(entity1) b = Position.from_entity(entity2) - origin d = math.sqrt(b.x**2 + b.x**2) r = entity1.radius + entity2.radius if d - r < 2 * max_speed: p1, p2 = find_adhesion_cone_circle_centered(b, r) # diff_vel = entity1.vel - entity2.vel # orthogonal because tangent definition ray_normals = [b - p1, b - p2] np.array([[ray_normals[0].x, ray_normals[0].y], [ray_normals[0].x, ray_normals[0].y]]) def constraint(diff_vel): return (ray_normals[0] * diff_vel < 0 or ray_normals[1] * diff_vel < 0 or b * diff_vel < 1 - r / d) else: constraint = None return constraint
def velocity_constraint_parabola(entity1, entity2, max_speed): origin = Position.from_entity(entity1) b = Position.from_entity(entity2) - origin r = entity1.radius + entity2.radius norm_b = Position.normed(b) orth_b = Position.orth(norm_b) apex = b - norm_b * r # velocity difference as if the apex was the origin, # simplify operations later diff_vel = entity1.vel - entity2.vel - apex constraint = diff_vel.dot(norm_b)**2 - 2 * r * diff_vel.dot(orth_b) < 0 return constraint
def assign_targets(self): """ Assign a target for each ship using planet attractivity The attractivity is discounted by the distance """ my_ships = self.my_undocked_ships planets = self.all_planets() ship_pos = np.array([[e.x, e.y] for e in my_ships]) planet_pos = np.array([[e.x, e.y] for e in planets]) planet_att = np.array([e.attractivity_level for e in planets]) n_ship = ship_pos.shape[0] n_planet = planet_pos.shape[0] diff_pos = (np.broadcast_to(ship_pos.reshape(n_ship, 1, 2), (n_ship, n_planet, 2)) - np.broadcast_to(planet_pos.reshape(1, n_planet, 2), (n_ship, n_planet, 2))) dist = np.sum(diff_pos * diff_pos, axis=2) dist = np.power(dist, DIST_POWER) attractivity = dist * planet_att for i, ship in enumerate(my_ships): planet_ind = np.argmax(attractivity[i, :]) ship.target = Position.from_entity(planets[planet_ind])