Hello Guest  /  Join  /  Sign In  /  Get Desura
Avatar

DJ_Link

David joined Aug 1, 2009

Offline Since
May 22, 2013
Country
Portugal Portugal
Gender
Male
Age
29
Homepage
David-amador.com
Contact
Private Message
Email
Members Only
Steam
^DJ_Link^
XBox Live
Different Link
Skype
the.david.amador

GarrysMod Level

10

Indie Game Developer (like the movie). I made a couple of games and I usually tweet a lot.

Blog RSS Report abuse A* Pathfinding

0 comments by DJ_Link on Oct 28th, 2010

For my current project I've been working with A* as a path-finding solution.

Basically it's a way to get from point A to point B.

This is used for AI in games mostly on tiled based games, It's very simple to understand and implement.

As A* traverses the graph, it follows a path of the lowest known cost, keeping a sorted priority queue of alternate path segments along the way. If, at any point, a segment of the path being traversed has a higher cost than another encountered path segment, it abandons the higher-cost path segment and traverses the lower-cost path segment instead. This process continues until the goal is reached.

The major choke point is the heuristic used to calculate which node to take next. I've started by using Manhattan Distance.
Its primary advantage is that it will generally get you to the target faster than most alternatives. Its primary drawback is that in a 8-way pathfinder so it is not guaranteed to give us the shortest possible path.

code:
float cost = 10*(abs(currentX-targetX) + abs(currentY-targetY))

Although it works fast it's not accurate and if I forced for finding the shortest possible it was rather slow for my 80x44 grid (3520 tiles), sometimes taking 3-4 seconds to find a path.

I decided to take another approach and using Diagonal Shortcut, its primary advantage is that, because it is balanced, it is able to fully consider small modifiers like a turning penalty or influence map modifier like having a non walkable tile, a rock, a tree, whatever. It is a bit slower than the Manhattan method, though.

code:
float cost;
float xDistance = abs(currentX-targetX)
float yDistance = abs(currentY-targetY)
if (xDistance > yDistance)
     cost = 14*yDistance + 10*(xDistance-yDistance)
else
     cost = 14*xDistance + 10*(yDistance-xDistance)

In case you are wondering the 10 and the 14 are to define if it's a diagonal or a horizontal/vertical movement since diagonals take longer to traverse than straight lines of course.
After I'm getting much faster movements.

It's not that Manhattan is worse and you shouldn't use it, it all depends on the application your using.

* On a square grid that allows 4 directions of movement, use Manhattan distance.
* On a square grid that allows 8 directions of movement, use Diagonal distance.

This is a video I've done using Manhattan distance using tile grids generated from a black and white image.

Comments
Wizardum
Wizardum Aug 31 2010, 5:33pm said:

wow um PortuguÊs por aqui
isto hoje em dia é dificil de encontrar

bem boa sorte para o teu projecto, segundo li, estás quase a acabá.lo.
Fico à espera ;)

cheerz

+1 vote     reply to
DJ_Link
DJ_Link Sep 13 2010, 3:43pm replied:

Heheh thanks.
Só vi agora aqui o comment. Pensava que o indieDB alertava. É difícil fazer track a isto tudo.
Sim está mesmo quase o jogo. Vamos lá a ver.

+1 vote     reply to
Post a Comment
click to sign in and comment

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.

Indie Devs
Indie Devs Hobbies & Interests group with 855 members
Desura
Desura Official group with 8,180 members
DIYGamer - An indie group for indie news.
DIYGamer - An indie group for indie news. Entertainment & Press group with 110 members
Spotlight
Spotlight Official group with 437 members
Different Pixel
Different Pixel Developer with 2 members
Friends
jax42
jax42 Online friends since May 22, 2013
jtpup0
jtpup0 friends since Sep 13, 2012
INtense!
INtense! friends since Mar 22, 2012
dcfedor
dcfedor friends since Sep 5, 2012
m0r3n4
m0r3n4 friends since Jul 1, 2010
paranoidMonkey
paranoidMonkey friends since Jun 20, 2010
KopanoGS
KopanoGS friends since Oct 24, 2012
overlordror
overlordror friends since Jun 21, 2010
StefanRomanHosch
StefanRomanHosch friends since Apr 10, 2012
fdpc
fdpc friends since Jan 20, 2013
Accolades
Desura Spotlight
Statistics
Activity Points
373
Rank
5,167 of 327,809
Watchers
0 members
Time Online
1 day
Comments
57
Site Visits
2,709
Profile Visitors
1,534 (3 today)
Twitter
Latest tweets from @dj_link
It's just a question of choosing what best goes towards your interests 14mins 0secs ago
All together these are great times for gamers. Each one of the 3 new gen-consoles have different strong points and kind of unique too. 14mins 52secs ago
@fidgetwidget would be cook if everyone started using it 3hours 18mins ago
I hope the new Wolverine movie has a cool intro like the first one did. T.co 3hours 26mins ago
@fidgetwidget *cool :P 3hours 37mins ago
@fidgetwidget that would cook 3hours 37mins ago
@fidgetwidget I though they owned it since it was an exclusive on the 360, Because if not I want that on other consoles :) 3hours 39mins ago
@TheBrain17 bet they're doing a "pooch" exclusive DLC 3hours 46mins ago
@OddlerPro Good enough for me, I need to wash RE6 from my mind anyway 5hours 59mins ago
We got one for the PS4, so here's one for the Xbox One, the announcement Abridged version T.co 6hours 18mins ago