Skip to content

Commit 0e73400

Browse files
committed
finished navigation mesh generation (needs a little work)
1 parent 51a6004 commit 0e73400

36 files changed

+1975
-563
lines changed

README.md

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ This library is meant to provide pathfinding algorithms which are easily to use
88

99
This library is created in typescript and provides types.
1010

11-
There is a [demo project](https://casper64.github.io/pathfinding-js) ([repository](https://github.com/Casper64/Casper64.github.io/blob/master/pathfinding-js/src/index.ts)) to show how you could implement the library in a sort of visualisation.
11+
I made a visualisation with this library for square grids [here](https://casper64.github.io/pathfinding-js) ([code](https://github.com/Casper64/Casper64.github.io/blob/master/pathfinding-js/src/index.ts)).
12+
And another visualisation how the algorithms generate a navigation mesh (non square 2d plane) based on the shapes you place in the 2d grid [here](https://casper64.github.io/pathfinding-js/navmesh) ([code](https://github.com/Casper64/Casper64.github.io/blob/master/pathfinding-js/navmesh/src/index.ts)) (working on implementing this in 3D!).
1213

13-
Note: you can only use this library for 2d grids!
14+
Note: This library is still in early stages of development so function and class names could change in the future!
1415

1516
Installation
1617
------------
@@ -38,8 +39,8 @@ var grid = new pf.Grid(10, 8);
3839
</script></script>
3940
```
4041

41-
Basic usage
42-
-----------
42+
Basic usage 2d square grids
43+
---------------------------
4344
Create a grid of width 10 and height 8:
4445
```javascript
4546
let grid = new pf.Grid(10, 8);
@@ -70,8 +71,8 @@ For the `grid` defined previously `result.path` will be:
7071
{x: 3, y: 6}, {x: 4, y: 6}, {x: 5, y: 6}, {x: 6, y: 6}, {x: 7, y: 6}, {x: 8, y: 6}, {x: 9, y: 6} ]
7172
```
7273

73-
Advanced usage
74-
--------------
74+
Advanced usage 2d square grids
75+
------------------------------
7576
Options can be passed in the constructor of the finder:
7677
```javascript
7778
let finder = new pf.AStar({
@@ -82,3 +83,33 @@ let finder = new pf.AStar({
8283
});
8384
```
8485
In this example the A* algorithm searches from the start and from the end (bidirectional search), is allowed to go diagonal and smoothens the path and also uses the Eucledian heuristic function.
86+
87+
Usage for non-square 2d grids
88+
-----------------------------
89+
Define the outlay of your map:
90+
```javascript
91+
let mapVertices = [new pf.Point(0, 0), new pf.Point(100, 0), new pf.Point(100, 1000), new pf.Point(0, 100)];
92+
let outlay = new pf.Polygon(mapVertices);
93+
```
94+
Next instantiate some object that should be in the map. They could be any polygon, but the algorithm Bowyer-Watson algorithm used in this library works best for convex polygons. Meaning that the shapes don't have large indents and they cannot overlap. For example:
95+
```javascript
96+
let object1Vertices = [new pf.Point(10, 10), new pf.Point(30, 5), new pf.Point(20, 20)];
97+
let object1 = new pf.Polygon(object1Vertices);
98+
let object2Vertices = [new pf.Point(40, 20), new pf.Point(70, 20), new pf.Point(70, 50), new pf.Point(40, 50)];
99+
let object2 = new pf.Polygon(object2Vertices);
100+
let object3Vertices = [new pf.Point(10, 90), new pf.Point(30, 60), new pf.Point(60, 60), new pf.Point(70, 80), new pf.Point(30, 95)];
101+
let object3 = new pf.Polygon(object3Vertices);
102+
let object4Vertices = [new pf.Point(85, 40), new pf.Point(95, 40), new pf.Point(95, 70), new pf.Point(85, 70)];
103+
let object4 = new pf.Polygon(object4Vertices);
104+
105+
let grid = new pf.MeshGrid([object1, object2, object3, object4], outlay.vertices);
106+
grid.generateMap();
107+
```
108+
The generation of the map takes a bit of time (around 500ms on a normal computer with 40 objects with 4 vertices), depending on how many objects you put in the grid. But when the map is generated the very fast A* algorithm saves you time and finds the path faster than with 2d square grids!. You have to use the A* algorithm in the following way:
109+
```javascript
110+
let finder = new pf.Astar(); // no options needed
111+
let result = finder.findPathMesh(new pf.Point(2, 4), new pf.Point(95, 95), grid);
112+
// result.path is the same path of points as with 'Astar.findPath'
113+
```
114+
The path given by 'Astar.findPathMesh' is not perfect, but close to perfect. In this case speed outweighs precision.
115+
Funny: The algorithms works better if you put more object into the map!

0 commit comments

Comments
 (0)