-
Notifications
You must be signed in to change notification settings - Fork 0
/
aisteering.cpp
executable file
·151 lines (126 loc) · 4.33 KB
/
aisteering.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
#include "mastrix.hpp"
AISteering::AISteering(
AIPlayer *owner,
Position *ownerPos)
{
bot = owner;
botPosition = ownerPos;
}
AISteering::~AISteering()
{
bot = 0;
botPosition = 0;
}
//this actually causes the ship to move to a waypoint
void AISteering::moveTo(const Position &destination)
{
turnTo(destination);
if (isFacing(destination))
bot->thrust(true);
}
//when you hit an intermediate goal, instantly change your heading and preserve
//your speed so that you don't have to stop (if the turn isn't too sharp)
//if the turn is too sharp, then you have to stop
void AISteering::faceNewGoal(const Position &destination)
{
const float angleDifference = (float)fabs(getClampedAngleRad(
getAngleDifference(destination)));
assert(angleDifference >= 0);
assert(angleDifference <= 2 * M_PI);
//angle difference is sufficiently small, so just change your direction
bool leftTurnWideEnough = (angleDifference >= 0 && angleDifference <= M_PI_2);
bool rightTurnWideEnough = (angleDifference <= (2 * M_PI) &&
angleDifference >= M_PI_2 + M_PI);
bool isTurnWideEnough = leftTurnWideEnough || rightTurnWideEnough;
if (isTurnWideEnough)
{
float oldXVel = botPosition->getX_vel();
float oldYVel = botPosition->getY_vel();
float oldSpeed = sqrtf(oldXVel * oldXVel + oldYVel * oldYVel);
botPosition->setR(getAngleFacingTowards(destination));
float newAngle = botPosition->getR();
//set the new speed based on the angle difference.
//A low angle difference means little speed loss, while a sharper turn
//(close to 90 degrees) means more speed loss
float newSpeed;
if (leftTurnWideEnough)
newSpeed = oldSpeed * (1 - (angleDifference / M_PI_2));
//newSpeed = oldSpeed * 0.2f;
else
{
//the angle difference normalized to the 0..90 degree range
float rightAngleDifference = (angleDifference - (M_PI_2 + M_PI));
newSpeed = oldSpeed * (1 - (rightAngleDifference / M_PI_2));
//newSpeed = oldSpeed * 0.2f;
}
float newXVel = newSpeed * cos(newAngle);
float newYVel = newSpeed * -sin(newAngle);
botPosition->setX_vel(newXVel);
botPosition->setY_vel(newYVel);
botPosition->setR_vel(0);
}
else //turn is too sharp, so slow down (stop)
{
botPosition->setX_vel(0);
botPosition->setY_vel(0);
botPosition->setR_vel(0);
bot->thrust(false);
bot->brake(false);
}
}
//returns the angle difference (in radians) between the direction
//that the bot is facing and the vector from the bot to the destination
//...(this does not apply any angle corrections!)
float AISteering::getAngleDifference(const Position &destination) const
{
float angle = botPosition->getR();
float correctAngle = getAngleFacingTowards(destination);
float angleDifference = angle - correctAngle;
return angleDifference;
}
//returns the angle that the bot should have if it were facing the destination
float AISteering::getAngleFacingTowards(const Position &destination) const
{
float deltaX = destination.getX() - botPosition->getX();
float deltaY = destination.getY() - botPosition->getY();
float angleFacingDest = atan2(-deltaY, deltaX);
return angleFacingDest;
}
//returns true if the bot is facing or close to facing the destination point
bool AISteering::isFacing(const Position &destination) const
{
return (fabs(getAngleDifference(destination)) < AIPlayer::angleErrorRange);
}
void AISteering::turnTo(const Position &destination)
{
float uncorrectedAngleDifference = getAngleDifference(destination);
float correctedAngleDifference = uncorrectedAngleDifference;
//angle corrections
while(correctedAngleDifference <= -M_PI)
correctedAngleDifference += 2*M_PI;
while(correctedAngleDifference >= M_PI)
correctedAngleDifference -= 2*M_PI;
//check the uncorrected angle to see if you're facing the target
//if not, then steer accordingly
if( abs(correctedAngleDifference) < getDt()*bot->type->getTurnSpeed() )
{
botPosition->setR(getHeading(*botPosition, destination));
bot->turnLeft(false);
bot->turnRight(false);
}
/* if(isFacing(destination))
{
bot->turnLeft(false);
bot->turnRight(false);
}*/
else if(correctedAngleDifference > 0)
{
bot->turnLeft(false);
bot->turnRight(true);
}
else
{
bot->turnLeft(true);
bot->turnRight(false);
}
}