Skip to content

Add weights to nodes to influence search paths #151

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

Open
wants to merge 5 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
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ Basic Usage
To build a grid-map of width 5 and height 3:

```javascript
var grid = new PF.Grid(5, 3);
var grid = new PF.Grid(5, 3);
```

By default, all the nodes in the grid will be able to be walked through.
To set whether a node at a given coordinate is walkable or not, use the `setWalkableAt` method.

For example, to set the node at (0, 1) to be un-walkable, where 0 is the x coordinate (from left to right), and
For example, to set the node at (0, 1) to be un-walkable, where 0 is the x coordinate (from left to right), and
1 is the y coordinate (from up to down):

```javascript
Expand Down Expand Up @@ -205,6 +205,19 @@ you may use `PF.Util.expandPath`.
var newPath = PF.Util.expandPath(path);
```

There is an optional weight that can be assigned to each node in the grid. This
functions as a multiplier to the next g value calculated for each neighbor. By
setting the weight for a given node higher than 1 you make it less likely that
the finder will examine that node. By setting the weight lower than 1 you make
it more likely the finder will examine that node. The default value is 1 which will not impact the next g calculation at all.

There are two calls, a getter and a setter to examine or set the weight for a given node.
These are accessed via the grid object.

```javascript
var w = grid.getWeightAt(x,y); // returns the current weight value for the node at x,y
grid.setWeightAt(x,y, weight); // sets the weight for the node at x,y
```

Development
------------
Expand All @@ -219,9 +232,9 @@ Layout:
|-- benchmark # benchmarks
`-- visual # visualization

Make sure you have `node.js` installed, then use `npm` to install the dependencies:
Make sure you have `node.js` installed, then use `npm` to install the dependencies:

npm install -d
npm install -d

The build system uses gulp, so make sure you have it installed:

Expand Down
1 change: 1 addition & 0 deletions docs/contributor-guide/authors.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This is the alphabetically sorted list of contributors to PathFinding.js:
Anders Riutta <https://github.com/ariutta><br>
Chris Khoo <https://github.com/chriskck><br>
Gerjo <https://github.com/Gerjo><br>
James Brown <https://github.com/jbrown123><br>
Juan Pablo Canepa <https://github.com/jpcanepa><br>
Mat Gadd <https://github.com/Drarok><br>
Murilo Pereira <https://github.com/mpereira><br>
Expand Down
12 changes: 6 additions & 6 deletions docs/user-guide/diagonal-movement.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var finder = new PF.AStarFinder({

See that the path is straight now:

![Screenshot](user-guide/images/DiagonalMovementDisabled.png)
![Screenshot](images/DiagonalMovementDisabled.png)

The `diagonalMovement` option can take any of the following values:

Expand All @@ -25,28 +25,28 @@ To understand them consider the following four simple maps labelled A, B, C and
D. A has no obstacles for diagonal movement from green to orange cell, B and C
have one obstacle and D has two obstacles.

![Screenshot](user-guide/images/DiagonalMaps.png)
![Screenshot](images/DiagonalMaps.png)

## Always
With this option PathFinding.js will always find a diagonal path, irrespective
of the obstacles when moving diagonally.

![Screenshot](user-guide/images/AllMapsWithAPath.png)
![Screenshot](images/AllMapsWithAPath.png)

## Never
With this option PathFinding.js will only find straight paths and will never
find any diagonal paths.

![Screenshot](user-guide/images/AllMapsWithStraightPaths.png)
![Screenshot](images/AllMapsWithStraightPaths.png)

## IfAtMostOneObstacle
With this option PathFinding.js will find diagonal paths only if there is at
most one obstacle for the diagonal path.

![Screenshot](user-guide/images/DiagonalPathsForAtMostOneObstacle.png)
![Screenshot](images/DiagonalPathsForAtMostOneObstacle.png)

## OnlyWhenNoObstacles
With this option PathFinding.js will find diagonal paths only if there are no
obstacles for the diagonal path.

![Screenshot](user-guide/images/DiagonalPathsForOnlyWhenNoObstacles.png)
![Screenshot](images/DiagonalPathsForOnlyWhenNoObstacles.png)
4 changes: 2 additions & 2 deletions docs/user-guide/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var grid = new PF.Grid(5, 7);
```
This will create a grid which is walkable all over.

![Screenshot](user-guide/images/5x7EmptyGrid.png)
![Screenshot](images/5x7EmptyGrid.png)

In this grid, the green cell at the top left is [0, 0] and the orange cell at
the bottom right is [4, 6].
Expand All @@ -49,4 +49,4 @@ This will return the following path:

Which when plotted on the grid looks like:

![Screenshot](user-guide/images/5x7GridWithPath.png)
![Screenshot](images/5x7GridWithPath.png)
4 changes: 2 additions & 2 deletions docs/user-guide/obstacles.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ grid.setWalkableAt(2, 1, false);

After setting the obstacles the grid should look like this.

![Screenshot](user-guide/images/5x7GridWithObstacles.png)
![Screenshot](images/5x7GridWithObstacles.png)

Let us find a path now.

Expand All @@ -37,7 +37,7 @@ var path = finder.findPath(0, 0, 4, 6, grid);

PathFinding.js will find the following path:

![Screenshot](user-guide/images/5x7GridWithObstaclesAndPath.png)
![Screenshot](images/5x7GridWithObstaclesAndPath.png)

Notice how the path moves diagonally where it can, thus making it shorter. This
may not be always desirable and you may want to create a path without any
Expand Down
Loading