Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Pathfinding without predefined grid

Discussion in 'Editor & General Support' started by MightySheep, Oct 7, 2015.

  1. MightySheep

    MightySheep

    Joined:
    Sep 23, 2015
    Posts:
    21
    Hello,
    I´m learning Unity making a 2D top-down game. The map in this game is made from 8k*8k block grid. Each block id in the grid has coresponding prefab(all 1 meter square size) and walkability. I tried to customize some A* solutions to use it instead of checking for walkability by rays and such, but i failed. My question is: Is there an easy way to pathfind with A* algorhitm without generating grid of nodes, not having to generate new class for each tile? And as this is surely doable for somebody more advanced, would this even be more useful than generating like 20*20 grid around every NPC?
    Thanks for any answers.
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I'm not sure what you are asking. A* is basically a graph search. Graph searches are inherently node based.

    There is no reason you can't generate nodes on the fly at runtime if needed. Nodes can be fairly light weight, all they need to store is the h-cost, g-cost, a reference to each neighbour and a reference to the previous node. (Sometimes adding in a reference to the nodes position can be valuable too, depending on your implementation).
     
    jpthek9 likes this.
  3. jpthek9

    jpthek9

    Joined:
    Nov 28, 2013
    Posts:
    944
    This. Although there's another key piece to A* which is connections. Basically, every node can connect to another node or not.

    Imagine brute forcing a maze... going down a path until the very end then finding another path to trace. That's what A* is fundamentally. You can define a maze with a grid, at places with turns (aka navmesh), or even a bunch of random nodes that aren't related in space.

    A* assumes that the nodes are related in space so that a node closer to the end point has a higher chance of being on the right track. Conceptually, it's brute forcing a maze but going down the closer routes first.

    Btw caching nodes might solve your problem of creating many new objects. When you no longer need a node, just send it into the cache.

    Code (CSharp):
    1. static readonly Stack<Node> nodeCache = new Stack<Node> ();
    2. public static Node CreateFromCache () {
    3.     return nodeCache.Count > 0 ? nodeCache.Pop () : new Node ();
    4. }
    5. public static void Cache (Node node) {
    6.     nodeCache.Add (node);
    7. }
    It's probably not even worth it though, especially if the nodes are only generated at startup.
     
    Last edited: Oct 8, 2015
    Kiwasi likes this.
  4. MightySheep

    MightySheep

    Joined:
    Sep 23, 2015
    Posts:
    21
    Thanks for your answers guys, guess I'll just have to learn more and write a system for my needs. My concerns were creating seperate node classes for each tile, which is 67108864 for the whole map, and when generating on runtime, the number of nodes I'd have to generate around NPCs. Thank you for the caching idea, I always connected it only with objects like bullets so it didn't come to my mind.

    EDIT: I found Voxelmetric, 3D open-source project with tutorials, and it had really easy to understand A* code with little struct and no big grid generation in Start, I modified it to work in 2D and it's flawless! Sorry guys for bothering you with nooby questions.
     
    Last edited: Oct 8, 2015