Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Setting up SFML and using A* for path finding.  (Read 10748 times)

0 Members and 1 Guest are viewing this topic.

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Setting up SFML and using A* for path finding.
« on: February 03, 2012, 02:06:04 am »
I'm working on some path finding codes and ran across A* and I'm lost about the conversion from the tutorial that has some DirectX in it.  I need a few ideas on what to do.

Here's the tutorial and the code that came with it.

http://
http://www.policyalmanac.org/games/aStarTutorial.htm

http://
https://legacy.sfmluploads.org/file/103


I need a little help converting this so I can tinker with it a little better.

At the moment I'm using SFML 1.6 and as soon as I fix an annoying issue I'll be using SFML 2.0 .

Anyone want to help?
All I need are some ideas and code setups to get parts of this working.
I have many ideas but need the help of others to find way to make use of them.

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Setting up SFML and using A* for path finding.
« Reply #1 on: February 03, 2012, 02:23:38 am »
I found the same tutorial around this time last year when doing an assignment, I got it to work and still have the code, I'll dig around later and post what I came up with here if you want.

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Setting up SFML and using A* for path finding.
« Reply #2 on: February 03, 2012, 02:27:34 am »
Quote from: "thePyro_13"
I found the same tutorial around this time last year when doing an assignment, I got it to work and still have the code, I'll dig around later and post what I came up with here if you want.


Thanks. :)

What's funny is I'm also working on an external library version of this for an engine me and my classmates use so if I can get this working and understand it I'll be able to make a few changes and drag and drop it into the engine we use. :)
I have many ideas but need the help of others to find way to make use of them.

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Setting up SFML and using A* for path finding.
« Reply #3 on: February 03, 2012, 03:20:29 am »
It isn't as hard as I'd thought it would be to change up the AStar Header file so it could be used in the Engine my class uses. Luckily for that all I have to do is split it into both a header and source file than I'm going to pop open the Fun Editor (The engine we use) and see if it works. :)

In the meantime anyone have some more ideas on the SFML version?

For some reason me + candy = impatient. :)
I have many ideas but need the help of others to find way to make use of them.

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Setting up SFML and using A* for path finding.
« Reply #4 on: February 03, 2012, 03:41:08 am »
EDIT: just read your post.

I don't think you need to do much to integrate it, you should be splitting logic and rendering as much as possible, and so SFML doesn't need to know anything about what pathfinding method you use. Just make sure you expose your terrain in a format that is easy to run the algorithm through.

OLD post:
I've uploaded it here.

The code uses SFML 1.6, though only the main.cpp actually references SFML, everything else is independent. It should compile fine with 1.6 and only needs a few modifications to work with 2.0

The algorithm i developed uses a Terrain graph, which consists of just a container of Nodes, linked by Edges. This is just to simplify the path-finding(mostly for the cost of moving between nodes, which is stored in the Edge). Its fairly simple, so you shouldn't have too much trouble replacing it which your own map format.

The actual A* algorithm spits out a path(queue of vector2 positions). The vectors correspond to tile positions.

And I use my own vector2 class to reduce the couple with SFML(this is just to demonstrate I understand the concept, assignment and all).

The algorithm goes something like this:

Create a list(the OPEN list) and place your starting tile in the list. This list holds the tiles that you are about to check, so any  unchecked tile adjacent to a closed tile.

Also create a closed list. You close a tile be removing it from the open list and adding it to the closed list. These are tiles you have checked and calculated movement costs for.

I have a third list to keep track of the current cost of each node(the cost includes the travel time from the start position, so later nodes as much more expensive than nodes adjacent to the start node). Add data to this list as you close tiles.

Then for each iteration:

Pick a tile from the open list(you can use a herustic to search in a specific direction, otherwise you end up searching in a circle) and close it, then add all tiles adjacent to this tile to the open list.

Each time you add a tile to the closed list, you create a pointer that points back to the closed tile that you came from previously, this way, for any tile in the closed list, their is a path of pointers leading back to the start position.

If you find a node that is already closed, then check to see if you current path to it is cheaper than the one stored, and replace it if so.

Keep repeating this until the tile chosen is the end point you were looking for.

I don't know if I'm good at explaining it. :p
But hopefully you can understand my code. It took me quite some time(spent reading the earlier link and Wikipedia) before I could understand it.

Note the algorithm is slow in debug, I really should have split the logic so that it could run over multiple frames. This may not be necessary depending on the size of your game world.

This post got pretty big, but I hope it helps. :D

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Setting up SFML and using A* for path finding.
« Reply #5 on: February 03, 2012, 04:21:29 am »
Thanks,  I was looking through the AStar tutorial Codes and couldn't figure out how it told the AI sprites what direction to go in and what I should link into my vector controlling equation. :?:

I'll look through and see what changes you made so you could link into the vector controls. :)
I have many ideas but need the help of others to find way to make use of them.

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Setting up SFML and using A* for path finding.
« Reply #6 on: February 03, 2012, 04:28:37 am »
I should say though, I made mine from scratch using the info on the page, so my code probably won't look very similar to the example code from that page.

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Setting up SFML and using A* for path finding.
« Reply #7 on: February 03, 2012, 04:47:24 am »
Quote from: "thePyro_13"
I should say though, I made mine from scratch using the info on the page, so my code probably won't look very similar to the example code from that page.



For some reason I couldn't figure out how to make functions from the info on the page.  I have no clue why. :?: :?: :?: :shock: It could be I hate reading large amounts of text or something.


The map I'm has 250 pixel wide and tall grids and is over 3200 pixels wide and tall.

I got greedy if you were wondering and was testing the limits of the engine we are using. :)

Any ideas on how the tutorial code sends information to the vectors telling the sprite how to move?  I see some functions that say they move the sprites but I'm unsure about what is being used as the vector information.
I have many ideas but need the help of others to find way to make use of them.

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Setting up SFML and using A* for path finding.
« Reply #8 on: February 03, 2012, 05:18:15 am »
I'm not to fond of plain C, and am quite unfamiliar with it. It looks like FindPath saves the end path to a global array indexed by the pathID.

Looks like Readpath formats the data? or sets the global so that you can easily read the path for the current pathID(seems to be one pathID per entity).

EDIT: readpath accepts your pathID and your current position, and formats xpath/ypath so that xpath[id]/ypath[id] are the coordinates of the next point in the path.

In the case of the maze app, the movesprite function is still reading from the global xPath/yPath variables(part of aStarLibrary) to find out where the draw the sprite next.

Though I would have to spend much longer playing with it before I'd be confident using any of that code.

You might find an easier time modifying my code as it's written in C++ and seems much smaller that the example library.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Setting up SFML and using A* for path finding.
« Reply #9 on: February 03, 2012, 08:38:55 pm »
By the way, Boost.Graph contains already a finished implementation of the A* algorithm.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Setting up SFML and using A* for path finding.
« Reply #10 on: February 04, 2012, 05:18:56 pm »
Quote from: "Nexus"
By the way, Boost.Graph contains already a finished implementation of the A* algorithm.



I still want to be able to make my own without getting a headache though.

It should be helpful when my maps or grids go above a certain size though.
I have many ideas but need the help of others to find way to make use of them.

Dreadi

  • Newbie
  • *
  • Posts: 1
    • ICQ Messenger - 95406325
    • View Profile
MicroPather
« Reply #11 on: February 09, 2012, 01:53:39 am »
I'd recommend MicroPather. It's a generic, small, fast and easy to use A* implementation. Without any requirements on the host program to change its data structures or objects.

http://www.grinninglizard.com/MicroPather/

Give it a try :wink:

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Setting up SFML and using A* for path finding.
« Reply #12 on: February 09, 2012, 05:12:15 am »
Nice tutorial. :)

I think I have an idea for grid based path finding but anyone have ideas on using Vectors and the detection of other objects in the area to find paths?

Me and a few classmates of mine have been a bit puzzled about this and need some good resources on this type of AI.
I have many ideas but need the help of others to find way to make use of them.