Step by Step Explanation of A* Pathfinding Algorithm in Java

Поделиться
HTML-код
  • Опубликовано: 28 авг 2024

Комментарии • 73

  • @RyiSnow
    @RyiSnow  2 года назад +21

    *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

    • @user-yx5rj2jv2d
      @user-yx5rj2jv2d Год назад

      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 года назад +20

    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 года назад +19

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

  • @NoahMoussa-su2yd
    @NoahMoussa-su2yd 7 месяцев назад +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 5 месяцев назад

      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.

  • @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 года назад

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

  • @Jagem_Kang
    @Jagem_Kang 2 года назад +1

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

  • @LolBruh
    @LolBruh 2 года назад +4

    Please continue to create these amazing tutorials!

  • @johndoubleub
    @johndoubleub 7 месяцев назад

    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

  • @rlfpotatoking9253
    @rlfpotatoking9253 4 месяца назад

    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.

  • @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!

  • @daryoushkian7011
    @daryoushkian7011 2 года назад +2

    you dont understand how badly i wanted this video

  • @adnan2734
    @adnan2734 Год назад +1

    You are a treasure for us, thanks for these tutorials

  • @STERGIOSTOULIOUDAS
    @STERGIOSTOULIOUDAS 9 месяцев назад

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

    • @RyiSnow
      @RyiSnow  9 месяцев назад

      Thank you so much 😀

  • @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!

  • @edrperez
    @edrperez 2 года назад +1

    This is cool! Very well done and meticulously explained.

    • @RyiSnow
      @RyiSnow  2 года назад +1

      Thanks Edgar, glad you liked it!

  • @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!!!!!

  • @3bood_kr
    @3bood_kr Год назад

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

  • @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!

  • @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.

  • @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!

  • @enki9904
    @enki9904 Год назад

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

  • @user-jo8ju9ri9c
    @user-jo8ju9ri9c 7 месяцев назад +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

  • @elvispontes4165
    @elvispontes4165 2 года назад

    Thank you very much for the video.

  • @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 3 месяца назад

      G cost should be true distance from start node

  • @dash8497
    @dash8497 2 года назад

    Great Video as always!

  • @dravenporsch7838
    @dravenporsch7838 3 месяца назад

    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❤❤

  • @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 :)

  • @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?

  • @MrPokerGaming
    @MrPokerGaming 2 года назад

    Such a legend

  • @wasanh8251
    @wasanh8251 3 месяца назад

    If we move diagonally how can we calculate the cost?

  • @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?

  • @javierbernardosaura8934
    @javierbernardosaura8934 2 года назад

    Thank you so much!

  • @yongtickjun4760
    @yongtickjun4760 10 месяцев назад

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

  • @andrasfejes8988
    @andrasfejes8988 Год назад

    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?

  • @zielony1212
    @zielony1212 Год назад

    what if I got a one dimensional array for nodes?

  • @tarekgordon4449
    @tarekgordon4449 Год назад

    Great video!

  • @eduardciobanu5450
    @eduardciobanu5450 2 года назад +1

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

  • @firewater1045
    @firewater1045 2 года назад

    Amazing!

  • @vipmember3315
    @vipmember3315 2 года назад

    Yeahhh all righthhhhht You rock dude!

  • @holmdev
    @holmdev 2 года назад

    Nice job here

  • @Clipaholick
    @Clipaholick Год назад

    amazing!!

  • @MrLoser-ks2xn
    @MrLoser-ks2xn Год назад

    Thanks!

  • @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.

  • @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.

  • @aarushkumar168
    @aarushkumar168 2 года назад

    Can you make a video for sorting algorithm as well

  • @quentinfouilleul2651
    @quentinfouilleul2651 2 года назад

    Nice.

  • @MrLoser-ks2xn
    @MrLoser-ks2xn 11 месяцев назад

    🥰🥰🥰

  • @nuhataha5543
    @nuhataha5543 2 года назад

    could you post code please?

  • @mohamedyasser2068
    @mohamedyasser2068 2 года назад

    please give me the source code

  • @ashishcw6796
    @ashishcw6796 10 месяцев назад

    @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.
    ruclips.net/video/1v918Tmp_QM/видео.html&ab_channel=Ashishcw
    Let me know your thoughts.

  • @user-rv9ss5ce7z
    @user-rv9ss5ce7z Год назад

    where is GIT?