-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnavigation.cpp
More file actions
77 lines (66 loc) · 3.26 KB
/
Copy pathnavigation.cpp
File metadata and controls
77 lines (66 loc) · 3.26 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
#include "navigation.h"
Navigation::Navigation(quint8 playerId, WorldMap *worldMap) {
_worldMap = worldMap;
_playerId = playerId;
_univector = new Univector(playerId);
_gui = nullptr;
}
QList<Geometry::Vector2D> Navigation::getObstacles(bool avoidOpponents, bool avoidTeammates, bool avoidBall){
Common::Enums::Color ourColor = Suassuna::Constants::teamColor();
QList<Geometry::Vector2D> obstacles;
if (avoidOpponents) {
Common::Enums::Color theirColor;
if (ourColor == Common::Enums::Color::YELLOW) {
theirColor = Common::Enums::Color::BLUE;
} else {
theirColor = Common::Enums::Color::YELLOW;
}
QList<quint8> opPlayers = getWorldMap()->getPlayersIds(theirColor);
for(int i = 0; i < opPlayers.size(); i++) {
QPair<Geometry::Vector2D, bool> pos = getWorldMap()->getPlayerPosition(theirColor, opPlayers[i]);
if(pos.second){
obstacles.push_back(Geometry::Vector2D(pos.first.x() * 1000.0, pos.first.y() * 1000.0));
}
}
}
if (avoidTeammates) {
QList<quint8> allyPlayers = getWorldMap()->getPlayersIds(ourColor);
for(int i = 0; i < allyPlayers.size(); i++) {
if(allyPlayers[i] != _playerId) {
QPair<Geometry::Vector2D, bool> pos = getWorldMap()->getPlayerPosition(ourColor, allyPlayers[i]);
if(pos.second){
obstacles.push_back(Geometry::Vector2D(pos.first.x() * 1000.0, pos.first.y() * 1000.0));
}
}
}
}
if (avoidBall) {
Geometry::Vector2D ballPos = getWorldMap()->getBall().getPosition();
obstacles.push_back(ballPos);
}
return obstacles;
}
QVector<float> Navigation::getVector(Geometry::Vector2D robotPos, Geometry::Vector2D targetPos, bool avoidOpponents, bool avoidTeammates, bool avoidBall){
QList<Geometry::Vector2D> obstacles = getObstacles(avoidOpponents, avoidTeammates, avoidBall);
QVector<float> uni = _univector->generateUnivectorField(robotPos, targetPos, obstacles, QPair<float, float>(0, 0), QPair<float, float>(0, 0));
// QVector<float> uni = _univector->generateHyperbolicField(targetPos, robotPos);
return uni;
}
Geometry::Angle Navigation::angleToTarget(Geometry::Vector2D robotPos, Geometry::Vector2D targetPos, bool avoidOpponents, bool avoidTeammates, bool avoidBall){
robotPos = Geometry::Vector2D(1000 * robotPos.x(), 1000 * robotPos.y());
targetPos = Geometry::Vector2D(1000 * targetPos.x(), 1000 * targetPos.y());
//std::cout << "robotPos: " << robotPos.x() << " " << robotPos.y() << " targetPos: " << targetPos.x() << " " << targetPos.y() << std::endl;
QVector<float> v = getVector(robotPos, targetPos, avoidOpponents, avoidTeammates, avoidBall);
Geometry::Vector2D resultPos = Geometry::Vector2D(robotPos.x() + 100 * v[0], robotPos.y() + 100 * v[1]);
Geometry::Angle angle = Geometry::Angle(atan2(resultPos.y() - robotPos.y(), resultPos.x() - robotPos.x()));
//std::cout << "V: " << v[0] << " " << v[1] << std::endl;
return angle;
}
WorldMap *Navigation::getWorldMap(){
if(_worldMap == nullptr) {
spdlog::info("[ERROR] WorldMap with nullptr value at Navigation.");
} else {
return _worldMap;
}
return nullptr;
}