def straight_shot_speed(carrom: Carrom, striker_position: Vector2, coin_position: Vector2, pocket_center: Vector2, decelerate, e): board = carrom.board distance_coin_pocket = pocket_center.distance_to(coin_position) distance_striker_coin = striker_position.distance_to(coin_position) col_coin_speed = sqrt(2 * distance_coin_pocket * decelerate) col_striker_speed = col_coin_speed * ( board.COIN_MASS + board.STRIKER_MASS) / ((1 + e) * board.STRIKER_MASS) striker_speed = sqrt(col_striker_speed**2 + 2 * decelerate * distance_striker_coin) return striker_speed
def find_closest_point( points: List[Vector2], position: Vector2, predicate: Callable[[Vector2], bool] = (lambda x: True), ) -> Tuple[int, Vector2]: """ Find the position of closest point in the given unordered points. Optionally, specify a predicate that the node must satisfy. Returns the index of and position of the closest point. """ # apply predicate to select elligible nodes points = list(filter(predicate, points)) closest_vec = None closest_dist = None for index, vec in enumerate(points): dist = position.distance_to(vec) if closest_vec is None: closest_vec = index closest_dist = dist continue if dist < closest_dist: closest_vec = index closest_dist = dist return (closest_vec, points[closest_vec])
def cut_shot_speed(carrom: Carrom, striker_position: Vector2, coin_position: Vector2, expected_position: Vector2, force_angle, pocket_center: Vector2, decelerate, e, dt): board = carrom.board distance_coin_pocket = pocket_center.distance_to(coin_position) """ Added a little more to be on the safer side """ distance_striker_coin = striker_position.distance_to(expected_position) col_coin_speed = sqrt(2 * distance_coin_pocket * decelerate) col_striker_speed = col_coin_speed * (board.COIN_MASS + board.STRIKER_MASS) / \ ((1 + e) * board.STRIKER_MASS * cos(radians(force_angle))) striker_speed = sqrt(col_striker_speed**2 + 2 * decelerate * distance_striker_coin) """ Sometimes speed is not just sufficient add some more, also for better shots, time of hit in terms of dt""" time = (striker_speed - col_striker_speed) / decelerate time = 0.95 * time time = time - (time % dt) striker_speed = (distance_striker_coin + decelerate * time**2 / 2) / time return striker_speed
def find_closest_edge(path: List[Vector2], position: Vector2) -> Dict[str, Vector2]: """Find the closest edge Returns a dictionary: { "vec": edge vector, "foot": foot of perpendicular location, "distance": distance to foot of perpendicular } """ closest_node = find_closest_point(path, position)[0] edge1_foot = foot_on_line( position, path[(closest_node + 1) % len(path)], path[closest_node], ) edge1_vec = path[(closest_node + 1) % len(path)] - path[closest_node] d_to_edge1_foot = position.distance_to(edge1_foot) edge2_foot = foot_on_line( position, path[closest_node - 1], path[closest_node], ) edge2_vec = path[closest_node] - path[closest_node - 1] d_to_edge2_foot = position.distance_to(edge2_foot) if d_to_edge1_foot < d_to_edge2_foot: return { "vec": edge1_vec, "foot": edge1_foot, "distance": d_to_edge1_foot, } return { "vec": edge2_vec, "foot": edge2_foot, "distance": d_to_edge2_foot, }