-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopponent.cpp
More file actions
147 lines (121 loc) · 4.38 KB
/
Copy pathopponent.cpp
File metadata and controls
147 lines (121 loc) · 4.38 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
/***************************************************************************
file : opponent.cpp
created : Thu Apr 22 01:20:19 CET 2003
copyright : (C) 2003 Bernhard Wymann
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "opponent.h"
/* class variables and constants */
tTrack* Opponent::track;
float Opponent::FRONTCOLLDIST = 200.0; /* [m] distance to check for other cars */
float Opponent::BACKCOLLDIST = 50.0; /* [m] distance to check for other cars */
float Opponent::LENGTH_MARGIN = 2.0; /* [m] safety margin */
float Opponent::SIDE_MARGIN = 1.0; /* [m] safety margin */
Opponent::Opponent()
{
}
/* compute speed component parallel to the track */
float Opponent::getSpeed(tCarElt *car)
{
v2d speed, dir;
float trackangle = RtTrackSideTgAngleL(&(car->_trkPos));
speed.x = car->_speed_X;
speed.y = car->_speed_Y;
dir.x = cos(trackangle);
dir.y = sin(trackangle);
return speed*dir;
}
/* Compute the length to the start of the segment */
float Opponent::getDistToSegStart()
{
if (car->_trkPos.seg->type == TR_STR) {
return car->_trkPos.toStart;
} else {
return car->_trkPos.toStart*car->_trkPos.seg->radius;
}
}
/* Update the values in Opponent this */
void Opponent::update(tSituation *s, Driver *driver)
{
tCarElt *mycar = driver->getCarPtr();
/* init state of opponent to ignore */
state = OPP_IGNORE;
/* if the car is out of the simulation ignore it */
if (car->_state & RM_CAR_STATE_NO_SIMU) {
return;
}
/* updating distance along the middle */
float oppToStart = car->_trkPos.seg->lgfromstart + getDistToSegStart();
distance = oppToStart - mycar->_distFromStartLine;
if (distance > track->length/2.0) {
distance -= track->length;
} else if (distance < -track->length/2.0) {
distance += track->length;
}
/* update speed in track direction */
speed = Opponent::getSpeed(car);
float cosa = speed/sqrt(car->_speed_X*car->_speed_X + car->_speed_Y*car->_speed_Y);
float alpha = acos(cosa);
width = car->_dimension_x*sin(alpha) + car->_dimension_y*cosa;
float SIDECOLLDIST = MIN(car->_dimension_x, mycar->_dimension_x);
/* is opponent in relevant range -50..200 m */
if (distance > -BACKCOLLDIST && distance < FRONTCOLLDIST) {
/* is opponent in front and slower */
if (distance > SIDECOLLDIST && speed < driver->getSpeed()) {
catchdist = driver->getSpeed()*distance/(driver->getSpeed() - speed);
state |= OPP_FRONT;
distance -= MAX(car->_dimension_x, mycar->_dimension_x);
distance -= LENGTH_MARGIN;
float cardist = car->_trkPos.toMiddle - mycar->_trkPos.toMiddle;
sidedist = cardist;
cardist = fabs(cardist) - fabs(width/2.0) - mycar->_dimension_y/2.0;
if (cardist < SIDE_MARGIN) state |= OPP_COLL;
} else
/* is opponent behind and faster */
if (distance < -SIDECOLLDIST && speed > driver->getSpeed()) {
catchdist = driver->getSpeed()*distance/(speed - driver->getSpeed());
state |= OPP_BACK;
distance -= MAX(car->_dimension_x, mycar->_dimension_x);
distance -= LENGTH_MARGIN;
} else
/* is opponent aside */
if (distance > -SIDECOLLDIST &&
distance < SIDECOLLDIST) {
sidedist = car->_trkPos.toMiddle - mycar->_trkPos.toMiddle;
state |= OPP_SIDE;
}
}
}
/* Initialize the list of opponents */
Opponents::Opponents(tSituation *s, Driver *driver)
{
opponent = new Opponent[s->_ncars - 1];
int i, j = 0;
for (i = 0; i < s->_ncars; i++) {
if (s->cars[i] != driver->getCarPtr()) {
opponent[j].setCarPtr(s->cars[i]);
j++;
}
}
Opponent::setTrackPtr(driver->getTrackPtr());
nopponents = s->_ncars - 1;
}
Opponents::~Opponents()
{
delete [] opponent;
}
/* Updates all the Opponent instances */
void Opponents::update(tSituation *s, Driver *driver)
{
int i;
for (i = 0; i < s->_ncars - 1; i++) {
opponent[i].update(s, driver);
}
}