/
SteerLibrary.h
1076 lines (857 loc) · 36.4 KB
/
SteerLibrary.h
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ----------------------------------------------------------------------------
//
//
// OpenSteer -- Steering Behaviors for Autonomous Characters
//
// Copyright (c) 2002-2005, Sony Computer Entertainment America
// Original author: Craig Reynolds <craig_reynolds@playstation.sony.com>
//
// 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.
//
//
// ----------------------------------------------------------------------------
//
//
// SteerLibraryMixin
//
// This mixin (class with templated superclass) adds the "steering library"
// functionality to a given base class. SteerLibraryMixin assumes its base
// class supports the AbstractVehicle interface.
//
// 10-04-04 bk: put everything into the OpenSteer namespace
// 02-06-03 cwr: create mixin (from "SteerMass")
// 06-03-02 cwr: removed TS dependencies
// 11-21-01 cwr: created
//
//
// ----------------------------------------------------------------------------
#ifndef OPENSTEER_STEERLIBRARY_H
#define OPENSTEER_STEERLIBRARY_H
#include "OpenSteer/AbstractVehicle.h"
#include "OpenSteer/Pathway.h"
#include "OpenSteer/Obstacle.h"
#include "OpenSteer/Utilities.h"
// Include OpenSteer::Color, OpenSteer::gBlack, ...
#include "Color.h"
namespace OpenSteer {
// ----------------------------------------------------------------------------
template <class Super>
class SteerLibraryMixin : public Super
{
public:
using Super::velocity;
using Super::maxSpeed;
using Super::speed;
using Super::radius;
using Super::maxForce;
using Super::forward;
using Super::position;
using Super::side;
using Super::up;
using Super::predictFuturePosition;
public:
// Constructor: initializes state
SteerLibraryMixin ()
{
// set inital state
reset ();
}
// reset state
void reset (void)
{
// initial state of wander behavior
WanderSide = 0;
WanderUp = 0;
// default to non-gaudyPursuitAnnotation
gaudyPursuitAnnotation = false;
}
// -------------------------------------------------- steering behaviors
// Wander behavior
float WanderSide;
float WanderUp;
Vec3 steerForWander (float dt);
// Seek behavior
Vec3 steerForSeek (const Vec3& target);
// Flee behavior
Vec3 steerForFlee (const Vec3& target);
// xxx proposed, experimental new seek/flee [cwr 9-16-02]
Vec3 xxxsteerForFlee (const Vec3& target);
Vec3 xxxsteerForSeek (const Vec3& target);
// Path Following behaviors
Vec3 steerToFollowPath (const int direction,
const float predictionTime,
Pathway& path);
Vec3 steerToStayOnPath (const float predictionTime, Pathway& path);
// ------------------------------------------------------------------------
// Obstacle Avoidance behavior
//
// Returns a steering force to avoid a given obstacle. The purely
// lateral steering force will turn our vehicle towards a silhouette edge
// of the obstacle. Avoidance is required when (1) the obstacle
// intersects the vehicle's current path, (2) it is in front of the
// vehicle, and (3) is within minTimeToCollision seconds of travel at the
// vehicle's current velocity. Returns a zero vector value (Vec3::zero)
// when no avoidance is required.
Vec3 steerToAvoidObstacle (const float minTimeToCollision,
const Obstacle& obstacle);
// avoids all obstacles in an ObstacleGroup
Vec3 steerToAvoidObstacles (const float minTimeToCollision,
const ObstacleGroup& obstacles);
// ------------------------------------------------------------------------
// Unaligned collision avoidance behavior: avoid colliding with other
// nearby vehicles moving in unconstrained directions. Determine which
// (if any) other other vehicle we would collide with first, then steers
// to avoid the site of that potential collision. Returns a steering
// force vector, which is zero length if there is no impending collision.
Vec3 steerToAvoidNeighbors (const float minTimeToCollision,
const AVGroup& others);
// Given two vehicles, based on their current positions and velocities,
// determine the time until nearest approach
float predictNearestApproachTime (AbstractVehicle& otherVehicle);
// Given the time until nearest approach (predictNearestApproachTime)
// determine position of each vehicle at that time, and the distance
// between them
float computeNearestApproachPositions (AbstractVehicle& otherVehicle,
float time);
/// XXX globals only for the sake of graphical annotation
Vec3 hisPositionAtNearestApproach;
Vec3 ourPositionAtNearestApproach;
// ------------------------------------------------------------------------
// avoidance of "close neighbors" -- used only by steerToAvoidNeighbors
//
// XXX Does a hard steer away from any other agent who comes withing a
// XXX critical distance. Ideally this should be replaced with a call
// XXX to steerForSeparation.
Vec3 steerToAvoidCloseNeighbors (const float minSeparationDistance,
const AVGroup& others);
// ------------------------------------------------------------------------
// used by boid behaviors
bool inBoidNeighborhood (const AbstractVehicle& otherVehicle,
const float minDistance,
const float maxDistance,
const float cosMaxAngle);
// ------------------------------------------------------------------------
// Separation behavior -- determines the direction away from nearby boids
Vec3 steerForSeparation (const float maxDistance,
const float cosMaxAngle,
const AVGroup& flock);
// ------------------------------------------------------------------------
// Alignment behavior
Vec3 steerForAlignment (const float maxDistance,
const float cosMaxAngle,
const AVGroup& flock);
// ------------------------------------------------------------------------
// Cohesion behavior
Vec3 steerForCohesion (const float maxDistance,
const float cosMaxAngle,
const AVGroup& flock);
// ------------------------------------------------------------------------
// pursuit of another vehicle (& version with ceiling on prediction time)
Vec3 steerForPursuit (const AbstractVehicle& quarry);
Vec3 steerForPursuit (const AbstractVehicle& quarry,
const float maxPredictionTime);
// for annotation
bool gaudyPursuitAnnotation;
// ------------------------------------------------------------------------
// evasion of another vehicle
Vec3 steerForEvasion (const AbstractVehicle& menace,
const float maxPredictionTime);
// ------------------------------------------------------------------------
// tries to maintain a given speed, returns a maxForce-clipped steering
// force along the forward/backward axis
Vec3 steerForTargetSpeed (const float targetSpeed);
// ----------------------------------------------------------- utilities
// XXX these belong somewhere besides the steering library
// XXX above AbstractVehicle, below SimpleVehicle
// XXX ("utility vehicle"?)
// xxx cwr experimental 9-9-02 -- names OK?
bool isAhead (const Vec3& target) const {return isAhead (target, 0.707f);};
bool isAside (const Vec3& target) const {return isAside (target, 0.707f);};
bool isBehind (const Vec3& target) const {return isBehind (target, -0.707f);};
bool isAhead (const Vec3& target, float cosThreshold) const
{
const Vec3 targetDirection = (target - position ()).normalize ();
return forward().dot(targetDirection) > cosThreshold;
};
bool isAside (const Vec3& target, float cosThreshold) const
{
const Vec3 targetDirection = (target - position ()).normalize ();
const float dp = forward().dot(targetDirection);
return (dp < cosThreshold) && (dp > -cosThreshold);
};
bool isBehind (const Vec3& target, float cosThreshold) const
{
const Vec3 targetDirection = (target - position()).normalize ();
return forward().dot(targetDirection) < cosThreshold;
};
// ------------------------------------------------ graphical annotation
// (parameter names commented out to prevent compiler warning from "-W")
// called when steerToAvoidObstacles decides steering is required
// (default action is to do nothing, layered classes can overload it)
virtual void annotateAvoidObstacle (const float /*minDistanceToCollision*/)
{
}
// called when steerToFollowPath decides steering is required
// (default action is to do nothing, layered classes can overload it)
virtual void annotatePathFollowing (const Vec3& /*future*/,
const Vec3& /*onPath*/,
const Vec3& /*target*/,
const float /*outside*/)
{
}
// called when steerToAvoidCloseNeighbors decides steering is required
// (default action is to do nothing, layered classes can overload it)
virtual void annotateAvoidCloseNeighbor (const AbstractVehicle& /*other*/,
const float /*additionalDistance*/)
{
}
// called when steerToAvoidNeighbors decides steering is required
// (default action is to do nothing, layered classes can overload it)
virtual void annotateAvoidNeighbor (const AbstractVehicle& /*threat*/,
const float /*steer*/,
const Vec3& /*ourFuture*/,
const Vec3& /*threatFuture*/)
{
}
};
} // namespace OpenSteer
// ----------------------------------------------------------------------------
template<class Super>
OpenSteer::Vec3
OpenSteer::SteerLibraryMixin<Super>::
steerForWander (float dt)
{
// random walk WanderSide and WanderUp between -1 and +1
const float speed = 12.0f * dt; // maybe this (12) should be an argument?
WanderSide = scalarRandomWalk (WanderSide, speed, -1, +1);
WanderUp = scalarRandomWalk (WanderUp, speed, -1, +1);
// return a pure lateral steering vector: (+/-Side) + (+/-Up)
return (side() * WanderSide) + (up() * WanderUp);
}
// ----------------------------------------------------------------------------
// Seek behavior
template<class Super>
OpenSteer::Vec3
OpenSteer::SteerLibraryMixin<Super>::
steerForSeek (const Vec3& target)
{
const Vec3 desiredVelocity = target - position();
return desiredVelocity - velocity();
}
// ----------------------------------------------------------------------------
// Flee behavior
template<class Super>
OpenSteer::Vec3
OpenSteer::SteerLibraryMixin<Super>::
steerForFlee (const Vec3& target)
{
const Vec3 desiredVelocity = position - target;
return desiredVelocity - velocity();
}
// ----------------------------------------------------------------------------
// xxx proposed, experimental new seek/flee [cwr 9-16-02]
template<class Super>
OpenSteer::Vec3
OpenSteer::SteerLibraryMixin<Super>::
xxxsteerForFlee (const Vec3& target)
{
// const Vec3 offset = position - target;
const Vec3 offset = position() - target;
const Vec3 desiredVelocity = offset.truncateLength (maxSpeed ()); //xxxnew
return desiredVelocity - velocity();
}
template<class Super>
OpenSteer::Vec3
OpenSteer::SteerLibraryMixin<Super>::
xxxsteerForSeek (const Vec3& target)
{
// const Vec3 offset = target - position;
const Vec3 offset = target - position();
const Vec3 desiredVelocity = offset.truncateLength (maxSpeed ()); //xxxnew
return desiredVelocity - velocity();
}
// ----------------------------------------------------------------------------
// Path Following behaviors
template<class Super>
OpenSteer::Vec3
OpenSteer::SteerLibraryMixin<Super>::
steerToStayOnPath (const float predictionTime, Pathway& path)
{
// predict our future position
const Vec3 futurePosition = predictFuturePosition (predictionTime);
// find the point on the path nearest the predicted future position
Vec3 tangent;
float outside;
const Vec3 onPath = path.mapPointToPath (futurePosition,
tangent, // output argument
outside); // output argument
if (outside < 0)
{
// our predicted future position was in the path,
// return zero steering.
return Vec3::zero;
}
else
{
// our predicted future position was outside the path, need to
// steer towards it. Use onPath projection of futurePosition
// as seek target
annotatePathFollowing (futurePosition, onPath, onPath, outside);
return steerForSeek (onPath);
}
}
template<class Super>
OpenSteer::Vec3
OpenSteer::SteerLibraryMixin<Super>::
steerToFollowPath (const int direction,
const float predictionTime,
Pathway& path)
{
// our goal will be offset from our path distance by this amount
const float pathDistanceOffset = direction * predictionTime * speed();
// predict our future position
const Vec3 futurePosition = predictFuturePosition (predictionTime);
// measure distance along path of our current and predicted positions
const float nowPathDistance =
path.mapPointToPathDistance (position ());
const float futurePathDistance =
path.mapPointToPathDistance (futurePosition);
// are we facing in the correction direction?
const bool rightway = ((pathDistanceOffset > 0) ?
(nowPathDistance < futurePathDistance) :
(nowPathDistance > futurePathDistance));
// find the point on the path nearest the predicted future position
// XXX need to improve calling sequence, maybe change to return a
// XXX special path-defined object which includes two Vec3s and a
// XXX bool (onPath,tangent (ignored), withinPath)
Vec3 tangent;
float outside;
const Vec3 onPath = path.mapPointToPath (futurePosition,
// output arguments:
tangent,
outside);
// no steering is required if (a) our future position is inside
// the path tube and (b) we are facing in the correct direction
if ((outside < 0) && rightway)
{
// all is well, return zero steering
return Vec3::zero;
}
else
{
// otherwise we need to steer towards a target point obtained
// by adding pathDistanceOffset to our current path position
float const targetPathDistance = nowPathDistance + pathDistanceOffset;
Vec3 const target = path.mapPathDistanceToPoint (targetPathDistance);
annotatePathFollowing (futurePosition, onPath, target, outside);
// return steering to seek target on path
return steerForSeek (target);
}
}
// ----------------------------------------------------------------------------
// Obstacle Avoidance behavior
//
// Returns a steering force to avoid a given obstacle. The purely lateral
// steering force will turn our vehicle towards a silhouette edge of the
// obstacle. Avoidance is required when (1) the obstacle intersects the
// vehicle's current path, (2) it is in front of the vehicle, and (3) is
// within minTimeToCollision seconds of travel at the vehicle's current
// velocity. Returns a zero vector value (Vec3::zero) when no avoidance is
// required.
//
// XXX The current (4-23-03) scheme is to dump all the work on the various
// XXX Obstacle classes, making them provide a "steer vehicle to avoid me"
// XXX method. This may well change.
//
// XXX 9-12-03: this routine is probably obsolete: its name is too close to
// XXX the new steerToAvoidObstacles and the arguments are reversed
// XXX (perhaps there should be another version of steerToAvoidObstacles
// XXX whose second arg is "const Obstacle& obstacle" just in case we want
// XXX to avoid a non-grouped obstacle)
template<class Super>
OpenSteer::Vec3
OpenSteer::SteerLibraryMixin<Super>::
steerToAvoidObstacle (const float minTimeToCollision,
const Obstacle& obstacle)
{
const Vec3 avoidance = obstacle.steerToAvoid (*this, minTimeToCollision);
// XXX more annotation modularity problems (assumes spherical obstacle)
if (avoidance != Vec3::zero)
annotateAvoidObstacle (minTimeToCollision * speed());
return avoidance;
}
// this version avoids all of the obstacles in an ObstacleGroup
template<class Super>
OpenSteer::Vec3
OpenSteer::SteerLibraryMixin<Super>::
steerToAvoidObstacles (const float minTimeToCollision,
const ObstacleGroup& obstacles)
{
const Vec3 avoidance = Obstacle::steerToAvoidObstacles (*this,
minTimeToCollision,
obstacles);
// XXX more annotation modularity problems (assumes spherical obstacle)
if (avoidance != Vec3::zero)
annotateAvoidObstacle (minTimeToCollision * speed());
return avoidance;
}
// ----------------------------------------------------------------------------
// Unaligned collision avoidance behavior: avoid colliding with other nearby
// vehicles moving in unconstrained directions. Determine which (if any)
// other other vehicle we would collide with first, then steers to avoid the
// site of that potential collision. Returns a steering force vector, which
// is zero length if there is no impending collision.
template<class Super>
OpenSteer::Vec3
OpenSteer::SteerLibraryMixin<Super>::
steerToAvoidNeighbors (const float minTimeToCollision,
const AVGroup& others)
{
// first priority is to prevent immediate interpenetration
const Vec3 separation = steerToAvoidCloseNeighbors (0, others);
if (separation != Vec3::zero) return separation;
// otherwise, go on to consider potential future collisions
float steer = 0;
AbstractVehicle* threat = NULL;
// Time (in seconds) until the most immediate collision threat found
// so far. Initial value is a threshold: don't look more than this
// many frames into the future.
float minTime = minTimeToCollision;
// xxx solely for annotation
Vec3 xxxThreatPositionAtNearestApproach;
Vec3 xxxOurPositionAtNearestApproach;
// for each of the other vehicles, determine which (if any)
// pose the most immediate threat of collision.
for (AVIterator i = others.begin(); i != others.end(); i++)
{
AbstractVehicle& other = **i;
if (&other != this)
{
// avoid when future positions are this close (or less)
const float collisionDangerThreshold = radius() * 2;
// predicted time until nearest approach of "this" and "other"
const float time = predictNearestApproachTime (other);
// If the time is in the future, sooner than any other
// threatened collision...
if ((time >= 0) && (time < minTime))
{
// if the two will be close enough to collide,
// make a note of it
if (computeNearestApproachPositions (other, time)
< collisionDangerThreshold)
{
minTime = time;
threat = &other;
xxxThreatPositionAtNearestApproach
= hisPositionAtNearestApproach;
xxxOurPositionAtNearestApproach
= ourPositionAtNearestApproach;
}
}
}
}
// if a potential collision was found, compute steering to avoid
if (threat != NULL)
{
// parallel: +1, perpendicular: 0, anti-parallel: -1
float parallelness = forward().dot(threat->forward());
float angle = 0.707f;
if (parallelness < -angle)
{
// anti-parallel "head on" paths:
// steer away from future threat position
Vec3 offset = xxxThreatPositionAtNearestApproach - position();
float sideDot = offset.dot(side());
steer = (sideDot > 0) ? -1.0f : 1.0f;
}
else
{
if (parallelness > angle)
{
// parallel paths: steer away from threat
Vec3 offset = threat->position() - position();
float sideDot = offset.dot(side());
steer = (sideDot > 0) ? -1.0f : 1.0f;
}
else
{
// perpendicular paths: steer behind threat
// (only the slower of the two does this)
if (threat->speed() <= speed())
{
float sideDot = side().dot(threat->velocity());
steer = (sideDot > 0) ? -1.0f : 1.0f;
}
}
}
annotateAvoidNeighbor (*threat,
steer,
xxxOurPositionAtNearestApproach,
xxxThreatPositionAtNearestApproach);
}
return side() * steer;
}
// Given two vehicles, based on their current positions and velocities,
// determine the time until nearest approach
//
// XXX should this return zero if they are already in contact?
template<class Super>
float
OpenSteer::SteerLibraryMixin<Super>::
predictNearestApproachTime (AbstractVehicle& otherVehicle)
{
// imagine we are at the origin with no velocity,
// compute the relative velocity of the other vehicle
const Vec3 myVelocity = velocity();
const Vec3 otherVelocity = otherVehicle.velocity();
const Vec3 relVelocity = otherVelocity - myVelocity;
const float relSpeed = relVelocity.length();
// for parallel paths, the vehicles will always be at the same distance,
// so return 0 (aka "now") since "there is no time like the present"
if (relSpeed == 0) return 0;
// Now consider the path of the other vehicle in this relative
// space, a line defined by the relative position and velocity.
// The distance from the origin (our vehicle) to that line is
// the nearest approach.
// Take the unit tangent along the other vehicle's path
const Vec3 relTangent = relVelocity / relSpeed;
// find distance from its path to origin (compute offset from
// other to us, find length of projection onto path)
const Vec3 relPosition = position() - otherVehicle.position();
const float projection = relTangent.dot(relPosition);
return projection / relSpeed;
}
// Given the time until nearest approach (predictNearestApproachTime)
// determine position of each vehicle at that time, and the distance
// between them
template<class Super>
float
OpenSteer::SteerLibraryMixin<Super>::
computeNearestApproachPositions (AbstractVehicle& otherVehicle,
float time)
{
const Vec3 myTravel = forward () * speed () * time;
const Vec3 otherTravel = otherVehicle.forward () * otherVehicle.speed () * time;
const Vec3 myFinal = position () + myTravel;
const Vec3 otherFinal = otherVehicle.position () + otherTravel;
// xxx for annotation
ourPositionAtNearestApproach = myFinal;
hisPositionAtNearestApproach = otherFinal;
return Vec3::distance (myFinal, otherFinal);
}
// ----------------------------------------------------------------------------
// avoidance of "close neighbors" -- used only by steerToAvoidNeighbors
//
// XXX Does a hard steer away from any other agent who comes withing a
// XXX critical distance. Ideally this should be replaced with a call
// XXX to steerForSeparation.
template<class Super>
OpenSteer::Vec3
OpenSteer::SteerLibraryMixin<Super>::
steerToAvoidCloseNeighbors (const float minSeparationDistance,
const AVGroup& others)
{
// for each of the other vehicles...
for (AVIterator i = others.begin(); i != others.end(); i++)
{
AbstractVehicle& other = **i;
if (&other != this)
{
const float sumOfRadii = radius() + other.radius();
const float minCenterToCenter = minSeparationDistance + sumOfRadii;
const Vec3 offset = other.position() - position();
const float currentDistance = offset.length();
if (currentDistance < minCenterToCenter)
{
annotateAvoidCloseNeighbor (other, minSeparationDistance);
return (-offset).perpendicularComponent (forward());
}
}
}
// otherwise return zero
return Vec3::zero;
}
// ----------------------------------------------------------------------------
// used by boid behaviors: is a given vehicle within this boid's neighborhood?
template<class Super>
bool
OpenSteer::SteerLibraryMixin<Super>::
inBoidNeighborhood (const AbstractVehicle& otherVehicle,
const float minDistance,
const float maxDistance,
const float cosMaxAngle)
{
if (&otherVehicle == this)
{
return false;
}
else
{
const Vec3 offset = otherVehicle.position() - position();
const float distanceSquared = offset.lengthSquared ();
// definitely in neighborhood if inside minDistance sphere
if (distanceSquared < (minDistance * minDistance))
{
return true;
}
else
{
// definitely not in neighborhood if outside maxDistance sphere
if (distanceSquared > (maxDistance * maxDistance))
{
return false;
}
else
{
// otherwise, test angular offset from forward axis
const Vec3 unitOffset = offset / sqrt (distanceSquared);
const float forwardness = forward().dot (unitOffset);
return forwardness > cosMaxAngle;
}
}
}
}
// ----------------------------------------------------------------------------
// Separation behavior: steer away from neighbors
template<class Super>
OpenSteer::Vec3
OpenSteer::SteerLibraryMixin<Super>::
steerForSeparation (const float maxDistance,
const float cosMaxAngle,
const AVGroup& flock)
{
// steering accumulator and count of neighbors, both initially zero
Vec3 steering;
int neighbors = 0;
// for each of the other vehicles...
AVIterator flockEndIter = flock.end();
for (AVIterator otherVehicle = flock.begin(); otherVehicle != flockEndIter; ++otherVehicle )
{
if (inBoidNeighborhood (**otherVehicle, radius()*3, maxDistance, cosMaxAngle))
{
// add in steering contribution
// (opposite of the offset direction, divided once by distance
// to normalize, divided another time to get 1/d falloff)
const Vec3 offset = (**otherVehicle).position() - position();
const float distanceSquared = offset.dot(offset);
steering += (offset / -distanceSquared);
// count neighbors
++neighbors;
}
}
// divide by neighbors, then normalize to pure direction
// bk: Why dividing if you normalize afterwards?
// As long as normilization tests for @c 0 we can just call normalize
// and safe the branching if.
/*
if (neighbors > 0) {
steering /= neighbors;
steering = steering.normalize();
}
*/
steering = steering.normalize();
return steering;
}
// ----------------------------------------------------------------------------
// Alignment behavior: steer to head in same direction as neighbors
template<class Super>
OpenSteer::Vec3
OpenSteer::SteerLibraryMixin<Super>::
steerForAlignment (const float maxDistance,
const float cosMaxAngle,
const AVGroup& flock)
{
// steering accumulator and count of neighbors, both initially zero
Vec3 steering;
int neighbors = 0;
// for each of the other vehicles...
for (AVIterator otherVehicle = flock.begin(); otherVehicle != flock.end(); otherVehicle++)
{
if (inBoidNeighborhood (**otherVehicle, radius()*3, maxDistance, cosMaxAngle))
{
// accumulate sum of neighbor's heading
steering += (**otherVehicle).forward();
// count neighbors
neighbors++;
}
}
// divide by neighbors, subtract off current heading to get error-
// correcting direction, then normalize to pure direction
if (neighbors > 0) steering = ((steering / (float)neighbors) - forward()).normalize();
return steering;
}
// ----------------------------------------------------------------------------
// Cohesion behavior: to to move toward center of neighbors
template<class Super>
OpenSteer::Vec3
OpenSteer::SteerLibraryMixin<Super>::
steerForCohesion (const float maxDistance,
const float cosMaxAngle,
const AVGroup& flock)
{
// steering accumulator and count of neighbors, both initially zero
Vec3 steering;
int neighbors = 0;
// for each of the other vehicles...
for (AVIterator otherVehicle = flock.begin(); otherVehicle != flock.end(); otherVehicle++)
{
if (inBoidNeighborhood (**otherVehicle, radius()*3, maxDistance, cosMaxAngle))
{
// accumulate sum of neighbor's positions
steering += (**otherVehicle).position();
// count neighbors
neighbors++;
}
}
// divide by neighbors, subtract off current position to get error-
// correcting direction, then normalize to pure direction
if (neighbors > 0) steering = ((steering / (float)neighbors) - position()).normalize();
return steering;
}
// ----------------------------------------------------------------------------
// pursuit of another vehicle (& version with ceiling on prediction time)
template<class Super>
OpenSteer::Vec3
OpenSteer::SteerLibraryMixin<Super>::
steerForPursuit (const AbstractVehicle& quarry)
{
return steerForPursuit (quarry, FLT_MAX);
}
template<class Super>
OpenSteer::Vec3
OpenSteer::SteerLibraryMixin<Super>::
steerForPursuit (const AbstractVehicle& quarry,
const float maxPredictionTime)
{
// offset from this to quarry, that distance, unit vector toward quarry
const Vec3 offset = quarry.position() - position();
const float distance = offset.length ();
const Vec3 unitOffset = offset / distance;
// how parallel are the paths of "this" and the quarry
// (1 means parallel, 0 is pependicular, -1 is anti-parallel)
const float parallelness = forward().dot (quarry.forward());
// how "forward" is the direction to the quarry
// (1 means dead ahead, 0 is directly to the side, -1 is straight back)
const float forwardness = forward().dot (unitOffset);
const float directTravelTime = distance / speed ();
const int f = intervalComparison (forwardness, -0.707f, 0.707f);
const int p = intervalComparison (parallelness, -0.707f, 0.707f);
float timeFactor = 0; // to be filled in below
Color color; // to be filled in below (xxx just for debugging)
// Break the pursuit into nine cases, the cross product of the
// quarry being [ahead, aside, or behind] us and heading
// [parallel, perpendicular, or anti-parallel] to us.
switch (f)
{
case +1:
switch (p)
{
case +1: // ahead, parallel
timeFactor = 4;
color = gBlack;
break;
case 0: // ahead, perpendicular
timeFactor = 1.8f;
color = gGray50;
break;
case -1: // ahead, anti-parallel
timeFactor = 0.85f;
color = gWhite;
break;
}
break;
case 0:
switch (p)
{
case +1: // aside, parallel
timeFactor = 1;
color = gRed;
break;
case 0: // aside, perpendicular
timeFactor = 0.8f;
color = gYellow;
break;
case -1: // aside, anti-parallel
timeFactor = 4;
color = gGreen;
break;
}
break;
case -1:
switch (p)
{