-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAstar.cpp
446 lines (360 loc) · 16.2 KB
/
Astar.cpp
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <utility>
using namespace std;
sf::Vector2u WINDOW_SIZE{800,480};
/* Background
~~~~~~~~~~
The A* path finding algorithm is a widely used and powerful shortest path
finding node traversal algorithm. A heuristic is used to bias the algorithm
towards success. This code is probably more interesting than the video. :-/*/
struct AStar {
public:
AStar(sf::RenderWindow* window) : window{window} {
this->shape.setSize(sf::Vector2f{(float)this->nNodeSize - this->nNodeBorder,(float)this->nNodeSize - this->nNodeBorder});
// Create a 2D array of nodes - this is for convenience of rendering and construction
// and is not required for the algorithm to work - the nodes could be placed anywhere
// in any space, in multiple dimensions...
nodes = new sNode[nMapWidth * nMapHeight];
for (auto x = 0; x < nMapWidth; x++) {
for (auto y = 0; y < nMapHeight; y++) {
nodes[y * nMapWidth + x].x = x; // ...because we give each node its own coordinates
nodes[y * nMapWidth + x].y = y;
nodes[y * nMapWidth + x].bObstacle = false;
nodes[y * nMapWidth + x].parent = nullptr;
nodes[y * nMapWidth + x].bVisited = false;
}
}
// Create connections - in this case nodes are on a regular grid
for (auto x = 0; x < nMapWidth; x++) {
for (auto y = 0; y < nMapHeight; y++) {
if (y > 0)
nodes[y * nMapWidth + x].vecNeighbors.push_back(&nodes[(y - 1) * nMapWidth + (x + 0)]);
if (y < nMapHeight - 1)
nodes[y * nMapWidth + x].vecNeighbors.push_back(&nodes[(y + 1) * nMapWidth + (x + 0)]);
if (x > 0)
nodes[y * nMapWidth + x].vecNeighbors.push_back(&nodes[(y + 0) * nMapWidth + (x - 1)]);
if (x < nMapWidth - 1)
nodes[y * nMapWidth + x].vecNeighbors.push_back(&nodes[(y + 0) * nMapWidth + (x + 1)]);
// if diagnals are included
if (b8Connection) {
// We can also connect diagonally
if (y > 0 && x > 0)
nodes[y * nMapWidth + x].vecNeighbors.push_back(&nodes[(y - 1) * nMapWidth + (x - 1)]);
if (y < nMapHeight - 1 && x>0)
nodes[y * nMapWidth + x].vecNeighbors.push_back(&nodes[(y + 1) * nMapWidth + (x - 1)]);
if (y > 0 && x < nMapWidth - 1)
nodes[y * nMapWidth + x].vecNeighbors.push_back(&nodes[(y - 1) * nMapWidth + (x + 1)]);
if (y < nMapHeight - 1 && x < nMapWidth - 1)
nodes[y * nMapWidth + x].vecNeighbors.push_back(&nodes[(y + 1) * nMapWidth + (x + 1)]);
}
}
}
// Manually position the start and end markers so they are not nullptr
nodeStart = &nodes[(nMapHeight / 2) * nMapWidth + 1];
nodeEnd = &nodes[(nMapHeight / 2) * nMapWidth + nMapWidth - 2];
this->font.loadFromFile("assets/fonts/sansation.ttf");
this->text.setFont(this->font);
this->text.setCharacterSize(12);
}
void toggleDiagnols() {
for (auto x = 0; x < nMapWidth; x++) {
for (auto y = 0; y < nMapHeight; y++) {
nodes[y * nMapWidth + x].x = x; // ...because we give each node its own coordinates
nodes[y * nMapWidth + x].y = y;
nodes[y * nMapWidth + x].bObstacle = nodes[y * nMapWidth + x].bObstacle;
nodes[y * nMapWidth + x].parent = nullptr;
nodes[y * nMapWidth + x].bVisited = false;
nodes[y * nMapWidth + x].vecNeighbors.clear();
}
}
// Create connections - in this case nodes are on a regular grid
for (auto x = 0; x < nMapWidth; x++) {
for (auto y = 0; y < nMapHeight; y++) {
if (y > 0)
nodes[y * nMapWidth + x].vecNeighbors.push_back(&nodes[(y - 1) * nMapWidth + (x + 0)]);
if (y < nMapHeight - 1)
nodes[y * nMapWidth + x].vecNeighbors.push_back(&nodes[(y + 1) * nMapWidth + (x + 0)]);
if (x > 0)
nodes[y * nMapWidth + x].vecNeighbors.push_back(&nodes[(y + 0) * nMapWidth + (x - 1)]);
if (x < nMapWidth - 1)
nodes[y * nMapWidth + x].vecNeighbors.push_back(&nodes[(y + 0) * nMapWidth + (x + 1)]);
// if diagnals are included
if (b8Connection) {
// We can also connect diagonally
if (y > 0 && x > 0)
nodes[y * nMapWidth + x].vecNeighbors.push_back(&nodes[(y - 1) * nMapWidth + (x - 1)]);
if (y < nMapHeight - 1 && x>0)
nodes[y * nMapWidth + x].vecNeighbors.push_back(&nodes[(y + 1) * nMapWidth + (x - 1)]);
if (y > 0 && x < nMapWidth - 1)
nodes[y * nMapWidth + x].vecNeighbors.push_back(&nodes[(y - 1) * nMapWidth + (x + 1)]);
if (y < nMapHeight - 1 && x < nMapWidth - 1)
nodes[y * nMapWidth + x].vecNeighbors.push_back(&nodes[(y + 1) * nMapWidth + (x + 1)]);
}
}
}
SolveAStar();
}
bool SolveAStar() {
// reset navigation graph - default all node states
for (int x = 0; x < nMapWidth; x++) {
for (int y = 0; y < nMapHeight; y++) {
nodes[y * nMapWidth + x].bVisited = false;
nodes[y * nMapWidth + x].fGlobalGoal = INFINITY;
nodes[y * nMapWidth + x].fLocalGoal = INFINITY;
nodes[y * nMapWidth + x].parent = nullptr; // No Parents
if (&nodes[y * nMapWidth + x] == nodeStart) {
shape.setPosition(x * this->nNodeSize + (float)this->nNodeBorder, y * this->nNodeSize + (float)this->nNodeBorder);
shape.setFillColor(sf::Color{127,255,0,255});
}
if (&nodes[y * nMapWidth + x] == nodeEnd) {
shape.setPosition(x * this->nNodeSize + (float)this->nNodeBorder, y * this->nNodeSize + (float)this->nNodeBorder);
shape.setFillColor(sf::Color{220,20,60,255});
}
}
}
auto distance = [](sNode* a, sNode* b) // for convenience
{
return sqrt((a->x - b->x) * (a->x - b->x) + (a->y - b->y) * (a->y - b->y));
};
auto heuristic = [distance](sNode* a, sNode* b) // so we can experiment with heuristic
{
return distance(a, b);
};
// setup starting conditions
sNode* nodeCurrent = nodeStart;
nodeStart->fLocalGoal = 0.f;
nodeStart->fGlobalGoal = heuristic(nodeStart, nodeEnd);
// Add start node to not tested list - this will ensure it gets tested.
// As the algorithm progress, newly discovered nodes get added to this list, and will themselves be tested later
list<sNode*> listNotTestedNodes;
listNotTestedNodes.push_back(nodeStart);
// If the not tested list contains nodes, there may be better paths which not yet been explored.
// However, we will also stop searching when we reach the target - there may well be better paths but this one will do - it won't be the longest
while (!listNotTestedNodes.empty() && nodeCurrent != nodeEnd) // find absolutely shorest path // && nodeCurrent != nodeEnd)
{
// Sort untested nodes by global goal, so lowest it first
listNotTestedNodes.sort([](const sNode* lhs, const sNode* rhs) { return lhs->fGlobalGoal < rhs->fGlobalGoal; });
// Front of listedNotTestedNodes is potentially the lowest distance node.
// Our list may also contain nodes that have been visited, so ditch these...
while (!listNotTestedNodes.empty() && listNotTestedNodes.front()->bVisited)
listNotTestedNodes.pop_front();
// ...or abort because there are no valid nodes left to test
if (listNotTestedNodes.empty())
break;
nodeCurrent = listNotTestedNodes.front();
nodeCurrent->bVisited = true; // We only explore a node once
// Check each of this node's neighbors...
for (auto nodeNeighbor : nodeCurrent->vecNeighbors) {
// ... and only if the neighbor is not visted and is not an obstacle, add it to NotTested List
if (!nodeNeighbor->bVisited && nodeNeighbor->bObstacle == 0)
{
listNotTestedNodes.push_back(nodeNeighbor);
}
// Calculate the neighbors potential lowest parent distance
float fPossiblyLowerGoal = nodeCurrent->fLocalGoal + distance(nodeCurrent, nodeNeighbor);
// If choosing to path through this node is a lower distance than what the neighbor currently has set, update the neighbor to use this node
// as the path source, and set its distance scores as necessary
if (fPossiblyLowerGoal < nodeNeighbor->fLocalGoal) {
nodeNeighbor->parent = nodeCurrent;
nodeNeighbor->fLocalGoal = fPossiblyLowerGoal;
// The best path length to the neighbor being tested has changed, so update the neighbor's score.
// The heuristic is used to globally bias the path algorithm, so its knows if it's getter better or worse.
// At some point the algorithm will realize this path is worse and abandon it, and then we go and search along the next best path.
nodeNeighbor->fGlobalGoal = nodeNeighbor->fLocalGoal + heuristic(nodeNeighbor, nodeEnd);
}
}
}
return true;
}
float d = 0.f;
void onUpdate(float dt, sf::Vector2i mousePos) {
d += dt;
this->mousePos = mousePos;
// Use integer division to nicely get cursor position in node space
int nSelectedNodeX = mousePos.x / nNodeSize;
int nSelectedNodeY = mousePos.y / nNodeSize;
if (d >= 0.355)
{
// use mouse to draw maze, shift and ctrl to place start and end
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
if (nSelectedNodeX >= 0 && nSelectedNodeX < nMapWidth) {
if (nSelectedNodeY >= 0 && nSelectedNodeY < nMapHeight) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::LShift))
nodeStart = &nodes[nSelectedNodeY * nMapWidth + nSelectedNodeX];
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::LControl))
nodeEnd = &nodes[nSelectedNodeY * nMapWidth + nSelectedNodeX];
else
nodes[nSelectedNodeY * nMapWidth + nSelectedNodeX].bObstacle = !nodes[nSelectedNodeY * nMapWidth + nSelectedNodeX].bObstacle;
SolveAStar(); // Solve in "real-time" gives a nice effect.
}
}
d = 0;
}
}
// surround startNode with blocks
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
auto x = nodeStart->x;
auto y = nodeStart->y;
nodes[y * nMapWidth + (x - 1)].bObstacle = true;
nodes[y * nMapWidth + (x + 1)].bObstacle = true;
nodes[(y - 1) * nMapWidth + x].bObstacle = true;
nodes[(y + 1) * nMapWidth + x].bObstacle = true;
nodes[(y - 1) * nMapWidth + (x - 1)].bObstacle = true;
nodes[(y - 1) * nMapWidth + (x + 1)].bObstacle = true;
nodes[(y + 1) * nMapWidth + (x - 1)].bObstacle = true;
nodes[(y + 1) * nMapWidth + (x + 1)].bObstacle = true;
}
// toggles diamond around starting noade
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter)) {
auto x = nodeStart->x;
auto y = nodeStart->y;
nodes[(y - 2) * nMapWidth + (x - 2)].bObstacle = true;
nodes[(y - 2) * nMapWidth + (x + 2)].bObstacle = true;
nodes[(y + 2) * nMapWidth + (x - 2)].bObstacle = true;
nodes[(y + 2) * nMapWidth + (x + 2)].bObstacle = true;
nodes[(y - 2) * nMapWidth + (x - 1)].bObstacle = true;
nodes[(y - 2) * nMapWidth + (x + 1)].bObstacle = true;
nodes[(y + 2) * nMapWidth + (x - 1)].bObstacle = true;
nodes[(y + 2) * nMapWidth + (x + 1)].bObstacle = true;
nodes[(y - 1) * nMapWidth + (x - 2)].bObstacle = true;
nodes[(y - 1) * nMapWidth + (x + 2)].bObstacle = true;
nodes[(y + 1) * nMapWidth + (x - 2)].bObstacle = true;
nodes[(y + 1) * nMapWidth + (x + 2)].bObstacle = true;
}
// toggles text on blocks
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num8)) {
b8Connection = !b8Connection;
toggleDiagnols();
this->window->setTitle("[8]Connectivity: " + std::to_string(b8Connection));
}
// sets outline in obsticles
if (sf::Keyboard::isKeyPressed(sf::Keyboard::R)) {
for(int x = 0; x < nMapWidth; x++) {
for(int y = 0; y < nMapHeight; y++) {
if (x == 0 || x == nMapWidth-1 || y == 0 || y == nMapHeight - 1)
nodes[y * nMapWidth + x].bObstacle = true;
}
}
}
// clear all obstacles
if (sf::Keyboard::isKeyPressed(sf::Keyboard::C)) {
for(int x = 0; x < nMapWidth; x++) {
for(int y = 0; y < nMapHeight; y++) {
nodes[y * nMapWidth + x].bObstacle = false;
}
}
}
// Draw Connection First - lines from this nodes position to its connected neighbor node positions
for (int x = 0; x < nMapWidth; x++) {
for (int y = 0; y < nMapHeight; y++) {
for (auto n : nodes[y * nMapWidth + x].vecNeighbors) {
sf::Vertex line[] = {
sf::Vertex(sf::Vector2f(x * this->nNodeSize + this->nNodeSize / 2 + 3,
y * this->nNodeSize + this->nNodeSize / 2 + 3)),
sf::Vertex(sf::Vector2f(n->x * this->nNodeSize + this->nNodeSize / 2 + 3,
n->y * this->nNodeSize + this->nNodeSize / 2 + 3))
};
line->color = sf::Color{30,144,255,255};
this->window->draw(line, 2, sf::Lines);
}
}
}
// Draw Nodes on top
for (int x = 0; x < nMapWidth; x++) {
for (int y = 0; y < nMapHeight; y++) {
if (nodes[y * nMapWidth + x].bObstacle)
shape.setFillColor(sf::Color{47,79,79,255});
else
shape.setFillColor(sf::Color{0,191,255,255});
// shape.setFillColor(sf::Color{0,191,255});
shape.setPosition(x * this->nNodeSize + this->nNodeBorder, y * this->nNodeSize + this->nNodeBorder);
if (nodes[y * nMapWidth + x].bVisited) {
shape.setPosition(x * this->nNodeSize + (float)this->nNodeBorder, y * this->nNodeSize + (float)this->nNodeBorder);
shape.setFillColor(sf::Color{30,144,255,255});
//shape.setFillColor(sf::Color{0,191,255});
}
if (&nodes[y * nMapWidth + x] == nodeStart) {
shape.setPosition(x * this->nNodeSize + (float)this->nNodeBorder, y * this->nNodeSize + (float)this->nNodeBorder);
shape.setFillColor(sf::Color{127,255,0,255});
}
if (&nodes[y * nMapWidth + x] == nodeEnd) {
shape.setPosition(x * this->nNodeSize + (float)this->nNodeBorder, y * this->nNodeSize + (float)this->nNodeBorder);
shape.setFillColor(sf::Color{220,20,60,255});
}
this->window->draw(shape);
}
// Draw Path by starting ath the end, and following the parent node trail
// back to the start - the start node will not have a parent path to follow
if (nodeEnd != nullptr) {
sNode* p = nodeEnd;
while (p->parent != nullptr) {
sf::Vertex line[]
{
sf::Vertex(sf::Vector2f(p->x * this->nNodeSize + this->nNodeSize / 2 + 3, p->y * this->nNodeSize + this->nNodeSize / 2 + 3)),
sf::Vertex(sf::Vector2f(p->parent->x * this->nNodeSize + this->nNodeSize / 2 + 3, p->parent->y * this->nNodeSize + this->nNodeSize / 2 + 3))
};
line->color = sf::Color{255,218,185,255};
this->window->draw(line, 2, sf::Lines);
// Set next node to this node's parent
p = p->parent;
}
}
}
}
private:
struct sNode {
bool bObstacle = false; // is the node an obstruction
bool bVisited = false; // have we searched this node before?
float fGlobalGoal; // Distance to goal so far
float fLocalGoal; // Distance to goal if we took the alternative route
int x; // Node position in 2D space
int y;
std::vector<sNode*> vecNeighbors; // Connection to neighbors
sNode* parent; // Node connecting to this node that offers shortest parent
};
const int nMapWidth = WINDOW_SIZE.x / 32;
const int nMapHeight = WINDOW_SIZE.y / 32;
const int nNodeSize = 32;
const int nNodeBorder = 9;
sNode* nodes = nullptr;
sNode* nodeStart = nullptr;
sNode* nodeEnd = nullptr;
sf::Vector2i mousePos;
sf::RenderWindow* window = nullptr;
bool b8Connection = false;
sf::RectangleShape shape;
sf::Font font;
sf::Text text;
};
int main() {
sf::RenderWindow window{sf::VideoMode{WINDOW_SIZE.x,WINDOW_SIZE.y},"SFML Sandbox"};
window.setFramerateLimit(10);
window.setPosition(sf::Vector2i{window.getPosition().x,0});
sf::Clock dtClock;
AStar astar(&window);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed || event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
window.close();
if (event.type == sf::Event::KeyPressed) {
switch (event.key.code) {
case sf::Keyboard::Enter: cout << "Enter Pressed\n"; break;
case sf::Keyboard::Space: cout << "Space Pressed\n"; break;
default: break;
}
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num1)) {
}
window.clear();
astar.onUpdate(dtClock.restart().asSeconds(), sf::Mouse::getPosition(window));
window.display();
}
return 0;
}