-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVehicle.cpp
More file actions
441 lines (385 loc) · 10.1 KB
/
Copy pathVehicle.cpp
File metadata and controls
441 lines (385 loc) · 10.1 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
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
#include "Vehicle.h"
#include <vector>
#include <cstdlib>
#define VEHICLE_MASS 0.00005f
#define SEEK_MESSAGE "SEEK"
#define SEEK_RADIUS 10
Vehicle::Vehicle() : m_forceMotion(VEHICLE_MASS, getPositionAddress())
{
m_currentPosition = Vector2D(0,0);
m_lastPosition = Vector2D(0, 0);
//m_waypointManager = nullptr;
m_otherCar = nullptr;
m_speed = 1;
m_fuel = 100;
m_path.clear();
//create whiskers and set to 0 at start
//the position will change on update whiskers
int noWhiskers = 1;
for (int i = 0; i < noWhiskers; i++)
{
m_whiskers.push_back(Vector2D(0, 0));
}
}
HRESULT Vehicle::initMesh(ID3D11Device* pd3dDevice, carColour colour)
{
m_scale = XMFLOAT3(30, 20, 1);
if (colour == carColour::redCar)
{
setTextureName(L"Resources\\car_red.dds");
}
else if (colour == carColour::blueCar)
{
setTextureName(L"Resources\\car_blue.dds");
}
HRESULT hr = DrawableGameObject::initMesh(pd3dDevice);
setPosition(Vector2D(0, 0));
m_lastPosition = Vector2D(0, 0);
return hr;
}
void Vehicle::update(const float deltaTime)
{
updateMessages(deltaTime);
/*switch (m_carMode)
{
case TAXI:
{
TaxiUpdate();
break;
}
case SABOTAGE:
{
SabotageUpdate();
break;
}
}*/
updateState();
m_forceMotion.update(deltaTime);
// rotate the object based on its last & current position
Vector2D diff = m_currentPosition - m_lastPosition;
for (int i = 0; i < m_whiskers.size(); i++)
{
UpdateWhisker(i, diff);
}
if (diff.Length() > 0) { // if zero then don't update rotation
diff.Normalize();
m_radianRotation = atan2f((float)diff.y, (float)diff.x); // this is used by DrawableGameObject to set the rotation
}
m_lastPosition = m_currentPosition;
// set the current poistion for the drawablegameobject
setPosition(m_currentPosition);
if (m_fuel > 0) {
m_fuel -= 0.02;
}
if (m_fuel <= 0) {
m_fuel = 0;
m_speed = 1;
}
std::cout << m_fuel;
DrawableGameObject::update(deltaTime);
}
void Vehicle::TaxiUpdate()
{
Vector2D cardis = m_otherCar->getPosition() - m_currentPosition;
Vector2D passengeris = m_passengerPosition - m_currentPosition;
//handles state trantions for taxi mode
if (m_fuel < 25) {
state = SEEK;
targetPosition = m_waypointManager->getNearestWaypoint(m_fuelPosition);
}
else if (abs(cardis.x) < 10|| abs(cardis.y) < 10) {
int random = rand() % 4;
if (random == 1) {
state = PURSUIT;
}
else {
state = FLEE;
}
}
else if (abs(passengeris.x) < 20|| abs(passengeris.y) < 20) {
state = ARRIVE;
targetPosition = m_waypointManager->getNearestWaypoint(m_passengerPosition);
}
else
{
state = WANDER;
}
}
void Vehicle::SabotageUpdate()
{
//this will need to check some things havent been called before
Vector2D cardis = m_otherCar->getPosition() - m_currentPosition;
Vector2D speedis = m_speedPosition - m_currentPosition;
//handles state trantions for sabo mode
if (m_fuel < 50) {
state = SEEK;
targetPosition = m_waypointManager->getNearestWaypoint(m_fuelPosition);
}
else if (abs(cardis.x) < 10 || abs(cardis.y) < 10)
{
int random = rand() % 4;
if (random == 1)
{
state = FLEE;
}
else
{
state = PURSUIT;
}
}
else if (abs(speedis.x) < 20 || abs(speedis.y) < 20) {
state = SEEK;
targetPosition = m_waypointManager->getNearestWaypoint(m_speedPosition);
}
else
{
state = WANDER;
}
}
void Vehicle::updateState()
{
switch (state)
{
case WANDER:
{
//call every x frames
break;
}
case SEEK:
{
Vector2D carpos = getPosition();
Vector2D targetspos = getPosition();
if (FindDistance(getPosition(), targetPosition->getPosition()) > 0.1f)
{
Seek();
}
if (FindDistance(getPosition(), targetPosition->getPosition()) < 0.1f)
{
m_path.erase(m_path.begin());
if (m_path.empty() == true)
{
state = WAIT;
}
else
{
targetPosition = m_path[0];
}
}
break;
}
case FLEE:
{
//need a limit on flee or it goes forever
if (state != m_previousState)
{
Flee();
}
break;
}
case ARRIVE:
{
if (getPosition() != targetPosition->getPosition()) {
Arrive(); //jittering issue
}
else {
state = WAIT;
}
break;
}
case PURSUIT:
{
Pursuit();
break;
}
case WAIT:
{
getForceMotion()->clearForce();
break;
}
}
m_previousState = state;
}
// set the current position
void Vehicle::setPosition(Vector2D position)
{
m_currentPosition = position;
DrawableGameObject::setPosition(position);
}
void Vehicle::applyForceToPosition(const Vector2D& positionTo, string name)
{
// create a vector from the position to, and the current car position
Vector2D posFrom = getPosition();
Vector2D force = positionTo - posFrom;
// normalise this (make it length 1)
force.Normalize();
force = force * m_speed;
getForceMotion()->applyForce(force);
// Tutorial todo
// create a message called 'SEEK' which detects when the car has reached a certain point
// note: this has been done for you in the updateMessages function.
MessagePosition message;
message.name = name;
message.position = positionTo;
addMessage(message);
//addMessage(message);
}
void Vehicle::setWaypointManager(WaypointManager* wpm)
{
m_waypointManager = wpm;
}
// -------------------------------------------------------------------------------
// a really rubbish messaging system.. there is clearly a better way to do this...
void Vehicle::addMessage(MessagePosition message)
{
m_vecMessages.push_back(message);
}
void Vehicle::updateMessages(const float deltaTime)
{
// create an iterator to iterate the message list
list<MessagePosition>::iterator messageIterator = m_vecMessages.begin();
// loop while the iterator is not at the end
while (messageIterator != m_vecMessages.end())
{
MessagePosition msg = *messageIterator;
if (msg.name.compare(SEEK_MESSAGE) == 0)
{
Vector2D differenceVector = getPosition() - msg.position;
// WARNING - when testing distances, make sure they are large enough to be detected. Ask a lecturer if you don't understand why. 10 *should* be about right
if (differenceVector.Length() < SEEK_RADIUS)
{
messageReceived(msg);
// delete the message. This will also assign(increment) the iterator to be the next item in the list
messageIterator = m_vecMessages.erase(messageIterator);
continue; // continue the next loop (we don't want to increment below as this will skip an item)
}
}
messageIterator++; // incremenet the iterator
}
}
void Vehicle::messageReceived(MessagePosition message)
{
if (message.name.compare(SEEK_MESSAGE) == 0)
{
// Tutorial Todo
m_forceMotion.clearForce();
}
}
void Vehicle::Seek()
{
applyForceToPosition(targetPosition->getPosition(), SEEK_MESSAGE);
}
//Flee
void Vehicle::Flee()
{
Vector2D force = (getPosition() - targetPosition->getPosition());
force.Normalize();
getForceMotion()->applyForce(force);
}
//arrive
void Vehicle::Arrive()
{
//needs to slow down sooner
Vector2D force = (targetPosition->getPosition() - getPosition()) * m_speed;
if (abs(force.x) > 1 || abs(force.y) > 1)
{
force.Normalize();
//force += force * m_speed;
}
getForceMotion()->applyForce(force);
if (abs(force.x) < 0.2 && abs(force.y) < 0.2) {
setPosition(targetPosition->getPosition());
state = WAIT;
}
}
void Vehicle::Wander(WaypointManager* waypoints)
{
SetTargetPosition(GetRandomWaypoint());
//chooses a waypoint to move to, but after a second or two (unless it reaches the waypoint first), it then chooses another waypoint.
}
void Vehicle::Pursuit()
{
//finds and follows the other car, always staying behind it.
}
void Vehicle::UpdateWhisker(int whiskerIndex, Vector2D direction)
{
//whisker = vehicle pos + vehicle dir
m_whiskers[whiskerIndex] = (getPosition() + direction);
//std::cout << m_whiskers[whiskerIndex];
}
void Vehicle::AddFuel() {
m_fuel += 25;
if (m_fuel > 100)
{
m_fuel = 100;
}
}
void Vehicle::AddSpeed() {
if (m_fuel > 0)
{
//add extra speed for x seconds
}
}
void Vehicle::FindPath(Waypoint* target)
{
//Open List is a collection of all possible waypoints.
vecWaypoints openList;
openList.clear();
for (int i = 0; i < m_waypointManager->getWaypointCount(); i++) {
openList.push_back(m_waypointManager->getWaypoint(i));
}
//closed list is a collection of all visited nodes, it is added to when a node is visited.
vecWaypoints closedList;
//this is the path the AI will follow
vecWaypoints pathToFollow;
//find current nearest waypoint from position
Waypoint* CurrentNode = m_waypointManager->getNearestWaypoint(this->getPosition());
//add current closest node to path and closed list
closedList.push_back(CurrentNode);
pathToFollow.push_back(CurrentNode);
//get neighboring waypoints and get the closest one until you reach the target node
while (CurrentNode != target)
{
vecWaypoints neighbors = m_waypointManager->getNeighbouringWaypoints(CurrentNode); //getting neighboring waypoints
Waypoint* nextNode = nullptr; //current waypoint with shortest path
for (int i = 0; i < neighbors.size(); i++) //loop to find which neighbor has shortest path
{
if (std::count(openList.begin(), openList.end(), neighbors[i])) { //checks that current neigthbor is not already in closed
if (nextNode == nullptr) {
nextNode = neighbors[i];
}
else
{
if (CurrentNode->distanceToWaypoint(neighbors[i]) + neighbors[i]->distanceToWaypoint(target) < CurrentNode->distanceToWaypoint(nextNode) + nextNode->distanceToWaypoint(target))
{
nextNode = neighbors[i];
}
}
}
}
pathToFollow.push_back(nextNode);
closedList.push_back(nextNode);
openList.erase(std::remove(openList.begin(), openList.end(), nextNode), openList.end());
CurrentNode = nextNode;
}
m_path = pathToFollow;
}
float Vehicle::FindDistance(Vector2D point1, Vector2D point2)
{
float a = (point1.x - point2.x) * (point1.x - point2.x);
float b = (point1.y - point2.y) * (point1.y - point2.y);
float c = abs(a + b) /100;
return c;
}
Waypoint* Vehicle::GetRandomWaypoint() {
int randomNumber = rand() % m_waypointManager->getWaypointCount();
Waypoint* Waypoint = m_waypointManager->getWaypoint(randomNumber);
return Waypoint;
}
void Vehicle::SetTargetPosition(Vector2D target)
{
FindPath(m_waypointManager->getNearestWaypoint(target));
}
void Vehicle::SetTargetPosition(Waypoint* target)
{
FindPath(target);
}