/
steerpipe.cpp
285 lines (246 loc) · 6.84 KB
/
steerpipe.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
/*
* Defines the pipeline steering system.
*
* Part of the Artificial Intelligence for Games system.
*
* Copyright (c) Ian Millington 2003-2006. All Rights Reserved.
*
* This software is distributed under licence. Use of this software
* implies agreement with all terms and conditions of the accompanying
* software licence.
*/
#include <stdlib.h>
#include <aicore/aicore.h>
#include <cassert>
namespace aicore
{
Goal::Goal()
{
clear();
}
void Goal::clear()
{
positionSet = orientationSet = velocitySet = rotationSet = false;
}
void Goal::updateGoal(const Goal& goal)
{
// Make sure we can merge
assert(canMergeGoals(goal));
if (goal.positionSet)
{
position = goal.position;
positionSet = true;
}
if (goal.orientationSet)
{
orientation = goal.orientation;
orientationSet = true;
}
if (goal.velocitySet)
{
velocity = goal.velocity;
velocitySet = true;
}
if (goal.rotationSet)
{
rotation = goal.rotation;
rotationSet = true;
}
}
bool Goal::canMergeGoals(const Goal& goal) const
{
return !(
positionSet && goal.positionSet ||
orientationSet && goal.orientationSet ||
velocitySet && goal.velocitySet ||
rotationSet && goal.rotationSet
);
}
real Path::getMaxPriority()
{
return (character->position - goal.position).magnitude();
}
SteeringPipe::SteeringPipe()
:
fallback(0),
constraintSteps(100),
path(0)
{
}
SteeringPipe::~SteeringPipe()
{
if (path) delete path;
}
void SteeringPipe::setActuator(aicore::Actuator *a)
{
actuator = a;
if (path) delete path;
path = 0;
}
void SteeringPipe::getSteering(SteeringOutput* output)
{
Goal goal;
std::list<Targeter*>::iterator ti;
for (ti = targeters.begin(); ti != targeters.end(); ti++)
{
Goal targeterResult = (*ti)->getGoal();
if (goal.canMergeGoals(targeterResult))
{
goal.updateGoal(targeterResult);
}
}
std::list<Decomposer*>::iterator di;
for (di = decomposers.begin(); di != decomposers.end(); di++)
{
goal = (*di)->decomposeGoal(goal);
}
// Create an ampty path object of the correct type.
if (!path) path = actuator->createPathObject();
std::list<Constraint*>::iterator ci;
real shortestViolation, currentViolation, maxViolation;
Constraint *violatingConstraint;
for (unsigned i = 0; i < constraintSteps; i++)
{
// Find the path to this goal
actuator->getPath(path, goal);
// Find the constraint that is violated first
maxViolation = shortestViolation = path->getMaxPriority();
for (ci = constraints.begin(); ci != constraints.end(); ci++)
{
// Clear the flags that indicate the constraints used
if (i == 0) (*ci)->suggestionUsed = false;
// Check to see if this constraint is violated early than any other.
currentViolation = (*ci)->willViolate(path, shortestViolation);
if (currentViolation > 0 && currentViolation < shortestViolation)
{
shortestViolation = currentViolation;
violatingConstraint = *ci;
}
}
// Check if we found a violation
if (shortestViolation < maxViolation)
{
// Update the goal and check constraints again.
goal = violatingConstraint->suggest(path);
violatingConstraint->suggestionUsed = true;
}
else
{
// We've found a solution - use it and return
actuator->getSteering(output, path);
return;
}
}
// We've run out of constraint iterations, so use the fallback
if (fallback) fallback->getSteering(output);
}
void SteeringPipe::registerComponents()
{
std::list<Targeter*>::iterator ti;
for (ti = targeters.begin(); ti != targeters.end(); ti++)
{
(*ti)->pipe = this;
}
std::list<Decomposer*>::iterator di;
for (di = decomposers.begin(); di != decomposers.end(); di++)
{
(*di)->pipe = this;
}
std::list<Constraint*>::iterator ci;
for (ci = constraints.begin(); ci != constraints.end(); ci++)
{
(*ci)->pipe = this;
}
actuator->pipe = this;
}
// --------------------------------------------------------------------------
// Basic implementations
Goal FixedGoalTargeter::getGoal()
{
return goal;
}
real AvoidSpheresConstraint::willViolate(const Path* path, real maxPriority)
{
real priority = REAL_MAX;
real thisPriority;
std::list<Sphere*>::iterator soi;
for (soi = obstacles.begin(); soi != obstacles.end(); soi++)
{
thisPriority = willViolate(path, priority, *(*soi));
if (thisPriority < priority) priority = thisPriority;
}
return priority;
}
real AvoidSpheresConstraint::willViolate(
const Path* path,
real maxPriority,
Sphere &obstacle
)
{
// Make sure we've got a positional goal
if (!path->goal.positionSet) return REAL_MAX;
// Alias the character object
const Kinematic *character = pipe->character;
// Work out where we're going
Vector3 direction = path->goal.position - character->position;
// Make sure we're moving
if (direction.squareMagnitude() > 0)
{
// Find the distance from the line we're moving along to the obstacle.
Vector3 movementNormal = direction.unit();
Vector3 characterToObstacle = obstacle.position - character->position;
real distanceSquared = characterToObstacle * movementNormal;
distanceSquared = characterToObstacle.squareMagnitude() -
distanceSquared*distanceSquared;
// Check for collision
real radius = obstacle.radius + avoidMargin;
if (distanceSquared < radius*radius)
{
// Find how far along our movement vector the closest pass is
real distanceToClosest = characterToObstacle * movementNormal;
// Make sure this isn't behind us and is closer than our lookahead.
if (distanceToClosest > 0 && distanceToClosest < maxPriority)
{
// Find the closest point
Vector3 closestPoint =
character->position + movementNormal*distanceToClosest;
// Find the point of avoidance
suggestion.position =
obstacle.position +
(closestPoint - obstacle.position).unit() *
(obstacle.radius + avoidMargin);
suggestion.positionSet = true;
return distanceToClosest;
}
}
}
return REAL_MAX;
}
Goal AvoidSpheresConstraint::suggest(const Path* path)
{
return suggestion;
}
Path* BasicActuator::createPathObject()
{
return new Path;
}
void BasicActuator::getPath(Path* path, const Goal& goal)
{
path->character = pipe->character;
path->goal = goal;
}
void BasicActuator::getSteering(SteeringOutput* output, const Path* path)
{
if (path->goal.positionSet)
{
seek.character = pipe->character;
seek.target = &path->goal.position;
seek.maxAcceleration = maxAcceleration;
seek.getSteering(output);
}
else
{
output->clear();
}
}
} // end of namespace