-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarai.cpp
171 lines (140 loc) · 4.15 KB
/
carai.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
#include "carai.h"
CarAI::CarAI(const CarAI &other) :
car(nullptr),
path(other.path),
timer(nullptr),
currPoint(other.currPoint),
carAIs(other.carAIs),
processDone(other.processDone),
graphicsScene(other.graphicsScene)
{
if (other.car != nullptr)
{
car = new Car(*other.car);
connect(car, &Car::needToUpdateLevelOfCar, this, [&] () {
graphicsScene->setItemLayer(car, car->GetPosition().level);
});
emit car->needToUpdateLevelOfCar();
}
if (other.timer != nullptr)
{
timer = new QTimer;
timer->setInterval(TICK_TIME);
connect(timer, &QTimer::timeout, this, &CarAI::Update);
timer->start();
}
}
CarAI::CarAI(CarAI &&other) noexcept :
car(other.car),
path(other.path),
timer(other.timer),
currPoint(other.currPoint),
carAIs(other.carAIs),
processDone(other.processDone),
graphicsScene(other.graphicsScene)
{
if (timer != nullptr)
{
connect(timer, &QTimer::timeout, this, &CarAI::Update);
}
if (car != nullptr)
{
connect(car, &Car::needToUpdateLevelOfCar, this, [&] () {
graphicsScene->setItemLayer(car, car->GetPosition().level);
});
}
other.car = nullptr;
other.timer = nullptr;
other.path = nullptr;
other.carAIs = nullptr;
other.graphicsScene = nullptr;
}
CarAI::CarAI(const QVector<RoadPoint> *path, const QVector<CarAI>* carAIs, GraphicScene* graphicsScene) :
car(new Car((*path)[0], vectorAngle((*path)[1].pos - (*path)[0].pos))),
path(path),
timer(new QTimer),
currPoint(1),
carAIs(carAIs),
processDone(false),
graphicsScene(graphicsScene)
{
timer->setInterval(TICK_TIME);
connect(timer, &QTimer::timeout, this, &CarAI::Update);
connect(car, &Car::needToUpdateLevelOfCar, this, [&] () {
graphicsScene->setItemLayer(car, car->GetPosition().level);
});
timer->start();
emit car->needToUpdateLevelOfCar();
}
CarAI::~CarAI()
{
ForceStop();
}
void CarAI::Update()
{
while (isEqual(car->GetPosition().pos, (*path)[currPoint].pos, SUCCESS_EPS))
{
car->setLevel((*path)[currPoint++].level);
if (currPoint == path->size())
{
ForceStop();
return;
}
}
QPointF position = car->GetPosition().pos;
int level = car->GetPosition().level;
float angle = car->GetAngle();
QPointF rotatedVector = rotateVector(QPointF(1, 0), angle);
auto angleDelta = angleBetweenVectors((*path)[currPoint].pos - position, rotatedVector);
car->Rotate(angleDelta * REACTION_COEFFICIENT);
angleDelta = qAbs(angleDelta);
float angleCoef = rotatedVector.y() / rotatedVector.x();
float offset = position.y() - angleCoef * position.x();
float distanceToNearestCar = MAX_VISIBILITY_LENGTH;
for (int iter = 0;iter < carAIs->size(); ++iter)
{
if ((*carAIs)[iter].isDone() || (*carAIs)[iter].GetCar() == car)
{
continue;
}
QPointF currPos = (*carAIs)[iter].GetCar()->PredictPosition().pos;
int currLevel = (*carAIs)[iter].GetCar()->PredictPosition().level;
if (currLevel != level || scalarMultiplicationVectors(currPos - position, rotatedVector) < 0)
{
continue;
}
float currOffset = currPos.y() - angleCoef * currPos.x();
float dist = qAbs(qSin(angle - PI / 2) * (currOffset - offset));
if (dist < MAX_VISIBILITY_WIDTH)
{
float projectionLength = qSqrt(qPow(vectorLength(position - currPos), 2) - qPow(dist, 2));
if (distanceToNearestCar > projectionLength)
{
distanceToNearestCar = projectionLength;
}
}
}
car->setVelocity(MAX_SPEED * ((distanceToNearestCar - STOP_POINT) / MAX_VISIBILITY_LENGTH) * ((PI - angleDelta) / PI));
}
void CarAI::ForceStop()
{
if (car != nullptr)
{
delete car;
car = nullptr;
}
if (timer != nullptr)
{
delete timer;
timer = nullptr;
}
processDone = true;
}
bool CarAI::isDone() const
{
return processDone;
}
Car* CarAI::GetCar() const
{
return car;
}