-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObstacle.cpp
More file actions
executable file
·278 lines (245 loc) · 7.61 KB
/
Obstacle.cpp
File metadata and controls
executable file
·278 lines (245 loc) · 7.61 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
#include "Obstacle.h"
#define OBSLog gSpeedLog << "[Obstac] "
Obstacle *Obstacle::mInstance = NULL;
Obstacle::Obstacle( WorldModel *wm )
{
mWorldModel = wm;
}
Obstacle::~Obstacle()
{
if( mInstance )
{
delete mInstance;
mInstance = NULL;
}
}
bool Obstacle::IsObstacle( Vector3f tarGoto, Vector3f obs, float radius )
{
Vector3f selfPos = mWorldModel->mSelf.mPos;
Angle selfToTarAng = selfPos.angTheta2( tarGoto );
Line selfToTarLine = Line::makeLineFromPositionAndAngle( selfPos, selfToTarAng );
float obsDistToLine = selfToTarLine.getDistanceWithPoint( obs );
Vector3f obsNearestPointInLine = selfToTarLine.getPointOnLineClosestTo( obs );
float obsDistToNearestPointInLine = obs.distXY2(obsNearestPointInLine);
if( radius > 0.15f ) // 判断player
{
if( obs.distXY2( tarGoto ) < 0.1f )
{
return false;
}
}
// else
// {
// if( obs.distXY2( tarGoto ) < 0.1f )
// {
// return true;
// }
// }
if( obsDistToLine > radius )
{
return false;
}
if( obs.distXY2(selfPos) < 0.15f )
{
return false;
}
// if( obsDistToNearestPointInLine < 0.1f )
// {
// return false;
// }
if( selfToTarLine.isInBetween(obsNearestPointInLine, selfPos, tarGoto ) )
{
return true;
}
return false;
}
bool Obstacle::IsPlayerObstacle( Vector3f tarGoto, Vector3f playerVector3f )
{
const float playerRadius = 0.5f;
return IsObstacle( tarGoto, playerVector3f, playerRadius );
}
bool Obstacle::IsOurPlayerObstacle( Vector3f tarGoto )
{
for( int i = 0; i < mWorldModel->mOurPlayers.size(); i ++ )
{
if( mWorldModel->mOurPlayers.at(i).mMyPlayerNo == mWorldModel->mMyPlayerNo )
{
continue;
}
if( IsPlayerObstacle( tarGoto, mWorldModel->mOurPlayers.at(i).mPos ) )
{
return true;
}
}
return false;
}
bool Obstacle::IsOppPlayerObstacle( Vector3f tarGoto )
{
for( int i = 0; i < mWorldModel->mOppPlayers.size(); i ++ )
{
if( IsPlayerObstacle( tarGoto, mWorldModel->mOppPlayers.at(i).mPos ) )
{
return true;
}
}
return false;
}
Vector3f Obstacle::GetRidOfObstacle( Vector3f tarGoto )
{
const Angle unitAngle = 3.0f;
Vector3f selfPos = mWorldModel->mSelf.mPos;
Vector3f ballPos = mWorldModel->mBall.mPos;
Vector3f avoidGoalpostGoto = tarGoto;
Vector3f oppGoalCenter = mWorldModel->mOppGoalCenter;
if( ballPos.distXY2(oppGoalCenter) < 2.0f )
{
return tarGoto;
}
//避开球门柱
Vector3f oppGoalLeft = mWorldModel->mFieldGoal[GOAL_OPP_LEFT].mPos;
Vector3f oppGoalRight = mWorldModel->mFieldGoal[GOAL_OPP_RIGHT].mPos;
Vector3f ourGoalLeft = mWorldModel->mFieldGoal[GOAL_OUR_LEFT].mPos;
Vector3f ourGoalRight = mWorldModel->mFieldGoal[GOAL_OUR_RIGHT].mPos;
oppGoalLeft.x += 0.1f; // 把球门柱的影响设大一些
oppGoalRight.x += 0.1f;
ourGoalLeft.x -= 0.1f;
ourGoalRight.x -= 0.1f;
oppGoalLeft.y += 0.1f;
oppGoalRight.y -= 0.1f;
ourGoalLeft.y += 0.1f;
ourGoalRight.y -= 0.1f;
// Line oppGoalLeftLine = Line::makeLineFromPositionAndAngle(oppGoalLeft,0.0f);
// Line oppGoalRightLine = Line::makeLineFromPositionAndAngle(oppGoalRight,0.0f);
// Line ourGoalLeftLine = Line::makeLineFromPositionAndAngle(ourGoalLeft,180.0f);
// Line ourGoalRightLine = Line::makeLineFromPositionAndAngle(ourGoalRight,180.0f);
// Line selfToTarLine = Line::makeLineFromPositionAndAngle(selfPos,selfPos.angTheta2(tarGoto));
// Vector3f interceptPos_1 = oppGoalLeftLine.getIntersection(oppGoalLeftLine);
// Vector3f interceptPos_2 = oppGoalLeftLine.getIntersection(oppGoalRightLine);
// Vector3f interceptPos_3 = oppGoalLeftLine.getIntersection(ourGoalLeftLine);
// Vector3f interceptPos_4 = oppGoalLeftLine.getIntersection(ourGoalRightLine);
bool neetToAvoidDoorpost = false;
if( fabs(selfPos.x) > mWorldModel->mFieldLength * 0.5f && fabs(selfPos.y) < mWorldModel->mFieldWidth * 0.5f )
{
if( IsObstacle(tarGoto,oppGoalLeft,0.2f)
|| IsObstacle(tarGoto,oppGoalRight,0.2f)
|| IsObstacle(tarGoto,ourGoalLeft,0.2f)
|| IsObstacle(tarGoto,ourGoalRight,0.2f) )
{
neetToAvoidDoorpost = true;
}
}
if( neetToAvoidDoorpost )
{
if(fabs(selfPos.y) > mWorldModel->mGoalWidth * 0.5f)
{
avoidGoalpostGoto.x = 0.0f;
avoidGoalpostGoto.y = selfPos.y;
}
// else if( fabs(selfPos.x) < mWorldModel->mFieldLength * 0.5f + mWorldModel->mGoalDepth )
// {
// avoidGoalpostGoto.x = 0.0f;
// avoidGoalpostGoto.y = selfPos.y;
// }
else if( fabs(selfPos.x) < mWorldModel->mFieldLength * 0.5f + mWorldModel->mGoalDepth )
{
avoidGoalpostGoto.x = selfPos.x;
avoidGoalpostGoto.y = 2.0f;
}
}
Vector3f avoidBallPos = AvoidBall( avoidGoalpostGoto );
Vector3f resultVector = avoidBallPos;
Vector3f resultVectorLeft = avoidBallPos;
Vector3f resultVectorRight = avoidBallPos;
unsigned int computeTime = 0;
unsigned int leftComputeTime = 0;
unsigned int rightComputeTime = 0;
while( computeTime < 20 )
{
float selfDistToTar = selfPos.distXY2( resultVectorLeft );
Angle selfAngToTar = selfPos.angTheta2( resultVectorLeft );
if( IsOurPlayerObstacle( resultVectorLeft )
|| IsOppPlayerObstacle( resultVectorLeft )
)
{
Angle deltaAng = unitAngle + selfAngToTar;
Ray deltaRay = Ray::MakeRayFromPositionAndAngle( selfPos, deltaAng );
Vector3f deltaVec = deltaRay.GetPoint( selfDistToTar );
resultVectorLeft = deltaVec;
}
else
{
break;
}
computeTime ++;
leftComputeTime ++;
}
computeTime = 0;
while( computeTime < 20 )
{
float selfDistToTar = selfPos.distXY2( resultVectorRight );
Angle selfAngToTar = selfPos.angTheta2( resultVectorRight );
if( IsOurPlayerObstacle( resultVectorRight )
|| IsOppPlayerObstacle( resultVectorRight )
)
{
Angle deltaAng = -unitAngle + selfAngToTar;
Ray deltaRay = Ray::MakeRayFromPositionAndAngle( selfPos, deltaAng );
Vector3f deltaVec = deltaRay.GetPoint( selfDistToTar );
resultVectorRight = deltaVec;
}
else
{
break;
}
computeTime ++;
rightComputeTime ++;
}
OBSLog << "leftComputeTime " << leftComputeTime << " rightComputeTime " << rightComputeTime << endl;
if( leftComputeTime < rightComputeTime)
//if( selfPos.distXY2(resultVectorLeft) <= selfPos.distXY2(resultVectorRight) )
{
return resultVectorLeft;
}
else
{
return resultVectorRight;
}
}
bool Obstacle::IsBallObstacle( Vector3f tarGoto )
{
const float ballRadius = 0.14f;
Vector3f ballPos = mWorldModel->mBall.mPos;
return IsObstacle( tarGoto, ballPos, ballRadius );
}
Vector3f Obstacle::AvoidBall( Vector3f tarGoto )
{
const float avoidBallRadius = 0.25f;
Vector3f ballPos = mWorldModel->mBall.mPos;
Vector3f selfPos = mWorldModel->mSelf.mPos;
Vector3f oppGoalPos = mWorldModel->mOppGoalCenter;
if( !IsBallObstacle(tarGoto) )
{
return tarGoto;
}
if( tarGoto.x > ballPos.x )
{
return tarGoto;
}
Angle ballToOppGoalAng = ballPos.angTheta2( oppGoalPos );
Angle ballToOppGoalAngLeft = ballPos.angTheta2( oppGoalPos ) + 100.0f;
Angle ballToOppGoalAngRight = ballPos.angTheta2( oppGoalPos ) - 100.0f;
Ray avoidBallPosRayLeft = Ray::MakeRayFromPositionAndAngle( ballPos, ballToOppGoalAngLeft );
Ray avoidBallPosRayRight = Ray::MakeRayFromPositionAndAngle( ballPos, ballToOppGoalAngRight );
Vector3f avoidBallPosLeft = avoidBallPosRayLeft.GetPoint( avoidBallRadius );
Vector3f avoidBallPosRight = avoidBallPosRayRight.GetPoint( avoidBallRadius );
float selfDistToPosLeft = selfPos.distXY2( avoidBallPosLeft );
float selfDistToPosRight = selfPos.distXY2( avoidBallPosRight );
if( selfDistToPosLeft < selfDistToPosRight )
{
return avoidBallPosLeft;
}
else
{
return avoidBallPosRight;
}
}