Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add extra cost for turns in A* to avoid staircase #32

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/pathfinding-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,7 @@ function AStarFinder(opt) {
this.dontCrossCorners = opt.dontCrossCorners;
this.heuristic = opt.heuristic || Heuristic.manhattan;
this.weight = opt.weight || 1;
this.avoidStaircase = opt.avoidStaircase;
}

/**
Expand All @@ -1224,8 +1225,10 @@ AStarFinder.prototype.findPath = function(startX, startY, endX, endY, grid) {
heuristic = this.heuristic,
allowDiagonal = this.allowDiagonal,
dontCrossCorners = this.dontCrossCorners,
avoidStaircase = this.avoidStaircase,
weight = this.weight,
abs = Math.abs, SQRT2 = Math.SQRT2,
turnPenalty = 1, lastDirection = undefined,
node, neighbors, neighbor, i, l, x, y, ng;

// set the `g` and `f` value of the start node to be 0
Expand Down Expand Up @@ -1263,6 +1266,14 @@ AStarFinder.prototype.findPath = function(startX, startY, endX, endY, grid) {
// and calculate the next g score
ng = node.g + ((x - node.x === 0 || y - node.y === 0) ? 1 : SQRT2);


if (avoidStaircase) {
// add additional cost if neighbor creates a turn
lastDirection = node.parent == undefined? undefined : { x : node.x - node.parent.x, y : node.y - node.parent.y };
var turned = lastDirection == undefined? 0 : lastDirection.x != x - node.x || lastDirection.y != y - node.y;
ng += turnPenalty * turned;
}

// check if the neighbor has not been inspected yet, or
// can be reached with smaller cost from the current node
if (!neighbor.opened || ng < neighbor.g) {
Expand Down Expand Up @@ -2006,4 +2017,4 @@ require.define("/PathFinding.js", function (require, module, exports, __dirname,

});
require("/PathFinding.js");
return require("/PathFinding");})();
return require("/PathFinding");})();
2 changes: 2 additions & 0 deletions visual/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ <h3>Options</h3>
<label class="option_label">Bi-directional</label> <br>
<input type="checkbox" class="dont_cross_corners">
<label class="option_label">Don't Cross Corners</label> <br>
<input type="checkbox" class="avoid_staircase">
<label class="option_label">Avoid staircase</label> <br>
<input class="spinner" name="astar_weight" value="1">
<label class="option_label">Weight</label> <br>
</div>
Expand Down
5 changes: 4 additions & 1 deletion visual/js/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ var Panel = {
'.bi-directional:checked').val() !=='undefined';
dontCrossCorners = typeof $('#astar_section ' +
'.dont_cross_corners:checked').val() !=='undefined';
avoidStaircase = typeof $('#astar_section ' +
'.avoid_staircase:checked').val() !=='undefined';

/* parseInt returns NaN (which is falsy) if the string can't be parsed */
weight = parseInt($('#astar_section .spinner').val()) || 1;
Expand All @@ -59,7 +61,8 @@ var Panel = {
allowDiagonal: allowDiagonal,
dontCrossCorners: dontCrossCorners,
heuristic: PF.Heuristic[heuristic],
weight: weight
weight: weight,
avoidStaircase: avoidStaircase
});
}
break;
Expand Down