- Home /
 
 
               Question by 
               tomaskillian41 · Feb 02 at 05:33 PM · 
                scripting problempathfinding  
              
 
              Duda script A Start Pathfinder
Hi, I'm trying to implement A* Start in unity but I don't understand why the same path in reverse fills the closed list with more nodes, example I pass the starting position Vector 2(2,2) and the target Vector(10 ,12) the closed list ends with 200 nodes but if I do the same thing but in reverse closed it ends with 448 nodes.
 class NODO : IComparable
 {
     public Posicion Pos;
     public float F,G;
     public bool Ocupado = false;
     public NODO Next;
     public NODO(Posicion _Pos)
     {
         Pos = _Pos;
     }
     public int CompareTo(object obj)
     {
         if (obj == null) return -1;
         NODO N = (NODO)obj;
         if (N != null)
         {
             return this.F.CompareTo(N.F);
         }
         else
         {
             throw new NotImplementedException();
         }
     }
 
                public class PathfinderA : MonoBehaviour
 {
     NODO[,] Grid;
     int Ancho, Alto;
     public void IniciarGridNodos(int _Ancho, int _Alto)
     {
         Ancho = _Ancho;
         Alto = _Alto;
         Grid = new NODO[Ancho, Alto];
         for (int x = 0; x < Ancho; x++)
         {
             for (int y = 0; y < Alto; y++)
             {
                 Grid[x, y] = new NODO(new Posicion(x, y));
             }
         }
     }
     public void Pathfinder(Vector2 Inicio, Vector2 Target)
     {
         NODO inicio = Grid[(int)Inicio.x, (int)Inicio.y];
         NODO target = Grid[(int)Target.x, (int)Target.y];
         List<NODO> Abierta = new List<NODO>();
         List<NODO> Cerrada = new List<NODO>();
         Abierta.Add(inicio);
         while (Abierta.Count > 0)
         {
             Abierta.Sort();
             NODO Actual = Abierta[0];
             if (Actual == target)
             {
                 //Camino Encontrado
                 Debug.Log("Camino Encontrado");
                 break;
             }
             //Buscar Adyasentes y Calcular Costos
             List<NODO> Adyasentes = GetAdyasentes(Actual);
             for (int i = 0; i < Adyasentes.Count; i++)
             {
                 if (!Cerrada.Contains(Adyasentes[i]) && !Adyasentes[i].Ocupado)
                 {
                     float newCostG = Actual.G + 10;
                     if (newCostG < Adyasentes[i].G || !Abierta.Contains(Adyasentes[i]))
                     {
                         Adyasentes[i].G = newCostG;
                         Adyasentes[i].F = newCostG + Vector2.Distance(new Vector2(Adyasentes[i].Pos.x, Adyasentes[i].Pos.y), Target);
                         Adyasentes[i].Next = Actual;
                         if (!Abierta.Contains(Adyasentes[i]))
                         {
                             Abierta.Add(Adyasentes[i]);
                         }
                     }
                 }
             }
             Cerrada.Add(Actual);
             Abierta.RemoveAt(0);
         }
         Debug.Log(Cerrada.Count);
     }
     List<NODO> GetAdyasentes(NODO Actual)
     {
         Posicion Pos = Actual.Pos;
         List<NODO> Adyasentes = new List<NODO>();
         //Izquirda
         if (Pos.x - 1 > 0)
         {
             Adyasentes.Add(Grid[Pos.x - 1, Pos.y]);
         }
         //Abajo
         if (Pos.y - 1 > 0)
         {
             Adyasentes.Add(Grid[Pos.x, Pos.y - 1]);
         }
         //Derecha
         if (Pos.x + 1 < Ancho - 1)
         {
             Adyasentes.Add(Grid[Pos.x + 1, Pos.y]);
         }
         //Arriba
         if (Pos.y + 1 < Alto - 1)
         {
             Adyasentes.Add(Grid[Pos.x, Pos.y + 1]);
         }
         return Adyasentes;
     }
 }
 
               
 
               Comment
              
 
               
              Your answer