-
Notifications
You must be signed in to change notification settings - Fork 0
/
PathFollowing.cpp
95 lines (78 loc) · 3.01 KB
/
PathFollowing.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
#include <vector>
#include "Dijkstra.hpp"
#include "game.hpp"
#include "Mobile.hpp"
#include "PathFollowing.hpp"
PathFollowing::PathFollowing(std::string name, Mobile *character, Mobile *target, double maxSpeed, double targetRadius, double slowRadius):
DirectKinematicV(name),
character(character),
target(target),
maxSpeed(maxSpeed),
targetRadius(targetRadius),
slowRadius(slowRadius),
create(false),
begin(NULL),
end(NULL)
{}
void PathFollowing::create_Path() {
double d;
d = (nodes[0]->pos - character->pos).length();
this->begin = nodes[0];
for (unsigned int i = 1; i < nodes.size(); i++) {
if (d > (nodes[i]->pos - character->pos).length()) {
d = (nodes[i]->pos - character->pos).length();
this->begin = nodes[i];
}
}
d = (nodes[0]->pos - target->pos).length();
this->end = nodes[0];
for (unsigned int i = 1; i < nodes.size(); i++) {
if (d > (nodes[i]->pos - target->pos).length()) {
d = (nodes[i]->pos - target->pos).length();
this->end = nodes[i];
}
}
path = dijkstra(character, begin, end);
//for (unsigned int i = 0; i < path.size(); i++)
// path[i]->print_node();
for (unsigned int i = 0; i < path.size(); i++)
path[i]->add_mod(character, 500);
}
std::vector<Triple> PathFollowing::getVel(unsigned int ticks, unsigned int delta_ticks) {
Triple steering;
Triple dir;
double d, targetSpeed;
if (!create) {
this->create = true;
this->create_Path();
}
if (path.size() > 0) {
dir = path.front()->pos - character->pos;
d = dir.length();
if (d < targetRadius) {
for (auto it = path.front()->mods.begin(); it != path.front()->mods.end(); ++it) {
if (std::get<0>(*it) == character) {
path.front()->mods.erase(it);
break;
}
}
path.erase(path.begin());
}
steering = dir.normalized();
steering *= maxSpeed;
} else {
Triple cp, tp;
std::tie(cp, tp) = points(character, target);
dir = tp - cp;
d = dir.length();
if (d < targetRadius) {
dead = true;
create = false;
return std::vector<Triple>();
}
targetSpeed = maxSpeed;
if (d < slowRadius) targetSpeed *= (d - targetRadius) / (slowRadius - targetRadius);
steering = dir * targetSpeed;
}
return std::vector<Triple>(1, steering);
}