Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A* heuristic to create Bresenham lines

From what I understand about A* heuristics and how the Bresenham algorithm works, this may not be be possible since only the current state and goal state are passed to the heuristic function. But maybe someone has a clever solution to this problem.

I am using A* to plan a path on a grid, and I would like a heuristic that would cause the best path to follow a Bresenham's line when there are free spaces between the current state and the goal or the next turn around an obstacle.

Here are some images to illustrate the problem.

Manhattan distance:

If movements in the world acted like a checkers on a grid, this would be perfectly fine, but I'm eventually going to convert the A* path to motions on a continuously plane, so this does work very well.

The shortest path from red to green using the Manhattan distance heuristic

Euclidean distance:

Better, but still not perfect. Notice the straight line at the end. The diagonal could have just as easily remained a diagonal, which is what I want.

The shortest path from red to green using the Euclidian distance heuristic

What I Want:

Bresenham lines are draw to the next turn or goal.

The best path I actually want, which uses Bresenham lines to get to the goal

I found a good resource here, http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html that touches on what I am looking for, but only seems to work for drawing Bresenham lines from the start to the goal. What I want are Bresenham lines being draw to the next turn around an obstacle too.

Any ideas for a good approach to this problem?

like image 690
peskal Avatar asked Apr 23 '11 05:04

peskal


People also ask

What is Bresenham line generation algorithm?

Bresenham's line algorithm is a line drawing algorithm that determines the points of an n-dimensional raster that should be selected in order to form a close approximation to a straight line between two points.

What is decision parameter in Bresenham's line drawing algorithm?

Once we choose a pixel, we have two possible pixels to select as the next pixel. To select the next pixel, we are using a decision parameter based on the slope error. If the slope error is greater than 0.5, the actual line is near the NE. The line is closer to E when the slope error is less than 0.5.

What is error term in Bresenham's line algorithm?

The error term is initially set as e = 2 Δy - Δx where Δy = y2 - y1, and Δx = x2 - x1. Then according to value of e following actions are taken, while ( e ≥ 0) { y = y + 1 e = e - 2 * Δx } x = x + 1 e = e + 2 * Δy.


2 Answers

Can you modify the cost function on the fly so that the cost of going forward increases with the accumulated error?

The idea is, at the start of the algorithm, calculate DX and DY as in standard Bresenham. (Assume for the rest of the example that DX > DY > 0. Modify accordingly for other directions.)

Then for every visited neighbor node, track the Bresnaham error:

if (neighbor.X > this.X) 
   neighbor.err=this.err+DY 
else if (neighbor.Y > this.Y) 
   neighbor.err=this.err-DX

Then modify your cost function to favor increasing X, but add if (err >= DX/2) then cost=cost+FACTOR. In a map where all other costs are equal, this should trace the right line.

The other thing you might need is special handling when the path steps around an obstacle, otherwise you could get strange wall-following paths similar to the "cross-product with obatacles" example in your linked article. You could possibly handle that situation by recalculating DX and DY whenever the neighbor node is not in the +X or +Y direction. (Unfortunately, this likely requires tracking a separate DX, DY, and error for each node, which may be too much overhead)

Disclaimer, I haven't implemented an A* or Bresneham algorithm in years. This whole idea may be unworkable

like image 195
AShelly Avatar answered Sep 25 '22 23:09

AShelly


Have all your move alternatives be the corners visible from your current position (or the goal if it is visible), and once you find the shortest path, draw Bresenham lines between all your stops.

like image 40
Null Set Avatar answered Sep 23 '22 23:09

Null Set