• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
GuiTeK

A* Bad heuristic (zigzag)

12 posts in this topic

Hello,

 

I'm new to AI programming and I need a pathfinding algorithm for my game which uses isometric tile maps (see below the screenshot in attachment). Player can move in the 8 cardinal directions (North, North East, East, etc.).

 

I successfully implemented the A* algorithm, however I think my heuristic is bad:

[attachment=19969:heuristic_bad.PNG]

As you can see, the algorithm doesn't keep a straight diagonal to the goal. It's not natural at all, I can't let the player move this way.

 

I used this heuristic which is described as the best algorithm for grids that allow 8 directions of movement.

Here is how I implemented it in Java:

public final int ORTHOGONAL_COST = 14;
public final int DIAGONAL_COST = 10;

public float getHeuristicCost(final Point2D.Int source, final Point2D.Int target)
{
	int dx = Math.abs(source.x - target.x);
	int dy = Math.abs(source.y - target.y) / 2;
	
	return ORTHOGONAL_COST * (dx + dy) + (DIAGONAL_COST - 2 * ORTHOGONAL_COST) * Math.min(dx, dy);
}

I divided dy by 2 because of the map's style, it gives a path even weirder if I don't divide by 2.

 

I can understand the path isn't straight because this heuristic isn't fit for my map style, but which one should I use then?

 

Thank you.

0

Share this post


Link to post
Share on other sites

Are you sure the heuristic is what's wrong and not your overall traversal cost computation?

Hmm... what do you mean? I think I didn't implement such a thing blink.png ...

Sorry but I'm new to AI world...

0

Share this post


Link to post
Share on other sites

I'd first try with the simplest implementation of your cost function and that is simple geometric distance between the two points. That is Math.sqrt(dx * dx + dy * dy). Get rid of that /2 in dy, just as the others said. If this doesn't work, then your problem is elsewhere :) Just then, when it works, you might go on with optimising the function to use integers only (for performance).

0

Share this post


Link to post
Share on other sites

The diagonal cost is supposed to be greater than the orthogonal cost. Of course your pathfinder will choose to use all diagonals if those are cheaper. They cover more ground.

Also, if you have to throw in a divide by 2 for no apparent reason other than style, you have other problems too. Your pathfinding shouldn't have to be any different than if you draw the map with no skew.

I've taken into account your tips. Here is the code and the results:

public final int ORTHOGONAL_COST = 10;
public final int DIAGONAL_COST = 14;

public float getHeuristicCost(final Point2D.Int source, final Point2D.Int target)
{
	int dx = Math.abs(source.x - target.x);
	int dy = Math.abs(source.y - target.y);
	
	return ORTHOGONAL_COST * (dx + dy) + (DIAGONAL_COST - 2 * ORTHOGONAL_COST) * Math.min(dx, dy);
}

[attachment=20008:diagonal_distance.PNG]

 

Maybe I don't get the expected result (straight diagonal line) because I don't handle the coordinates the right way? Below you can see how I do it:

[attachment=20009:grid_blank.PNG]

Is it right?

 

 

 

I'd first try with the simplest implementation of your cost function and that is simple geometric distance between the two points. That is Math.sqrt(dx * dx + dy * dy). Get rid of that /2 in dy, just as the others said. If this doesn't work, then your problem is elsewhere smile.png Just then, when it works, you might go on with optimising the function to use integers only (for performance).

Tried that too. Code & results:

public float getHeuristicCost(final Point2D.Int source, final Point2D.Int target)
{
	int dx = Math.abs(source.x - target.x);
	int dy = Math.abs(source.y - target.y);
	
	return (float)Math.sqrt(dx * dx + dy * dy);
}

[attachment=20007:euclidean_distance.PNG]

 

 

As a very general rule, the heuristic is not to blame for bad quality of the results (as long as it's admissible). If your algorithm is picking a result you don't like among many possible results with the same cost, it means your cost function doesn't accurately reflect your preferences.

How can I adjust my cost function? I just try again and again until I find something that matches with what I want?

0

Share this post


Link to post
Share on other sites

How can I adjust my cost function? I just try again and again until I find something that matches with what I want?


Why don't you share with the forum the actual cost function that you use when you're building the path? The heuristic and the real cost have to be fixed together.

Also, because you're having trouble with an 8-way system, I think you should try to get 4 directions working correctly first. I'd like to know how that works out.

Another interesting experiment would be using a heuristic that always returned 0. You would have a slower path-finding function but it should work and rule out any heuristic issues.
0

Share this post


Link to post
Share on other sites

Yeah, I'm with ApochPiQ, I suspect the problem is that there are two functions to determine the next move, the cost to move to that new tile, and the heuristic to determine how close that move will get you to the goal.

 

For your case, you want to take the path that costs the least to move to (diagonals cost more) and yet yields the best H value, in your case the least distance to the goal.

Edited by ferrous
1

Share this post


Link to post
Share on other sites

Well, since my last post there have been many changes. Here's the long story.

 

First of all, the screenshots I showed you didn't come from a program I coded. Actually, the program comes from here.

My pathfinding algorithm wasn't working as expected so I decided to search for one that would work as I wanted. Unfortunately, I didn't find anything for isometric tile maps with 8 directions possible. I then decided to modify the program I linked above to make it work with isometric tiles and with 8 directions. It was actually a bad idea because it took me time to understand how the pathfinding was implemented and the modifications I made were... quite messy (on top of that I'm not a Java programmer and it's written in Java sleep.png ).

 

Ok, so I decided to take back MY algorithm (implemented in C++) and try (one more time) to make it work.

 

[...] Also, it sounds like you're not actually implementing A* correctly, or don't fully understand the algorithm. There are two costs: the heuristic (which as Alvaro said is generally not the problem provided it is admissible) and the actual traversal cost. Chances are your problem is in your A* implementation in how you compute the actual traversal cost versus the values provided by your heuristic.

Yes there are 2 costs, thanks for reminding me! All this time, I don't know why but I never considered the "G" (known) cost. I focused on the "H" (heuristic) cost. Your answer, ferrous, helped me as well:

 

Yeah, I'm with ApochPiQ, I suspect the problem is that there are two functions to determine the next move, the cost to move to that new tile, and the heuristic to determine how close that move will get you to the goal.

 

For your case, you want to take the path that costs the least to move to (diagonals cost more) and yet yields the best H value, in your case the least distance to the goal.

After reading your post, I modified my heuristic function to a simple one (diffX + diffY):

int Pathfinder::calculateHeuristicCost(PathNode *node)
{
    Point cellPt = this->getCoordsFromCellId(node->CellId);
    Point goalPt = this->getCoordsFromCellId(m_goalCellId);

    int dx = std::abs(cellPt.X - goalPt.X);
    int dy = std::abs(cellPt.Y - goalPt.Y);

    return dx + dy;
}

and I worked on the function which calculates the G cost. I decided to penalize orthogonal moves by adding an extra cost. But then the question is: what cost should I add to orthogonal moves?

  • If the cost it too high, the algorithm will always prefer diagonal moves and it will result in longer paths sometimes.
  • If the cost is too low, the algorithm will always prefer orthogonal moves (as shown in the screenshots above) and it will result in longer paths as well.

After a few tries, I ended up with the values +10 for diagonal moves and +14 for orthogonal moves (which are common values for pathfinding, don't really know why, I read some things about distances and Pythagorean theorem).

const int ORTHOGONAL_COST = 14;
const int DIAGONAL_COST = 10;

int Pathfinder::calculateMoveCost(PathNode *parent, PathNode *node)
{
    const CellData &parentCellData = m_mapGrid.getCellDataFromId(parent->CellId);
    const CellData &nodeCellData = m_mapGrid.getCellDataFromId(node->CellId);

    if (this->getDiagonalAlignment(parentCellData, nodeCellData) != Direction::None)
        return parent->G + DIAGONAL_COST;
    else
        return parent->G + ORTHOGONAL_COST;

    return 0;
}

Result: http://www.youtube.com/watch?v=G8HXmoVp2tA (I don't know why there is a black line in the middle, it doesn't matter).

As you can see, it works pretty well.

 

What do you think? Good/bad method?

2

Share this post


Link to post
Share on other sites

Good work!

 

The reason you wound up with 10 and 14 is because of Pythagoras, as you noted: if a straight line costs 10 units, then the triangle formed by taking two straight moves also offers you a third move, the diagonal. By the Pythagorean theorem, the diagonal cost = sqrt(a*a + b*b) = sqrt(10*10 + 10*10) = sqrt(200) ~= 14.142.

2

Share this post


Link to post
Share on other sites

Good work!

 

The reason you wound up with 10 and 14 is because of Pythagoras, as you noted: if a straight line costs 10 units, then the triangle formed by taking two straight moves also offers you a third move, the diagonal. By the Pythagorean theorem, the diagonal cost = sqrt(a*a + b*b) = sqrt(10*10 + 10*10) = sqrt(200) ~= 14.142.

Thank you for this explanation and for all your answers.

Thanks to everybody who posted in this thread too.

 

I can move on to the next part of my game now smile.png .

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0