Step by Step Explanation of A* Pathfinding Algorithm in Java

Поділитися
Вставка
  • Опубліковано 9 лис 2024

КОМЕНТАРІ • 73

  • @RyiSnow
    @RyiSnow  2 роки тому +23

    *CORRECTION*
    41:44 I used the "step" variable in the search() method but it was supposed to be in the autoSearch() method! It was my silly mistake. (Thank you Aung Linn for pointing this out)
    Also, I realized I didn't use the checkedList at all in this video. I thought I might use it to check what nodes are checked and when I reset the search result but we can handle both with the checked boolean so the list was not really necessary :P

    • @poLirLANCER
      @poLirLANCER Рік тому

      hello maybe there are some mistakes,300steps is easily passed by,we haven't set the FPS,so one ENTER key doesn't means many times?

  • @jamesconwell2767
    @jamesconwell2767 2 роки тому +22

    Theres no way, Ive been following your tutorials and just had to make my own A star pathfinding algorithm in java, I finished it yesterday and now today you upload a video on it.

  • @od1367
    @od1367 2 роки тому +20

    Wow you are the first person to make a great algothim demo in java! Thank you so much! Your video's mean so much!

  • @mousabthebest8587
    @mousabthebest8587 2 роки тому +4

    Congrats! You are the first person to make me understand A*

    • @RyiSnow
      @RyiSnow  2 роки тому +1

      That's great to hear!

    • @mousabthebest8587
      @mousabthebest8587 2 роки тому

      @@RyiSnow thank you so much for your help I am making a java game and this will help me

    • @mousabthebest8587
      @mousabthebest8587 2 роки тому +1

      @@RyiSnow I have searched A* in google and I didnt understand anything

  • @NoahMoussa-su2yd
    @NoahMoussa-su2yd 9 місяців тому +1

    Hello, I am very grateful to you for this video! Thank you so much. It works; One tip for MacOS users. For me the tiles would not change color by just using the method setBackground(); For example to make the start tile blue, I had to add setOpaque(true) and setBoarderPainted(false) as follows.
    public void setAsStart()
    setBackground(Color.BLUE);
    setForeground(Color.WHITE);
    setText("S");
    setOpaque(true); // ensure that the button completely covers any background behind it.
    setBorderPainted(false); // if set to true The button will draw its border.
    start = true;
    }

    • @aprilpolubiec3097
      @aprilpolubiec3097 8 місяців тому

      Thank you!!!! I also had to use setFocusPainted(false); to get the button outline to go away:
      public Node(int col, int row) {
      this.col = col;
      this.row = row;
      setBackground(Color.white);
      setForeground(Color.black);
      setOpaque(true); // ensure that the button completely covers any background behind it.
      setBorderPainted(false); // if set to true The button will draw its border.
      setFocusPainted(false);
      addActionListener(this);
      }

  • @lucaxtal
    @lucaxtal 2 роки тому +4

    Great!!! I just implemented it too... I also learned this: To move many entities, there is also the solution using Flow Fields, its algorithm is similar to A* one, I implemented it in Java recently and the performance are good. With it you have to generate just one field for all the entities, the drawback is you have to generate one vector for each tile of the map, so for big maps you have to make tradeoffs. But with many entities it should outperform A* calculation for each entity.

  • @johndoubleub
    @johndoubleub 9 місяців тому

    Really good tutorial! I'm not coding in Java but I was able to follow along just fine implementing this in a C# based project. I was quite suprised at how intuitive A* pathfinding is in terms of set up

  • @JaGEM09
    @JaGEM09 2 роки тому +1

    I sincerely want to thank you very much for giving me the energy to love coding even more

  • @rlfpotatoking9253
    @rlfpotatoking9253 6 місяців тому

    Thank you very much, I was struggling to understand A* pathfinding, but this video helped me so much and I adapted it into my project which uses MVC architecture. Great tutorial.

  • @STERGIOSTOULIOUDAS
    @STERGIOSTOULIOUDAS Рік тому

    Just GREAT!! After a lot of searching this is far the best i have seen from all views.. GREAT JOB!

    • @RyiSnow
      @RyiSnow  Рік тому

      Thank you so much 😀

  • @felipemiranda1227
    @felipemiranda1227 2 роки тому +1

    I just found your channel and I'm already a fan. I'm already arranging to watch all your videos. Thank you so much for the content, you are amazing!

    • @RyiSnow
      @RyiSnow  2 роки тому

      Welcome and I'm glad you liked it!

  • @LolBruh
    @LolBruh 2 роки тому +4

    Please continue to create these amazing tutorials!

  • @PedroHenrique-hy7yw
    @PedroHenrique-hy7yw 2 роки тому +1

    Heeey RyiSnow!! That's awesome. I saw that recently in my cs major, really liked to see it here. Thank you for another video!

  • @adnan2734
    @adnan2734 Рік тому +1

    You are a treasure for us, thanks for these tutorials

  • @daryoushkian7011
    @daryoushkian7011 2 роки тому +2

    you dont understand how badly i wanted this video

  • @bartoszjozwicki4541
    @bartoszjozwicki4541 Рік тому

    As always amazing! Worth to mention it is closely related to Breadth First Search very useful for solving programming challenges of different sort!

  • @forte2265
    @forte2265 2 роки тому +1

    i started off watching your videos but stopped and started working on my own but had a nice base to work off of, this is extremely helpful for me!!!! thank you!!!!!

  • @michajurkiewicz5471
    @michajurkiewicz5471 2 роки тому

    Hi,
    I haven't been here for a long time, but I'm slowly coming back. All I can say is that one big WOW.
    Both this algorithm and the trading system. This is so great, I can't wait to do it myself.
    Please tell us if you plan to do quests? If I can ask, I would ask for a minimap and a day and night system. And most importantly, that you develop this project. You are the best java game developer na yt: D

    • @RyiSnow
      @RyiSnow  2 роки тому +1

      Welcome back Michał. No plan for implementing quest system but since I've received a lot of requests for that maybe I will make it once all the essential mechanics are taken care of.

  • @3bood_kr
    @3bood_kr Рік тому

    Wow. This is perfect.
    This will make my pacman's ghost ai so much better. Thank you.

  • @edrperez
    @edrperez 2 роки тому +1

    This is cool! Very well done and meticulously explained.

    • @RyiSnow
      @RyiSnow  2 роки тому +1

      Thanks Edgar, glad you liked it!

  • @aunglinn6377
    @aunglinn6377 2 роки тому +2

    Wow!! I have been trying to implement such an algorithm but you know, I don't understand many of them
    And now, you have uploaded one with full explanation. Thank you.
    One Thing I don't understand is why you put ' step ' variable in " search "(i mean manual search) function? Shouldn't it be in " autoSearch " function?

    • @RyiSnow
      @RyiSnow  2 роки тому +2

      Oooops did I? Yes, the step++ should be in the autoSearch(). That's my mistake. Sorry about that and thanks for pointing this out!

  • @Perfect.Quotes-s2e
    @Perfect.Quotes-s2e 9 місяців тому +1

    Hey Ryi! Please make a video on how to make a grappling hook in Java. I am struggling with it for a week now. Please don't ignore this request

  • @hanazahalkova7271
    @hanazahalkova7271 Рік тому

    Awesome tutorial, even Java begginer like me could understand the principle.
    I just noticed something while trying to implement it myself. The way you used the while loop and step counter doesn't really seem to handle the cases when goal is unreachable. At least it did not for me. Unreachable goal would result in index out of bounds exception.
    Since you placed "step ++" outside the while loop, it seems to me it can't count the repetitions. And it also seems that having "currentNode = openList.get(bestNodeIndex)" outside the forloop leads to a situaton when, at some point, all reachable tiles are checked, the openList is empty and there's nothing left to add (nothing that passes the conditions) and at this point, for loop ends (because openList size is 0) but AFTER that, program tries to get an element from this empty list, which causes the exception.
    So, should "step++" be inside while loop and "currentNode = openList.get(bestNodeIndex)" be inside the for loop (cause that fixed the issue for me perfectly) or am I missing something?

  • @enki9904
    @enki9904 2 роки тому

    Thank you man, this video is very helpful and awesome.

  • @janderdenkeinnerkennt5453
    @janderdenkeinnerkennt5453 2 роки тому

    ayo im first and also i saw a vid of you the other day and started with the code a game series and i think you explain it very well :)

  • @elvispontes4165
    @elvispontes4165 2 роки тому

    Thank you very much for the video.

  • @dash8497
    @dash8497 2 роки тому

    Great Video as always!

  • @MrPokerGaming
    @MrPokerGaming 2 роки тому

    Such a legend

  • @eduardciobanu5450
    @eduardciobanu5450 2 роки тому +1

    Hi! Can you make a video using databases(mySQL) for the game? Please!

  • @berkayw5781
    @berkayw5781 2 роки тому +4

    Hi Ryi! When i set my nodes like that, it doesn't find the shortest path. Is A* algorithm finds the shortest path or am i missing something in my code?
    //SET START AND GOAL NODE
    setStartNode(4,7);
    setGoalNode(11,5);
    //PLACE SOLID NODES
    setSolidNode(10,2);
    setSolidNode(10,3);
    setSolidNode(10,4);
    setSolidNode(10,5);
    setSolidNode(10,6);
    setSolidNode(10,7);
    setSolidNode(6,2);
    setSolidNode(7,2);
    setSolidNode(8,2);
    setSolidNode(9,2);
    setSolidNode(11,7);
    setSolidNode(12,7);
    setSolidNode(6,1);
    setSolidNode(6,2);
    setSolidNode(6,2);
    setSolidNode(6,1);
    setSolidNode(7,7);

    • @RyiSnow
      @RyiSnow  2 роки тому +3

      Oh, you're right. I tested that setting and the path weren't the shortest one indeed. So I guess the current method doesn't necessarily produce the shortest one. Sorry about that! To always produce the shortest path, I think you need to re-evaluate the path when you set a parent or when you trace back to the start node.

    • @orangomango
      @orangomango Рік тому +1

      @@RyiSnow you should actually check the h cost after the f cost instead of the g cost. That's why it isn't working.

    • @3bood_kr
      @3bood_kr Рік тому +1

      @@orangomango you are right. i just tried both and checking the h cost after the g cost was shorter.

    • @orangomango
      @orangomango Рік тому +1

      @@3bood_kr That makes more sense, because you have to reach the end so the end distance should be the shortest

    • @toastonastick9381
      @toastonastick9381 5 місяців тому

      G cost should be true distance from start node

  • @Stavros887
    @Stavros887 2 роки тому

    Awesome tutorial, thank you so much!
    I'm trying to understand the evaluation part. I tried using the H cost for checking against the distance from the goal after the F cost check. It seems to me that the algorithm is faster that way, am I missing something in the logic?

    • @RyiSnow
      @RyiSnow  2 роки тому

      Hmm, I tested both and both took exactly the same steps to reach the goal.

  • @javierbernardosaura8934
    @javierbernardosaura8934 2 роки тому

    Thank you so much!

  • @tarekgordon4449
    @tarekgordon4449 2 роки тому

    Great video!

  • @firewater1045
    @firewater1045 2 роки тому

    Amazing!

  • @yongtickjun4760
    @yongtickjun4760 Рік тому

    May I ask that is the coding same for Mac OS?

  • @aarushkumar168
    @aarushkumar168 2 роки тому

    Can you make a video for sorting algorithm as well

  • @wasanh8251
    @wasanh8251 5 місяців тому

    If we move diagonally how can we calculate the cost?

  • @MrLoser-ks2xn
    @MrLoser-ks2xn 2 роки тому

    Thanks!

  • @andrasfejes8988
    @andrasfejes8988 2 роки тому

    If I put a Thread.sleep(1000); in the autosearch metodus for presentation, the program runs but the tiles do not change. Why is this?

  • @vipmember3315
    @vipmember3315 2 роки тому

    Yeahhh all righthhhhht You rock dude!

  • @dravenporsch7838
    @dravenporsch7838 6 місяців тому

    So i stored the best path into a list array and made the entity move to it every second. Now the problem is when the entity reaches the goal i want to set the start node to the goal node and set a new goal node position, how do i do that? I hope you can reply❤❤

  • @holmdev
    @holmdev 2 роки тому

    Nice job here

  • @Nekaelia
    @Nekaelia Рік тому

    I think every node's cost should be recalculated every step you make, otherwise you are not finding the shortest path. Am I wrong?

  • @zielony1212
    @zielony1212 Рік тому

    what if I got a one dimensional array for nodes?

  • @quentinfouilleul2651
    @quentinfouilleul2651 2 роки тому

    Nice.

  • @MrLoser-ks2xn
    @MrLoser-ks2xn Рік тому

    🥰🥰🥰

  • @eugenemar1505
    @eugenemar1505 Рік тому

    Hi any git reference to resulting code?

  • @learningCodingWithMe
    @learningCodingWithMe Рік тому

    Can you disponibilize the code somewhere??
    It's hard to follow along without it.

  • @nuhataha5543
    @nuhataha5543 2 роки тому

    could you post code please?

  • @ashishcw6796
    @ashishcw6796 Рік тому

    @RyiSnow
    Great video and it has helped me massively to understand the graph traversing.
    But, I think, a small correction is needed. At 36:33 while defining the best node index, we need to consider hCost and not fcost.
    because despite of we declaring hcost, it's not being utilized anywhere.
    Event at the time of comapring fCost == bestNodeFCost, we are only checking for gCost as a secondary check, Shouldn't it be a hCost(which is heuristic cost, to reach to goal?)
    Without hCost it will be a Dijkstra's Algorithm, with hCost, it will be an A*.
    Anyway, I've made a simple comparison of these both in a video.
    ua-cam.com/video/1v918Tmp_QM/v-deo.html&ab_channel=Ashishcw
    Let me know your thoughts.

  • @mohamedyasser2068
    @mohamedyasser2068 2 роки тому

    please give me the source code

  • @ВасилийПетров-л1й

    where is GIT?

  • @Clipaholick
    @Clipaholick Рік тому

    amazing!!