Exemple #1
0
def generate_neighbors(node, nodes):
    """
        Auxiliary function that calculate the neighbors of a given node within a navigation mesh.
        Inputs:
            - node: node for which to calculate the neighbors.
            - nodes: list of all the nodes conforming the mesh.
    """
    found = {}
    for n in nodes.values():
        if n is node:
            continue  # We want to ignore the node for which we are currently generating neighbors
        # Calculate the angle of the vector between nodes
        angle = math.degrees(
            math.atan2(n.point[1] - node.point[1], n.point[0] - node.point[0]))
        if angle in found:  # There was already a node in that angle
            if u.los_raycasting(node.point, n.point,
                                u.npdata) == (-1,
                                              -1):  # There is line of sight
                new_dist = np.linalg.norm(
                    (node.point[0] - n.point[0], node.point[1] - n.point[1]))
                cur_dist = np.linalg.norm(
                    (node.point[0] - found[angle].point[0],
                     node.point[1] - found[angle].point[1]))
                if new_dist < cur_dist:  # We have found a better alternative than what we already had
                    found[angle] = n
        else:
            if u.los_raycasting(node.point, n.point,
                                u.npdata) is None:  # There is line of sight
                found[
                    angle] = n  # There was nothing stored, so we just save the current node in that direction
    node.neighbors = found  # Replace the list of neighbors so they can be explored later
 def __can_move(self, spot):
     return u.los_raycasting((self.robot.x, self.robot.y), spot, u.npdata)