-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathastar.js.html
More file actions
257 lines (221 loc) · 9.01 KB
/
astar.js.html
File metadata and controls
257 lines (221 loc) · 9.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: astar.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: astar.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>"use strict";
var BinaryHeap = require("./binary_heap");
/**
* Implements the [A* pathfinding algorithm]{@link http://en.wikipedia.org/wiki/A*_search_algorithm} on a 2-dimensional grid. You can use this to find a path between a source and destination coordinate while avoiding obstacles.
* @constructor
* @alias Splat.AStar
* @param {isWalkable} isWalkable A function to test if a coordinate is walkable by the entity you're performing the pathfinding for.
*/
function AStar(isWalkable) {
this.destX = 0;
this.destY = 0;
this.scaleX = 1;
this.scaleY = 1;
this.openNodes = {};
this.closedNodes = {};
this.openHeap = new BinaryHeap(function(a, b) {
return a.f - b.f;
});
this.isWalkable = isWalkable;
}
/**
* The [A* heuristic]{@link http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html}, commonly referred to as h(x), that estimates how far a location is from the destination. This implementation is the [Manhattan method]{@link http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html#manhattan-distance}, which is good for situations when the entity can travel in four directions. Feel free to replace this with a different heuristic implementation.
* @param {number} x The x coordinate to estimate the distance to the destination.
* @param {number} y The y coordinate to estimate the distance to the destination.
*/
AStar.prototype.heuristic = function(x, y) {
// manhattan method
var dx = Math.abs(x - this.destX) / this.scaleX;
var dy = Math.abs(y - this.destY) / this.scaleY;
return dx + dy;
};
/**
* Make a node to track a given coordinate
* @param {number} x The x coordinate of the node
* @param {number} y The y coordinate of the node
* @param {object} parent The parent node for the current node. This chain of parents eventually points back at the starting node.
* @param {number} g The g(x) travel cost from the parent node to this node.
* @private
*/
AStar.prototype.makeNode = function(x, y, parent, g) {
g += parent.g;
var h = this.heuristic(x, y);
return {
x: x,
y: y,
parent: parent,
f: g + h,
g: parent.g + g,
h: h
};
};
/**
* Update the g(x) travel cost to a node if a new lower-cost path is found.
* @param {string} key The key of the node on the open list.
* @param {object} parent A parent node that may have a shorter path for the node specified in key.
* @param {number} g The g(x) travel cost from parent to the node specified in key.
* @private
*/
AStar.prototype.updateOpenNode = function(key, parent, g) {
var node = this.openNodes[key];
if (!node) {
return false;
}
var newG = parent.g + g;
if (newG >= node.g) {
return true;
}
node.parent = parent;
node.g = newG;
node.f = node.g + node.h;
var pos = this.openHeap.indexOf(node);
this.openHeap.bubbleUp(pos);
return true;
};
/**
* Create a neighbor node to a parent node, and add it to the open list for consideration.
* @param {string} key The key of the new neighbor node.
* @param {number} x The x coordinate of the new neighbor node.
* @param {number} y The y coordinate of the new neighbor node.
* @param {object} parent The parent node of the new neighbor node.
* @param {number} g The travel cost from the parent to the new parent node.
* @private
*/
AStar.prototype.insertNeighbor = function(key, x, y, parent, g) {
var node = this.makeNode(x, y, parent, g);
this.openNodes[key] = node;
this.openHeap.insert(node);
};
AStar.prototype.tryNeighbor = function(x, y, parent, g) {
var key = makeKey(x, y);
if (this.closedNodes[key]) {
return;
}
if (!this.isWalkable(x, y)) {
return;
}
if (!this.updateOpenNode(key, parent, g)) {
this.insertNeighbor(key, x, y, parent, g);
}
};
AStar.prototype.getNeighbors = function getNeighbors(parent) {
var diagonalCost = 1.4;
var straightCost = 1;
this.tryNeighbor(parent.x - this.scaleX, parent.y - this.scaleY, parent, diagonalCost);
this.tryNeighbor(parent.x, parent.y - this.scaleY, parent, straightCost);
this.tryNeighbor(parent.x + this.scaleX, parent.y - this.scaleY, parent, diagonalCost);
this.tryNeighbor(parent.x - this.scaleX, parent.y, parent, straightCost);
this.tryNeighbor(parent.x + this.scaleX, parent.y, parent, straightCost);
this.tryNeighbor(parent.x - this.scaleX, parent.y + this.scaleY, parent, diagonalCost);
this.tryNeighbor(parent.x, parent.y + this.scaleY, parent, straightCost);
this.tryNeighbor(parent.x + this.scaleX, parent.y + this.scaleY, parent, diagonalCost);
};
function generatePath(node) {
var path = [];
while (node.parent) {
var ix = node.x;
var iy = node.y;
while (ix !== node.parent.x || iy !== node.parent.y) {
path.unshift({x: ix, y: iy});
var dx = node.parent.x - ix;
if (dx > 0) {
ix++;
} else if (dx < 0) {
ix--;
}
var dy = node.parent.y - iy;
if (dy > 0) {
iy++;
} else if (dy < 0) {
iy--;
}
}
node = node.parent;
}
return path;
}
function makeKey(x, y) {
return x + "," + y;
}
/**
* Search for an optimal path between srcX, srcY and destX, destY, while avoiding obstacles.
* @param {number} srcX The starting x coordinate
* @param {number} srcY The starting y coordinate
* @param {number} destX The destination x coordinate
* @param {number} destY The destination y coordinate
* @returns {Array} The optimal path, in the form of an array of objects that each have an x and y property.
*/
AStar.prototype.search = function aStar(srcX, srcY, destX, destY) {
function scale(c, s) {
var downscaled = (c / s) |0;
return downscaled * s;
}
srcX = scale(srcX, this.scaleX);
srcY = scale(srcY, this.scaleY);
this.destX = scale(destX, this.scaleX);
this.destY = scale(destY, this.scaleY);
if (!this.isWalkable(this.destX, this.destY)) {
return [];
}
var srcKey = makeKey(srcX, srcY);
var srcNode = {
x: srcX,
y: srcY,
g: 0,
h: this.heuristic(srcX, srcY)
};
srcNode.f = srcNode.h;
this.openNodes = {};
this.openNodes[srcKey] = srcNode;
this.openHeap = new BinaryHeap(function(a, b) {
return a.f - b.f;
});
this.openHeap.insert(srcNode);
this.closedNodes = {};
var node = this.openHeap.deleteRoot();
while (node) {
var key = makeKey(node.x, node.y);
delete this.openNodes[key];
this.closedNodes[key] = node;
if (node.x === this.destX && node.y === this.destY) {
return generatePath(node);
}
this.getNeighbors(node);
node = this.openHeap.deleteRoot();
}
return [];
};
module.exports = AStar;
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-buffer.html">buffer</a></li><li><a href="module-KeyMap.html">KeyMap</a></li></ul><h3>Externals</h3><ul><li><a href="external-AudioContext.html">AudioContext</a></li><li><a href="external-canvas.html">canvas</a></li><li><a href="external-CanvasRenderingContext2D.html">CanvasRenderingContext2D</a></li><li><a href="external-image.html">image</a></li></ul><h3>Classes</h3><ul><li><a href="Accelerometer.html">Accelerometer</a></li><li><a href="Animation.html">Animation</a></li><li><a href="AnimationLoader.html">AnimationLoader</a></li><li><a href="FontLoader.html">FontLoader</a></li><li><a href="ImageLoader.html">ImageLoader</a></li><li><a href="Keyboard.html">Keyboard</a></li><li><a href="Mouse.html">Mouse</a></li><li><a href="SceneManager.html">SceneManager</a></li><li><a href="SoundLoader.html">SoundLoader</a></li><li><a href="Splat.AnimatedEntity.html">AnimatedEntity</a></li><li><a href="Splat.AStar.html">AStar</a></li><li><a href="Splat.BinaryHeap.html">BinaryHeap</a></li><li><a href="Splat.Camera.html">Camera</a></li><li><a href="Splat.Entity.html">Entity</a></li><li><a href="Splat.EntityBoxCamera.html">EntityBoxCamera</a></li><li><a href="Splat.Game.html">Game</a></li><li><a href="Splat.math.Random.html">Random</a></li><li><a href="Splat.NinePatch.html">NinePatch</a></li><li><a href="Splat.Scene.html">Scene</a></li><li><a href="Splat.Timer.html">Timer</a></li></ul><h3>Namespaces</h3><ul><li><a href="Splat.html">Splat</a></li><li><a href="Splat.ads.html">ads</a></li><li><a href="Splat.leaderboards.html">leaderboards</a></li><li><a href="Splat.math.html">math</a></li><li><a href="Splat.saveData.html">saveData</a></li></ul><h3><a href="global.html">Global</a></h3>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.2</a> on Thu Sep 17 2015 23:22:42 GMT-0400 (EDT)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>