• FEATURED
• FEATURED
• FEATURED
• FEATURED
• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# pathfinding - making turning cost (A-star algorithm)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1suliman  Members

Posted 01 March 2014 - 10:02 AM

Hi

Im using an A-star algorithm for pathfinding for my units in my rts game. I didnt make the a-star code myself but rather a wrapper around some code i found. I want to add a cost for turning (changing direction between to nodes) so the pather will prefer to make straighter paths instead of zigzagging.

Example:
Units can move in 8 directions between the tiles. When moving NNW (direction 1/16th or 1.30 a'clock) it prefers to go up, up-right, up, up-right (turning 1/8th each tile) instead of going all up it needs, turn once, and then go the rest up-right.

Best way to solve this?

Thanks

Erik

### #2Álvaro  Members

Posted 01 March 2014 - 11:12 AM

The straight-forward way to do this is to consider a node in the graph to mean a position on the map together with a heading. A node is connected only to the nodes corresponding to turning left or right (same position on the map, change the heading) and moving forward (different position on the map, same heading). However, this means multiplying the number of nodes by 8...

### #3ferrous  Members

Posted 01 March 2014 - 02:14 PM

Alvaro has the right of it for true turning costs, though I think he may just want to prevent zigzagging, so you could alter the F cost to make it more expensive to move to a node if it involves moving in a different direction than the last move.  Maybe store the dx/dy for each move, then compare it to the new potential, and roll it into the F cost?

(Also, if you haven't done so already, you may be able to fix your problem by just making diagonal moves more expensive that straight moves, check this thread for more info)

Edited by ferrous, 01 March 2014 - 02:15 PM.

### #4suliman  Members

Posted 02 March 2014 - 02:04 AM

my diagonal cost is quareroot of 2 already. tried to put this to 1.5 to see what happens but it doesnt change the zigzagging. I dont need true turn-cost, just avoid the worst zigzagging.

Ill look into your suggestions! Any good examples of this would be most appreciated!

Thanks
Erik

### #5LorenzoGatti  Members

Posted 03 March 2014 - 06:24 AM

Alvaro has the right of it for true turning costs, though I think he may just want to prevent zigzagging, so you could alter the F cost to make it more expensive to move to a node if it involves moving in a different direction than the last move.  Maybe store the dx/dy for each move, then compare it to the new potential, and roll it into the F cost?

No, if nodes represent locations they "forget" the direction of the last move. The cost of leaving the node is the same for every path through it. Only nodes representing position and direction can distinguish turning from straight movement.

Example with no obstacles:

a..
41.
752
.63
..b


As the preferred path from a to b, you want 1-2-3 (diagonal, diagonal, straight, straight) or its symmetrical counterpart 4-7-6 (straight, straight, diagonal, diagonal) rather than 4-5-6 (straight, diagonal, straight, diagonal); the status of 1-5-6 (diagonal, straight, straight, diagonal)  and 4-5-3 (straight, diagonal, diagonal, straight)
is unclear. At node 5 you want to prefer 3 as next node if coming from 4, 6 if coming from 1, demonstrating the need to split location-only nodes into location + direction nodes.

Unfortunately the cost of all four paths is going to be the identical with any fudging of diagonal step cost away from the correct factor of sqrt(2), because all have two straight steps and two diagonal steps.

Fudging costs can only affect search order, which doesn't seem a very good idea if you like symmetry and consistent behaviour.

Omae Wa Mou Shindeiru

### #6suliman  Members

Posted 03 March 2014 - 01:35 PM

but cannot you use adjacent cost? I mean the cost to move from a node to all 8 around it?

I think my a-star works like that. (since it knows that diagonal is more expensive this is a relationship, no single node is "diagonal" in itself, its diagonal in relation to which node is investigating it)

### #7ferrous  Members

Posted 03 March 2014 - 02:03 PM

Alvaro has the right of it for true turning costs, though I think he may just want to prevent zigzagging, so you could alter the F cost to make it more expensive to move to a node if it involves moving in a different direction than the last move.  Maybe store the dx/dy for each move, then compare it to the new potential, and roll it into the F cost?

No, if nodes represent locations they "forget" the direction of the last move. The cost of leaving the node is the same for every path through it. Only nodes representing position and direction can distinguish turning from straight movement.

In my data representation, each potential move keeps a link to the previous move, so it's just a matter of looking at the previous move, look at it's dy/dx compared to the current tile, compare it to the new potential move's dy/dx from the current tile.  It would basically mean that the search would tend to favor tiles "in front" over tiles to the side.

You're right in that it's just altering search order though, so I'm not sure how helpful it would be to implement it that way.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.