Custom made a* Path finding isn't working very well

我们两清 提交于 2021-02-05 09:46:53

问题


I am making a pathfinding algorithm on a map with coordinates, for a project with rivers. I looked at how a* worked, and I started to understand it, so I made it in my own code. The issue is that is doesn't work at all. The pathfinder is only making the first two squares of the river, and then just giving up. I do not know the issue.

Here is my code:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public  class APath 
{
    public bool[,] open;
    private bool[,] closed;
    private float width;
    private float height;
    public int currentX, currentY;

    public void FindPath(int startX, int startY, int endX, int endY)
    {

        open = new bool[75,75];// stores path
        open[startX, startY] = true;
        bool[,] opens = open;
        closed = new bool[75,75] ;
        currentY = startY;
        currentX = startX;
        bool end = false;

        for(int l = 0; l< 1000; l++) //while loop went on forever. Had to use for loop
        {

            float[][] adjacent = Adjacent(currentX,currentY,endX,endY,startX,startY);//gets adjacent squares
            float min = adjacent[0][2];//creates temp min

            int per = 0;// storage of refrence for later

            for(int i = 0; i < 8; i++)// checks adjacent squares
            {




                if (adjacent[i][2] < min)//if there is a new min
                {


                    min = adjacent[i][2];// new min
                    per = i; // for later use
                }
            }
           if(currentX == endX && currentY == endY)// makes sure that adjacent isn't at end place
            {

                open[endX, endY] = true;
            }

            currentX = Mathf.RoundToInt((int)adjacent[(int)per][0]);//new current
            currentY = Mathf.RoundToInt(adjacent[(int)per][1]);


            opens[currentX, currentY] = true;// tracks data

            closed[currentX, currentY] = false;//tracks data

        }
        open = opens;
    }
   float[][] Adjacent (int currentX, int currentY, float endX, float endY, float startX, float startY)
    {
     //creating adjacent squares areound current square
        float[][] adjacent = new float[4][];
        adjacent[0] = new float[3] { currentX + 1, currentY +0, HandG( currentX + 1, currentY,endX,  endY,  startX,  startY) };

       adjacent[1] = new float[3] { currentX + 1, currentY + -1, HandG(currentX + 1, currentY + -1, endX, endY, startX, startY) };

        adjacent[2] = new float[3] { currentX + 0, currentY + -1, HandG(currentX + 0, currentY + -1, endX, endY, startX, startY) };

      adjacent[3] = new float[3] { currentX + -1, currentY +- 01, HandG(currentX + -1, currentY +-1, endX, endY, startX, startY) };

        adjacent[4] = new float[3] { currentX + -1, currentY + 0, HandG(currentX + -1, currentY, endX, endY, startX, startY) };

        adjacent[5] = new float[3] { currentX + -1, currentY + 01, HandG(currentX + -1, currentY + 1, endX, endY, startX, startY) };

        adjacent[6] = new float[3] { currentX,  currentY + 1, HandG(currentX , currentY + 1, endX, endY, startX, startY) };

        adjacent[7] = new float[3] { currentX + 1, currentY + 1, HandG(currentX + 1, currentY + 1, endX, endY, startX, startY) };


        return adjacent;
    }

    float HandG (float currentX, float currentY,float endX, float endY, float startX, float startY )
    {
      //creates g of a*
        float g  = Mathf.Sqrt(Mathf.Pow(currentX + -startX, 2) + Mathf.Pow(currentY + -startY, 2));
       //creates h of a*
        float h = Mathf.Sqrt(Mathf.Pow(currentX + -endX, 2) + Mathf.Pow(currentY + -endY, 2));
        //creates f of a*
        float f =g + h;


        return f;
    }
}

The river class using this

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rivers : MonoBehaviour
{
    // Start is called before the first frame update
   public Color[] MakeRiver (Color[] color, int mapHeight, int mapWidth)
    {
        Color[] colorss = color;// makes color[]
        APath path = new APath();
      path.FindPath(20,20,9,19);// makes path. Set certain values for testing
        bool[,] open = path.open;// gets path from that class
        for(int y = 0;y < mapHeight; y++)
        {
            for(int x = 0; x< mapWidth; x++)
            {
                if(open[x,y] == true)
                {

                    colorss[y * mapWidth + x] = Color.red;// shows the river.
                }
            }
        }




        return colorss;
    }
}

If anybody, is able to see the issue, I would be very grateful. Thank you for reading my question, and have a wonderful, spectacular day.

来源:https://stackoverflow.com/questions/60245811/custom-made-a-path-finding-isnt-working-very-well

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!