Good evening everyone,
After 4 days of trying I still am struggling with implementing a pathfinding to my game.
I want to learn how to completely from scratch write my own, instead of using Boost or anything.
I was hoping that someone with experience can show some examples and -or explain some stuff
to me if possible. It would be highly appreciated.
My questions:
1. How should I build up my map?I have a 800x800 map (pixels of course lol). I would like to cut it in nodes of 40x40 pixels.
I am using sprites (at this moment only RectangleShapes, but will be sprites) which I will be placing on my map (PNG images).
I store all my sprites in a vector, so that the sprites (textures) will exists as long as the program is running. If I wouldnt do that, the program would crash and -or result in an error.
So what I have is:
- 800 x 800 SFML Window
- Class that draws the sprites in the window (lets say 2 walls in this example) named level1.h
- Vector (level1.h) which holds all the sprites that I need to draw in the level
- Class that draws the player sprite in the window named player.h
Is this the correct way? or should I store stuff differently when making use of A* pathfinding?
2. How do I add the grid?is this just
screenWidth / 40 (pixels) + screenHeight / 40 (pixels) = amount and then draw RectangleShape's all over the window, so I have a visual grid (which wont be displayed in the final version of course)? so basicly its just a grid for our reference so we can see what and where during the development, and to get pixels (width and height) for our codes.
3. Now I have the grid, how should I add the path? i.e. where sprites may or may not walk. Where my spirites are drawn on the map, no walking is allowed. If no sprite is there, then walking should be allowed.
- Should I just make a vector with all the nodes in it (x-pos and y-pos) which are allowed to walk on?
4. Now we know where we are allowed to walk, how and what code should I implement? I am struggeling to think of / write the right alchoritm.I know it has to be something like the following illustration (sorry for my poor drawing skills lol) but I have no clue on how to code this into a while loop or what so ever.
5. How to move the sprite to the target following the best way possible which was calculated in the A* alchoritm.Well, I hope someone can help me to get on the right track.
Best regards
P.S. Some info on my sample picture so you know what I meant:Green square = player
Red square = a enemie which will walk towards you (I know it says finish lol)
Blue is a wall, which cannot be walked over and -or through
Yellow line is the most efficiƫnt line. Because you go diagonal each movement would cost 7 points (for example). In total the costs for getting to the finish would be: 35
The orange line is an alternative line (just as example), the total costs would be: 120 points.
This because going horizontal or vertical costs 10 points.
EDIT:I found a good sample picture of what I meant: