-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathObstacle.cpp
355 lines (295 loc) · 12.6 KB
/
Obstacle.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
// ----------------------------------------------------------------------------
//
//
// OpenSteer -- Steering Behaviors for Autonomous Characters
//
// Copyright (c) 2002-2004, Sony Computer Entertainment America
// Original author: Craig Reynolds <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
//
// ----------------------------------------------------------------------------
//
//
// OpenSteer Obstacle classes
//
// 10-28-04 cwr: split off from Obstacle.h
//
//
// ----------------------------------------------------------------------------
#include "OpenSteer/Obstacle.h"
// ----------------------------------------------------------------------------
// Obstacle
// compute steering for a vehicle to avoid this obstacle, if needed
OpenSteer::Vec3
OpenSteer::Obstacle::steerToAvoid (const AbstractVehicle& vehicle,
const float minTimeToCollision) const
{
// find nearest intersection with this obstacle along vehicle's path
PathIntersection pi;
findIntersectionWithVehiclePath (vehicle, pi);
// return steering for vehicle to avoid intersection, or zero if non found
return pi.steerToAvoidIfNeeded (vehicle, minTimeToCollision);
}
// ----------------------------------------------------------------------------
// Obstacle
// static method to apply steerToAvoid to nearest obstacle in an ObstacleGroup
OpenSteer::Vec3
OpenSteer::Obstacle::
steerToAvoidObstacles (const AbstractVehicle& vehicle,
const float minTimeToCollision,
const ObstacleGroup& obstacles)
{
PathIntersection nearest, next;
// test all obstacles in group for an intersection with the vehicle's
// future path, select the one whose point of intersection is nearest
firstPathIntersectionWithObstacleGroup (vehicle, obstacles, nearest, next);
// if nearby intersection found, steer away from it, otherwise no steering
return nearest.steerToAvoidIfNeeded (vehicle, minTimeToCollision);
}
// ----------------------------------------------------------------------------
// Obstacle
// static method to find first vehicle path intersection in an ObstacleGroup
//
// returns its results in the PathIntersection argument "nearest",
// "next" is used to store internal state.
void
OpenSteer::Obstacle::
firstPathIntersectionWithObstacleGroup (const AbstractVehicle& vehicle,
const ObstacleGroup& obstacles,
PathIntersection& nearest,
PathIntersection& next)
{
// test all obstacles in group for an intersection with the vehicle's
// future path, select the one whose point of intersection is nearest
next.intersect = false;
nearest.intersect = false;
for (ObstacleIterator o = obstacles.begin(); o != obstacles.end(); o++)
{
// find nearest point (if any) where vehicle path intersects obstacle
// o, storing the results in PathIntersection object "next"
(**o).findIntersectionWithVehiclePath (vehicle, next);
// if this is the first intersection found, or it is the nearest found
// so far, store it in PathIntersection object "nearest"
const bool firstFound = !nearest.intersect;
const bool nearestFound = (next.intersect &&
(next.distance < nearest.distance));
if (firstFound || nearestFound) nearest = next;
}
}
// ----------------------------------------------------------------------------
// PathIntersection
// determine steering once path intersections have been found
OpenSteer::Vec3
OpenSteer::Obstacle::PathIntersection::
steerToAvoidIfNeeded (const AbstractVehicle& vehicle,
const float minTimeToCollision) const
{
// if nearby intersection found, steer away from it, otherwise no steering
const float minDistanceToCollision = minTimeToCollision * vehicle.speed();
if (intersect && (distance < minDistanceToCollision))
{
// compute avoidance steering force: take the component of
// steerHint which is lateral (perpendicular to vehicle's
// forward direction), set its length to vehicle's maxForce
Vec3 lateral = steerHint.perpendicularComponent (vehicle.forward ());
return lateral.normalize () * vehicle.maxForce ();
}
else
{
return Vec3::zero;
}
}
// ----------------------------------------------------------------------------
// SphereObstacle
// find first intersection of a vehicle's path with this obstacle
void
OpenSteer::
SphereObstacle::
findIntersectionWithVehiclePath (const AbstractVehicle& vehicle,
PathIntersection& pi) const
{
// This routine is based on the Paul Bourke's derivation in:
// Intersection of a Line and a Sphere (or circle)
// http://www.swin.edu.au/astronomy/pbourke/geometry/sphereline/
// But the computation is done in the vehicle's local space, so
// the line in question is the Z (Forward) axis of the space which
// simplifies some of the calculations.
float b, c, d, p, q, s;
Vec3 lc;
// initialize pathIntersection object to "no intersection found"
pi.intersect = false;
// find sphere's "local center" (lc) in the vehicle's coordinate space
lc = vehicle.localizePosition (center);
pi.vehicleOutside = lc.length () > radius;
// if obstacle is seen from inside, but vehicle is outside, must avoid
// (noticed once a vehicle got outside it ignored the obstacle 2008-5-20)
if (pi.vehicleOutside && (seenFrom () == inside))
{
pi.intersect = true;
pi.distance = 0.0f;
pi.steerHint = (center - vehicle.position()).normalize();
return;
}
// compute line-sphere intersection parameters
const float r = radius + vehicle.radius();
b = -2 * lc.z;
c = square (lc.x) + square (lc.y) + square (lc.z) - square (r);
d = (b * b) - (4 * c);
// when the path does not intersect the sphere
if (d < 0) return;
// otherwise, the path intersects the sphere in two points with
// parametric coordinates of "p" and "q". (If "d" is zero the two
// points are coincident, the path is tangent)
s = sqrtXXX (d);
p = (-b + s) / 2;
q = (-b - s) / 2;
// both intersections are behind us, so no potential collisions
if ((p < 0) && (q < 0)) return;
// at least one intersection is in front, so intersects our forward
// path
pi.intersect = true;
pi.obstacle = this;
pi.distance =
((p > 0) && (q > 0)) ?
// both intersections are in front of us, find nearest one
((p < q) ? p : q) :
// otherwise one is ahead and one is behind: we are INSIDE obstacle
(seenFrom () == outside ?
// inside a solid obstacle, so distance to obstacle is zero
0.0f :
// hollow obstacle (or "both"), pick point that is in front
((p > 0) ? p : q));
pi.surfacePoint =
vehicle.position() + (vehicle.forward() * pi.distance);
pi.surfaceNormal = (pi.surfacePoint-center).normalize();
switch (seenFrom ())
{
case outside:
pi.steerHint = pi.surfaceNormal;
break;
case inside:
pi.steerHint = -pi.surfaceNormal;
break;
case both:
pi.steerHint = pi.surfaceNormal * (pi.vehicleOutside ? 1.0f : -1.0f);
break;
}
}
// ----------------------------------------------------------------------------
// BoxObstacle
// find first intersection of a vehicle's path with this obstacle
void
OpenSteer::
BoxObstacle::
findIntersectionWithVehiclePath (const AbstractVehicle& vehicle,
PathIntersection& pi) const
{
// abbreviations
const float w = width; // dimensions
const float h = height;
const float d = depth;
const Vec3 s = side (); // local space
const Vec3 u = up ();
const Vec3 f = forward ();
const Vec3 p = position ();
const Vec3 hw = s * (0.5f * width); // offsets for face centers
const Vec3 hh = u * (0.5f * height);
const Vec3 hd = f * (0.5f * depth);
const seenFromState sf = seenFrom ();
// the box's six rectangular faces
RectangleObstacle r1 (w, h, s, u, f, p + hd, sf); // front
RectangleObstacle r2 (w, h, -s, u, -f, p - hd, sf); // back
RectangleObstacle r3 (d, h, -f, u, s, p + hw, sf); // side
RectangleObstacle r4 (d, h, f, u, -s, p - hw, sf); // other side
RectangleObstacle r5 (w, d, s, -f, u, p + hh, sf); // top
RectangleObstacle r6 (w, d, -s, -f, -u, p - hh, sf); // bottom
// group the six RectangleObstacle faces together
ObstacleGroup faces;
faces.push_back (&r1);
faces.push_back (&r2);
faces.push_back (&r3);
faces.push_back (&r4);
faces.push_back (&r5);
faces.push_back (&r6);
// find first intersection of vehicle path with group of six faces
PathIntersection next;
firstPathIntersectionWithObstacleGroup (vehicle, faces, pi, next);
// when intersection found, adjust PathIntersection for the box case
if (pi.intersect)
{
pi.obstacle = this;
pi.steerHint = ((pi.surfacePoint - position ()).normalize () *
(pi.vehicleOutside ? 1.0f : -1.0f));
}
}
// ----------------------------------------------------------------------------
// PlaneObstacle
// find first intersection of a vehicle's path with this obstacle
void
OpenSteer::
PlaneObstacle::
findIntersectionWithVehiclePath (const AbstractVehicle& vehicle,
PathIntersection& pi) const
{
// initialize pathIntersection object to "no intersection found"
pi.intersect = false;
const Vec3 lp = localizePosition (vehicle.position ());
const Vec3 ld = localizeDirection (vehicle.forward ());
// no obstacle intersection if path is parallel to XY (side/up) plane
if (ld.dot (Vec3::forward) == 0.0f) return;
// no obstacle intersection if vehicle is heading away from the XY plane
if ((lp.z > 0.0f) && (ld.z > 0.0f)) return;
if ((lp.z < 0.0f) && (ld.z < 0.0f)) return;
// no obstacle intersection if obstacle "not seen" from vehicle's side
if ((seenFrom () == outside) && (lp.z < 0.0f)) return;
if ((seenFrom () == inside) && (lp.z > 0.0f)) return;
// find intersection of path with rectangle's plane (XY plane)
const float ix = lp.x - (ld.x * lp.z / ld.z);
const float iy = lp.y - (ld.y * lp.z / ld.z);
const Vec3 planeIntersection (ix, iy, 0.0f);
// no obstacle intersection if plane intersection is outside 2d shape
if (!xyPointInsideShape (planeIntersection, vehicle.radius ())) return;
// otherwise, the vehicle path DOES intersect this rectangle
const Vec3 localXYradial = planeIntersection.normalize ();
const Vec3 radial = globalizeDirection (localXYradial);
const float sideSign = (lp.z > 0.0f) ? +1.0f : -1.0f;
const Vec3 opposingNormal = forward () * sideSign;
pi.intersect = true;
pi.obstacle = this;
pi.distance = (lp - planeIntersection).length ();
pi.steerHint = opposingNormal + radial; // should have "toward edge" term?
pi.surfacePoint = globalizePosition (planeIntersection);
pi.surfaceNormal = opposingNormal;
pi.vehicleOutside = lp.z > 0.0f;
}
// ----------------------------------------------------------------------------
// RectangleObstacle
// determines if a given point on XY plane is inside obstacle shape
bool
OpenSteer::
RectangleObstacle::
xyPointInsideShape (const Vec3& point, float radius) const
{
const float w = radius + (width * 0.5f);
const float h = radius + (height * 0.5f);
return !((point.x > w) || (point.x < -w) || (point.y > h) || (point.y < -h));
}
// ----------------------------------------------------------------------------