Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find path to direct neighbour #7

Open
codevogel opened this issue Apr 2, 2019 · 3 comments
Open

Find path to direct neighbour #7

codevogel opened this issue Apr 2, 2019 · 3 comments

Comments

@codevogel
Copy link

codevogel commented Apr 2, 2019

It could be that you left this out for optimisation reasons, seeing as you could easily implement a way to check if one of the neighbours is your target tile before even calling the regular algorithm,

But the algorithms implementation (at least after episode 5) cannot find the path to it's direct neighbour.

Here's a proposed fix:

// in Pathfinding.cs

// in
    Vector3[] SimplifyPath(List<Tile> path)
    {
        List<Vector3> waypoints = new List<Vector3>();
        Vector2 directionOld = Vector2.zero;

        // ***************
        // Add this to make sure we do not just ignore the case where path.Count == 1
        // before even checking the rest
        //  **************
        if (path.Count == 1)
        {
            waypoints.Add(path[0].worldPos);
            return waypoints.ToArray();
        }

        for (int i = 1; i < path.Count; i++)
        {
            Vector2 directionNew = new Vector2(path[i - 1].gridCoords.x - path[i].gridCoords.x,
                                               path[i - 1].gridCoords.y - path[i].gridCoords.y);
            if (directionNew != directionOld)
            {
                // Also of importance: 
                // We start at i = 1, but we need to check all the way to the origin! so [i-1]
                waypoints.Add(path[i-1].worldPos);
            }
            directionOld = directionNew;
        }
        return waypoints.ToArray();
    }
@vaillancourt
Copy link

There is a bug in the code, as pointed out by user DMGregory on Gamedev.Stackexchange:

In file Node.cs,

return gCost = hCost;

should be

return gCost + hCost;

Fixing this bug in the code could get the rest of the code to work, so the solution you propose might not be needed.

@Blaxor
Copy link

Blaxor commented Dec 20, 2020

It could be that you left this out for optimisation reasons, seeing as you could easily implement a way to check if one of the neighbours is your target tile before even calling the regular algorithm,

But the algorithms implementation (at least after episode 5) cannot find the path to it's direct neighbour.

Here's a proposed fix:

// in Pathfinding.cs

// in
    Vector3[] SimplifyPath(List<Tile> path)
    {
        List<Vector3> waypoints = new List<Vector3>();
        Vector2 directionOld = Vector2.zero;

        // ***************
        // Add this to make sure we do not just ignore the case where path.Count == 1
        // before even checking the rest
        //  **************
        if (path.Count == 1)
        {
            waypoints.Add(path[0].worldPos);
            return waypoints.ToArray();
        }

        for (int i = 1; i < path.Count; i++)
        {
            Vector2 directionNew = new Vector2(path[i - 1].gridCoords.x - path[i].gridCoords.x,
                                               path[i - 1].gridCoords.y - path[i].gridCoords.y);
            if (directionNew != directionOld)
            {
                // Also of importance: 
                // We start at i = 1, but we need to check all the way to the origin! so [i-1]
                waypoints.Add(path[i-1].worldPos);
            }
            directionOld = directionNew;
        }
        return waypoints.ToArray();
    }

thx.

@ghost
Copy link

ghost commented Feb 16, 2022

Complementing the suggested topic optimization, add the commented line after the while loop:

	// In Pathfinding.cs:
	
	Vector3[] RetracePath(Node startNode, Node endNode) {
		
		List<Node> path = new List<Node>();
		Node currentNode = endNode;
		
		while (currentNode != startNode) {
			path.Add(currentNode);
			currentNode = currentNode.parent;
		}
		
		path.Add(currentNode); // Be sure to add this line, or the nearest neighbour will be skipped;

		Vector3[] waypoints = SimplifyPath(path);
		Array.Reverse(waypoints);

		grid.path = path;
		return waypoints;
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants