Search This Blog

Featured post

Welcome

Tuesday 28 March 2023

A* Pathfinding

Made my first attempt at A* pathfinding with Raylib using this tutorial Path Planning - A* (A-Star

With a bit of editing to get the code to work with Raylib and my game code I managed to get a functional A* path to be created. The main parts that needed to be edited where the game engine references and some function variables to work with my game.


This Gif was taken after I figured out how to add my Raylib collision rectangles into node space of the A*. This would have been a lot easier if I had added the pathfinding code at the start of making the game.



First attempt at getting the object to walk along the path to the target location. As you can see it did not work to well.

Second attempt worked better. At least the bear was walking along the path.


Third attempt much better. I just had the end and start locations mixed up. The game itself is not in node space. A* adds a layer of nodes over the map that is uses to navigate. I need to translate the location of the agents from world space to node space and that was where I was having problems.

Forth attempt looks like it worked but it didn't. The bear wasn't really following the right path. What was happening here was the code was refreshing the path to fast and the yellow line wasn't. Also the speed of the bear was off. 

Fifth attempt was very good. In this gif I have the obstacle nodes drawn so I could make sure the bear was following the path properly. Unfortunately there was an issue with this one, that was the node space was to small the bears location wasn't using it. So sometimes when trying to walk around an obstacle there node location would be change to the obstacle location instead of the node next to it.

The finished product. A* is working almost perfectly in game. There has been no major frame drops with multiple wolfs using the code. I also manage to update the node space to remove the tree obstacle once said tree is cut down.

That was my journey in making A* for the first time. There is a fair bit I didn't document.

 I'll leave you with a picture of the first installment of the wolfs having access to the A* code. This is before I added the agents (wolfs) location and the trees to node space.