-
Notifications
You must be signed in to change notification settings - Fork 10
/
driver.cpp
617 lines (561 loc) · 21.8 KB
/
driver.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
#define _USE_MATH_DEFINES
#include "driver.h"
// needed to overcome circular dependency errors
#include "ai/gradientdescent.h"
#include "entities/lakitu.h"
#include "map/map.h"
#include "states/race.h"
sf::Time StateRace::currentTime;
const sf::Time Driver::SPEED_UP_DURATION = sf::seconds(1.5f);
const sf::Time Driver::SPEED_DOWN_DURATION = sf::seconds(10.0f);
const sf::Time Driver::STAR_DURATION = sf::seconds(23.0f);
const sf::Time Driver::UNCONTROLLED_DURATION = sf::seconds(1.0f);
const float Driver::COIN_SPEED = 0.007;
// Try to simulate graph from:
// https://www.mariowiki.com/Super_Mario_Kart#Acceleration
void simulateSpeedGraph(Driver *self, float &accelerationLinear) {
if (self->vehicle->convex) {
float speedPercentage =
self->speedForward / self->vehicle->maxNormalLinearSpeed;
if (speedPercentage < 0.25f) {
accelerationLinear += self->vehicle->motorAcceleration / 2.0f;
} else if (speedPercentage < 0.45f) {
accelerationLinear +=
self->vehicle->motorAcceleration * (speedPercentage + 0.075f);
} else if (speedPercentage < 0.95f) {
accelerationLinear += self->vehicle->motorAcceleration / 2.0f;
} else {
accelerationLinear +=
(0.05f * self->vehicle->maxNormalLinearSpeed) / 4.0f;
}
} else {
float speedPercentage =
self->speedForward / self->vehicle->maxNormalLinearSpeed;
if (speedPercentage < 0.45f) {
accelerationLinear += self->vehicle->motorAcceleration / 2.0f;
} else if (speedPercentage < 0.95f) {
accelerationLinear +=
self->vehicle->motorAcceleration * (1.0f - speedPercentage);
} else {
accelerationLinear +=
(0.05f * self->vehicle->maxNormalLinearSpeed) / 4.0f;
}
}
}
// return true if its drifting
bool incrisingAngularAceleration(Driver *self, float &accelerationAngular) {
bool drifting = false;
if (self->pressedToDrift) {
if (Input::held(Key::TURN_RIGHT)) {
self->speedTurn = self->vehicle->maxTurningAngularSpeed * 0.30f;
} else if (Input::held(Key::TURN_LEFT)) {
self->speedTurn =
-1.0 * self->vehicle->maxTurningAngularSpeed * 0.30f;
}
self->pressedToDrift = false;
}
if (std::fabs(self->speedTurn) >
(self->vehicle->maxTurningAngularSpeed * 0.4f) &&
std::fabs(self->speedForward) >
(self->vehicle->maxNormalLinearSpeed * 0.4f)) {
accelerationAngular = self->vehicle->turningAcceleration * 1.0f;
drifting = true;
} else {
accelerationAngular = self->vehicle->turningAcceleration * 0.15f;
}
return drifting;
}
void reduceLinearSpeedWhileTurning(Driver *self, float &accelerationLinear,
float &speedTurn) {
float speedTurnPercentage =
std::fabs(speedTurn / self->vehicle->maxTurningAngularSpeed);
if (self->speedForward > self->vehicle->maxNormalLinearSpeed * 0.9f) {
accelerationLinear =
-1.0 * self->vehicle->motorAcceleration * speedTurnPercentage;
}
}
// update using input service
void Driver::usePlayerControls(float &accelerationLinear) {
// Speed control
if (Input::held(Key::ACCELERATE)) {
simulateSpeedGraph(this, accelerationLinear);
}
if (Input::held(Key::BRAKE)) {
// dont make brakes too high as friction still applies
accelerationLinear += VehicleProperties::BREAK_ACELERATION;
}
if (height == 0.0f) {
bool drift = false;
if (Input::held(Key::TURN_LEFT) && !Input::held(Key::TURN_RIGHT)) {
float accelerationAngular = 0.0;
drift = incrisingAngularAceleration(this, accelerationAngular);
speedTurn = std::fmaxf(speedTurn - accelerationAngular,
vehicle->maxTurningAngularSpeed * -1.0f);
reduceLinearSpeedWhileTurning(this, accelerationLinear, speedTurn);
animator.goLeft(drift);
} else if (Input::held(Key::TURN_RIGHT) &&
!Input::held(Key::TURN_LEFT)) {
float accelerationAngular = 0.0;
drift = incrisingAngularAceleration(this, accelerationAngular);
speedTurn = std::fminf(speedTurn + accelerationAngular,
vehicle->maxTurningAngularSpeed);
reduceLinearSpeedWhileTurning(this, accelerationLinear, speedTurn);
animator.goRight(drift);
}
}
}
// update based on gradient AI
void Driver::useGradientControls(float &accelerationLinear) {
// just accelerate if the ai is jumping
if (height > 0.0f) {
simulateSpeedGraph(this, accelerationLinear);
return;
}
sf::Vector2f dirSum(0.0f, 0.0f);
// if it's going too slow its probably stuck to a wall
// reduce its vision so it knows how to exit the wall
int tilesForward = speedForward < vehicle->maxNormalLinearSpeed / 4.0f
? 1
: Map::getCurrentMapAIFarVision();
for (int i = 0; i < tilesForward; i++) {
dirSum += AIGradientDescent::getNextDirection(position + dirSum);
}
float targetAngle = std::atan2(dirSum.y, dirSum.x);
float diff = targetAngle - posAngle;
diff = fmodf(diff, 2.0f * M_PI);
if (diff < 0.0f) diff += 2.0f * M_PI;
if (fabsf(M_PI - diff) > 0.7f * M_PI) {
// accelerate if it's not a sharp turn
simulateSpeedGraph(this, accelerationLinear);
}
if (height == 0.0f) {
if (diff >= 0.05f * M_PI && diff <= 1.95f * M_PI) {
float accelerationAngular = vehicle->turningAcceleration;
if (diff > M_PI) {
// left turn
speedTurn = std::fmaxf(speedTurn - accelerationAngular,
vehicle->maxTurningAngularSpeed * -1.0f);
reduceLinearSpeedWhileTurning(this, accelerationLinear,
speedTurn);
} else {
// right turn
speedTurn = std::fminf(speedTurn + accelerationAngular,
vehicle->maxTurningAngularSpeed);
reduceLinearSpeedWhileTurning(this, accelerationLinear,
speedTurn);
}
}
}
}
void Driver::updateGradientPosition() {
static constexpr const int CONSECUTIVE_INCREMENTS_FOR_BACKWARDS = 5;
// check bounds
if (position.x < 1e-4f || position.x > 1.0f - 1e-4f || position.y < 1e-4f ||
position.y > 1.0f - 1e-4f) {
return;
}
int gradient = AIGradientDescent::getPositionValue(
position.x * MAP_TILES_WIDTH, position.y * MAP_TILES_HEIGHT);
// either player is on a bad tile (wall?) or gradient didnt change
if (gradient == -1 || gradient == lastGradient) {
return;
}
if (lastGradient == -1) { // lastGradient wasn't initialized
lastGradient = gradient;
return;
}
if (gradient > lastGradient) {
consecutiveGradientIncrements =
std::min(consecutiveGradientIncrements + 1,
CONSECUTIVE_INCREMENTS_FOR_BACKWARDS);
if (consecutiveGradientIncrements ==
CONSECUTIVE_INCREMENTS_FOR_BACKWARDS) {
goingForwards = false;
}
} else {
consecutiveGradientIncrements =
std::max(consecutiveGradientIncrements - 1,
CONSECUTIVE_INCREMENTS_FOR_BACKWARDS * -1);
if (consecutiveGradientIncrements ==
CONSECUTIVE_INCREMENTS_FOR_BACKWARDS * -1) {
goingForwards = true;
}
}
int diff = gradient - lastGradient;
if (diff > AIGradientDescent::GRADIENT_LAP_CHECK) {
laps = laps + 1;
if (controlType == DriverControlType::PLAYER && laps < 6) {
Map::reactivateQuestionPanels();
Lakitu::showLap(laps);
}
} else if (diff < AIGradientDescent::GRADIENT_LAP_CHECK * -1 && laps > 0) {
laps--;
}
lastGradient = gradient;
if (controlType == DriverControlType::PLAYER) {
Lakitu::setWrongDir(isGoingBackwards());
}
}
void Driver::applyMushroom() {
if (controlType == DriverControlType::PLAYER)
Gui::speed(SPEED_UP_DURATION.asSeconds());
speedForward = vehicle->maxSpeedUpLinearSpeed;
pushStateEnd(DriverState::SPEED_UP,
StateRace::currentTime + SPEED_UP_DURATION);
}
void Driver::applyStar() {
if (controlType == DriverControlType::PLAYER)
Audio::play(SFX::CIRCUIT_ITEM_STAR);
speedForward = speedForward * 2.0f;
pushStateEnd(DriverState::STAR, StateRace::currentTime + STAR_DURATION);
animator.star(STAR_DURATION);
}
void Driver::applyThunder() {
speedTurn = 0.0f;
speedForward =
std::fmin(speedForward, vehicle->maxNormalLinearSpeed * 0.6f);
pushStateEnd(DriverState::UNCONTROLLED,
StateRace::currentTime + UNCONTROLLED_DURATION);
animator.small(SPEED_DOWN_DURATION + SPEED_DOWN_DURATION);
pushStateEnd(DriverState::SPEED_DOWN,
StateRace::currentTime + SPEED_DOWN_DURATION);
}
void Driver::shortJump() {
if (height == 0.0f) {
flightAngle = posAngle;
height = 8.0f;
}
}
void Driver::applyHit() {
if (~state & (int)DriverState::STAR) {
addCoin(-1);
pushStateEnd(DriverState::UNCONTROLLED,
StateRace::currentTime + UNCONTROLLED_DURATION);
}
}
void Driver::applySmash() {
addCoin(-2);
animator.smash(SPEED_DOWN_DURATION + UNCONTROLLED_DURATION);
pushStateEnd(DriverState::UNCONTROLLED,
StateRace::currentTime + UNCONTROLLED_DURATION);
}
void handlerHitBlock(Driver *self, const sf::Vector2f &nextPosition) {
sf::Vector2f moveWidth = sf::Vector2f(1.0 / MAP_TILES_WIDTH, 0.0);
sf::Vector2f moveHeight = sf::Vector2f(0.0, 1.0 / MAP_TILES_HEIGHT);
int widthSize = 0;
for (int j = -1; j <= 1; j += 2) {
for (int i = 1; i <= 4; i++) {
if (Map::getLand(nextPosition + float(i * j) * moveWidth) ==
MapLand::BLOCK) {
widthSize++;
} else {
break;
}
}
}
int heightSize = 0;
for (int j = -1; j <= 1; j += 2) {
for (int i = 1; i <= 4; i++) {
if (Map::getLand(nextPosition + float(i * j) * moveHeight) ==
MapLand::BLOCK) {
heightSize++;
} else {
break;
}
}
}
sf::Vector2f momentum =
sf::Vector2f(cosf(self->posAngle), sinf(self->posAngle)) *
float(std::fmax(self->speedForward,
self->vehicle->maxNormalLinearSpeed * 0.5));
if (widthSize > 4 && heightSize < 4) {
self->vectorialSpeed = sf::Vector2f(momentum.x, -momentum.y);
} else if (widthSize < 4 && heightSize > 4) {
self->vectorialSpeed = sf::Vector2f(-momentum.x, momentum.y);
} else {
self->vectorialSpeed = sf::Vector2f(-momentum.x, -momentum.y);
}
}
void Driver::addCoin(int amount) {
// TODO check for negative coins
coins += amount;
if (coins < 11 && controlType == DriverControlType::PLAYER) {
Gui::addCoin(amount);
}
if (coins > 10)
coins = 10;
else if (coins < 0)
coins = 0;
}
void Driver::pickUpPowerUp(PowerUps power) {
powerUp = power;
if (controlType == DriverControlType::PLAYER) {
Gui::setPowerUp(power);
}
}
void Driver::endRaceAndReset() {
// State reset
pressedToDrift = false;
state = (int)DriverState::NORMAL;
for (int i = 0; i < (int)DriverState::_COUNT; i++) {
stateEnd[i] = sf::seconds(0);
}
// Animator reset
animator.reset();
}
void Driver::setPositionAndReset(const sf::Vector2f &newPosition) {
// Location update
position = newPosition;
posAngle = M_PI_2 * -1.0f;
flightAngle = 0;
// Counters reset
laps = 0;
powerUp = PowerUps::NONE;
coins = 0;
goingForwards = true;
lastGradient = -1;
// TODO IMPORTANT clear all states / speeds
// speed, momentum, etc.
speedForward = 0.0f;
speedTurn = 0.0f;
speedUpwards = 0.0f;
collisionMomentum = sf::Vector2f(0.0f, 0.0f);
vectorialSpeed = sf::Vector2f(0.0f, 0.0f);
// State reset
pressedToDrift = false;
state = (int)DriverState::NORMAL;
for (int i = 0; i < (int)DriverState::_COUNT; i++) {
stateEnd[i] = sf::seconds(0);
}
// Animator reset
animator.reset();
// Gui reset
Gui::reset();
}
void improvedCheckOfMapLands(Driver *self, const sf::Vector2f &position,
sf::Vector2f &deltaPosition) {
sf::Vector2f nextPosition = position + deltaPosition;
// TODO: Adjust size when character is set with the correct scale
float halfTileWidthInMapCoord =
float(MAP_TILE_SIZE) / MAP_ASSETS_WIDTH / 2.5f;
float halfTileHeightInMapCoord =
float(MAP_TILE_SIZE) / MAP_ASSETS_HEIGHT / 2.5f;
float deltaAngle[5] = {0, M_PI_2, -M_PI_2, M_PI_4, -M_PI_4};
for (int i = 0; i < 5; i++) {
sf::Vector2f shifting = sf::Vector2f(
cosf(self->posAngle + deltaAngle[i]) * halfTileWidthInMapCoord,
sinf(self->posAngle + deltaAngle[i]) * halfTileHeightInMapCoord);
switch (Map::getLand(nextPosition + shifting)) {
case MapLand::BLOCK:
handlerHitBlock(self, nextPosition + shifting);
self->speedForward = 0.0f;
self->collisionMomentum = sf::Vector2f(0.0f, 0.0f);
deltaPosition = sf::Vector2f(0.0f, 0.0f);
return;
case MapLand::OUTER:
self->animator.fall();
if (DriverControlType::PLAYER == self->controlType)
Lakitu::pickUpDriver(self);
self->position =
AIGradientDescent::getNextDirection(self->position);
return;
default:
break;
}
}
}
void Driver::jumpRamp(const MapLand &land) {
const float RAMP_INCLINATION = 45.0f / 90.0f;
// 384 = 20.0 / (MAX(MAX_LINEAR_SPEED[i]) / 2.0)
speedUpwards = RAMP_INCLINATION * speedForward * 384.0;
speedUpwards = std::fmax(speedUpwards, 10.0);
speedUpwards = std::fmin(speedUpwards, 20.0);
speedForward = (1.0 - RAMP_INCLINATION) * speedForward;
// 0.05 = MIN(MAX_LINEAR_SPEED[i]) / 2.0
speedForward = std::fmax(speedForward, 0.05);
float normalizedAngle = posAngle;
while (normalizedAngle >= 2 * M_PI) {
normalizedAngle -= 2 * M_PI;
}
while (normalizedAngle < 0) {
normalizedAngle += 2 * M_PI;
}
if (land == MapLand::RAMP_HORIZONTAL) {
if (normalizedAngle >= 0 && normalizedAngle <= M_PI) {
flightAngle = M_PI_2;
} else {
flightAngle = 3 * M_PI_2;
}
} else if (land == MapLand::RAMP_VERTICAL) {
if (normalizedAngle > M_PI_2 && normalizedAngle < 3 * M_PI_2) {
flightAngle = M_PI;
} else {
flightAngle = 0;
}
}
}
void Driver::update(const sf::Time &deltaTime) {
// Physics variables
float accelerationLinear = 0.0f;
// Friction
accelerationLinear += VehicleProperties::FRICTION_LINEAR_ACELERATION;
if ((!Input::held(Key::TURN_LEFT) && !Input::held(Key::TURN_RIGHT)) ||
(Input::held(Key::TURN_LEFT) && speedTurn > 0.0f) ||
(Input::held(Key::TURN_RIGHT) && speedTurn < 0.0f)) {
speedTurn /= 1.2f;
}
// remove expired states
popStateEnd(StateRace::currentTime);
if ((state & (int)DriverState::UNCONTROLLED)) {
animator.hit();
} else {
if (height == 0) {
animator.goForward();
}
switch (controlType) {
case DriverControlType::PLAYER:
usePlayerControls(accelerationLinear);
break;
case DriverControlType::AI_GRADIENT:
useGradientControls(accelerationLinear);
break;
default:
break;
}
}
MapLand land = Map::getLand(position);
if (land == MapLand::SLOW && (~state & (int)DriverState::STAR)) {
if (speedForward > vehicle->slowLandMaxLinearSpeed) {
accelerationLinear +=
VehicleProperties::SLOW_LAND_LINEAR_ACELERATION;
}
}
if (height == 0.0f) {
if (land == MapLand::OIL_SLICK && (~state & (int)DriverState::STAR)) {
speedTurn = 0.0f;
speedForward =
std::fmin(speedForward, vehicle->maxNormalLinearSpeed * 0.6f);
pushStateEnd(DriverState::UNCONTROLLED,
StateRace::currentTime + UNCONTROLLED_DURATION);
} else if (land == MapLand::RAMP) {
std::cerr << "ERROR: MapLand::RAMP is deprecated" << std::endl;
} else if (land == MapLand::RAMP_HORIZONTAL ||
land == MapLand::RAMP_VERTICAL) {
jumpRamp(land);
} else if (land == MapLand::ZIPPER) {
pushStateEnd(DriverState::SPEED_UP,
StateRace::currentTime + SPEED_UP_DURATION);
speedForward = vehicle->maxSpeedUpLinearSpeed;
} else if (land == MapLand::OTHER) {
// set a custom destructor to avoid deletion of the object itself
Map::collideWithSpecialFloorObject(
DriverPtr(this, [](Driver *) {}));
}
}
// Gravity
if ((height > 0.0f || speedUpwards > 0.0f) && !onLakitu) {
// -9.8 * 5.0 MANUAL ADJUST
const float gravityAceleration = -9.8 * 6.0;
height = height + speedUpwards * deltaTime.asSeconds() +
0.5 * gravityAceleration * deltaTime.asSeconds() *
deltaTime.asSeconds();
height = std::fmax(height, 0.0f);
speedUpwards =
speedUpwards + gravityAceleration * deltaTime.asSeconds();
if (height == 0.0f) {
speedUpwards = 0.0f;
}
}
float maxLinearSpeed;
if (state & (int)DriverState::SPEED_UP || state & (int)DriverState::STAR) {
maxLinearSpeed = vehicle->maxSpeedUpLinearSpeed;
} else if (state & (int)DriverState::SPEED_DOWN) {
maxLinearSpeed = vehicle->maxSpeedDownLinearSpeed;
} else {
maxLinearSpeed = vehicle->maxNormalLinearSpeed;
}
maxLinearSpeed = maxLinearSpeed + (COIN_SPEED * coins);
// Speed & rotation changes
// Calculate space traveled
float deltaAngle = speedTurn * deltaTime.asSeconds();
float deltaSpace = speedForward * deltaTime.asSeconds() +
accelerationLinear *
(deltaTime.asSeconds() * deltaTime.asSeconds()) /
2.0;
deltaSpace = std::fminf(deltaSpace, maxLinearSpeed * deltaTime.asSeconds());
deltaSpace = std::fmaxf(deltaSpace, 0.0f);
// Update speed
speedForward += accelerationLinear * deltaTime.asSeconds();
speedForward = std::fminf(speedForward, maxLinearSpeed);
speedForward = std::fmaxf(speedForward, 0.0f);
float movementAngle = height == 0.0f ? posAngle : flightAngle;
sf::Vector2f deltaPosition =
sf::Vector2f(cosf(movementAngle), sinf(movementAngle)) * deltaSpace;
// collision momentum
deltaPosition += collisionMomentum;
collisionMomentum /= 1.3f;
deltaPosition += vectorialSpeed * deltaTime.asSeconds();
vectorialSpeed /= 1.3f;
if (height == 0.0f && Map::getLand(position) != MapLand::BLOCK) {
improvedCheckOfMapLands(this, position, deltaPosition);
}
// normal driving
position += deltaPosition;
if (height == 0) {
posAngle += deltaAngle;
posAngle = fmodf(posAngle, 2.0f * M_PI);
}
updateGradientPosition();
animator.update(speedForward, speedTurn, height, deltaTime);
}
bool Driver::canDrive() { return !(state & (int)DriverState::UNCONTROLLED); }
sf::Sprite &Driver::getSprite() { return animator.sprite; }
std::pair<float, sf::Sprite *> Driver::getDrawable(
const sf::RenderTarget &window, const float scale) {
// possible player moving/rotation/etc
float width = window.getSize().x;
float y = window.getSize().y * 45.0f / 100.0f;
//(halfHeight * 3) / 4 + animator.sprite.getGlobalBounds().height * 1.05;
// height is substracted for jump effect
animator.sprite.setPosition(width / 2, y);
float moveX = animator.spriteMovementDrift;
float moveY = animator.spriteMovementSpeed - height;
animator.sprite.move(moveX * scale, moveY * scale);
animator.sprite.scale(scale, scale);
float z = Map::CAM_2_PLAYER_DST / MAP_ASSETS_WIDTH;
return std::make_pair(z, &animator.sprite);
}
void Driver::pushStateEnd(DriverState state, const sf::Time &endTime) {
this->state |= (int)state;
stateEnd[(int)log2((int)state)] = endTime;
}
int Driver::popStateEnd(const sf::Time ¤tTime) {
int finishedEstates = 0;
if (this->state != 0) {
int state = 1;
for (int i = 0; i < (int)DriverState::_COUNT; i++) {
if (this->state & state) {
if (stateEnd[i] < currentTime) {
finishedEstates |= state;
this->state &= ~state;
}
}
state *= 2;
}
}
return finishedEstates;
}
bool Driver::solveCollision(CollisionData &data, const sf::Vector2f &otherSpeed,
const sf::Vector2f &otherPos,
const float otherWeight, const float distance2) {
sf::Vector2f speed =
speedForward * sf::Vector2f(cosf(posAngle), sinf(posAngle));
float weight = vehicle->weight;
sf::Vector2f quantity = speed * weight + otherSpeed * otherWeight;
quantity /= (weight + otherWeight) * weight;
sf::Vector2f dir = (otherPos - position) / sqrtf(distance2 + 1e-2f);
float mod =
sqrtf(quantity.x * quantity.x + quantity.y * quantity.y + 1e-2f);
data = CollisionData(dir * mod, 1.0f);
return true;
}