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 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Brains.Framework.Utility;
using Brains.Framework.Grouping;
namespace Brains.Framework.Locomotion
{
/// <summary>
/// This class is currently W.I.P
/// </summary>
/// <remarks></remarks>
public class LocomtionSteering:LocomotionController
{
private static Random rand = new Random();
private Vector2 _wanderTarget;
public const float WanderRadius = 20;
public const float WanderDistance = 30;
public const float WanderJitter = 80;
public enum Deceleration
{
Fast,
Normal,
Slow
}
[Flags()]
public enum BehaviorType
{
None = 0x00000,
Seek = 0x00002,
Flee= 0x00004,
Arrive= 0x00008,
Wander = 0x00010,
Cohesion= 0x00020,
Seperation= 0x00040,
Alignment = 0x00080,
//ObstacleAvoidance= 0x00100,
//WallAvoidance = 0x00200,
//FollowPath = 0x00400,
Pursuit= 0x00800,
Evade = 0x01000,
//Interpose= 0x02000,
//Hide= 0x04000,
//Flock = 0x08000,
OffsetPursuit= 0x10000,
}
public void TurnAllOff()
{
_behaviorFlags = BehaviorType.None;
}
public void TurnOnArrive(Vector2 targetPosition)
{
TurnOn(BehaviorType.Arrive);
SeekPos = targetPosition;
}
public void TurnOffArrive()
{
TurnOff(BehaviorType.Arrive);
}
public void TurnOnSeek(Vector2 targetPosition)
{
SeekPos = targetPosition;
TurnOn(BehaviorType.Seek);
}
public void TurnOffSeek()
{
TurnOff(BehaviorType.Seek);
}
public void TurnOnWander()
{
TurnOn(BehaviorType.Wander);
}
public void TurnOffWander()
{
TurnOff(BehaviorType.Wander);
}
public void TurnOnSeperation()
{
TurnOn(BehaviorType.Seperation);
}
public void TurnOffSeperation()
{
TurnOff(BehaviorType.Seperation);
}
public void TurnOnCohesion()
{
TurnOn(BehaviorType.Cohesion);
}
public void TurnOffCohesion()
{
TurnOff(BehaviorType.Cohesion);
}
public void TurnOnAlignment()
{
TurnOn(BehaviorType.Alignment);
}
public void TurnOffAlignment()
{
TurnOff(BehaviorType.Alignment);
}
public void TurnOnOffsetPursuit(Vector2 seek)
{
}
public void TurnOffOffsetPursuit()
{
}
public void TurnOnPursuit(Vector2 seek)
{
}
public void TurnOffPursuit()
{
}
public void TurnOnFlee(Vector2 seek)
{
}
public void TurnOffFlee()
{
}
private Vector2 _steeringForce;
private float ObstacleAvoidanceWeight=10;
private Vector2 SteeringForce = Vector2.Zero;
private Vector2 Heading;
private float MaxForce = 2;
private Vector2 Velocity;
private BehaviorType _behaviorFlags;
public float SeekWeight = 1;
public float ArriveWeight= 1;
public float WanderWeight= 1;
public float SeperationWeight= 2;
public float AlignmentWeight= 1;
public float CohesionWeight = 1;
public Vector2 SeekPos { get; set; }
public Deceleration DecelerationSpeed { get; set; }
public LocomtionSteering()
{
DecelerationSpeed = Deceleration.Normal;
}
float timeElapsed;
public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
{
timeElapsed = gameTime.GetElapsed();
SteeringUpdate(gameTime.GetElapsed());
}
private void SteeringUpdate(float elapsed)
{
Vector2 OldPos = Owner.Position;
SteeringForce = Vector2.Zero;
SteeringForce = Calculate();
Vector2 acceleration = SteeringForce / 1;
Velocity = Vector2.Add(Velocity, acceleration);//* elapsed);
Velocity = Truncate(Velocity, MaxSpeed);
if (Velocity.LengthSquared() > 0.00000001)
{
SetHeading(Vector2.Normalize(Velocity));
}
Owner.Position += Velocity * elapsed;
if(Heading!=Vector2.Zero)
Owner.Orientation = Heading;// (float)Math.Atan2(Heading.Y, Heading.X);
}
private void SetHeading(Vector2 new_heading)
{
Heading= new_heading;
//Right = Perp(_heading);
}
private Vector2 Truncate(Vector2 vec, float max)
{
Vector2 __v = vec;
if (vec.Length() > max)
{
__v = Vector2.Normalize(vec);
__v = Vector2.Multiply(__v, max);
}
return __v;
}
private Vector2 Calculate()
{
_steeringForce = Vector2.Zero;
_steeringForce = CalculatePrioritized();
return _steeringForce;
}
private bool On(BehaviorType type)
{
return (_behaviorFlags & type) == type;
}
private void TurnOn(BehaviorType bt)
{
if(!On(bt))
_behaviorFlags |= bt;
}
private void TurnOff(BehaviorType bt)
{
if(On(bt))
_behaviorFlags ^= bt;
}
private Vector2 CalculatePrioritized()
{
Vector2 force = Vector2.Zero;
if (On(BehaviorType.Seperation))
{
force = Separation() * SeperationWeight;
if (!AccumulateForce(ref _steeringForce, force)) return _steeringForce;
}
if (On(BehaviorType.Alignment))
{
force = Alignment() * AlignmentWeight;
if (!AccumulateForce(ref _steeringForce, force)) return _steeringForce;
}
if (On(BehaviorType.Cohesion))
{
force = Cohesion() * CohesionWeight;
if (!AccumulateForce(ref _steeringForce, force)) return _steeringForce;
}
if (On(BehaviorType.Seek))
{
force = Seek(SeekPos);
force = Vector2.Multiply(force, SeekWeight);
if (!AccumulateForce(ref _steeringForce, force)) return _steeringForce;
}
if (On(BehaviorType.Arrive))
{
force = Arrive(SeekPos, DecelerationSpeed) * ArriveWeight;
if (!AccumulateForce(ref _steeringForce, force)) return _steeringForce;
}
if (On(BehaviorType.Wander))
{
force = Wander() * WanderWeight;
if (!AccumulateForce(ref _steeringForce, force)) return _steeringForce;
}
return _steeringForce;
}
private Vector2 Cohesion()
{
if (Owner.GetLabel(AIConsts.ISGROUPMEMBER) == 0)
return Vector2.Zero;
Group g = Owner.ParentWorld.GetGroup(Owner.GetLabel(AIConsts.GROUPID));
//first find the center of mass of all the agents
Vector2 CenterOfMass=Vector2.Zero;
Vector2 SteeringForce=Vector2.Zero;
int NeighborCount = 0;
List<Agent> _agents = Owner.GetNearbyAgents(100);
//iterate through the neighbors and sum up all the position vectors
for (int a = 0; a < g.Agents.Count; ++a)
{
//make sure *this* agent isn't included in the calculations and that
//the agent being examined is a neighbor
if ((g.Agents[a] != Owner))// && neighbors[a]->IsTagged())
{
CenterOfMass += g.Agents[a].Position;
++NeighborCount;
}
}
if (NeighborCount > 0)
{
//the center of mass is the average of the sum of positions
CenterOfMass /= (float)NeighborCount;
//now seek toward that position
SteeringForce = Seek(CenterOfMass);
}
return SteeringForce;
}
private Vector2 Alignment()
{
if (Owner.GetLabel(AIConsts.ISGROUPMEMBER) == 0)
return Vector2.Zero;
Group g = Owner.ParentWorld.GetGroup(Owner.GetLabel(AIConsts.GROUPID));
//used to record the average heading of the neighbors
Vector2 AverageHeading = Vector2.Zero;
//used to count the number of vehicles in the neighborhood
int NeighborCount = 0;
//iterate through all the tagged vehicles and sum their heading vectors
for (int a = 0; a < g.Agents.Count; ++a)
{
//make sure *this* agent isn't included in the calculations and that
//the agent being examined is close enough
if ((g.Agents[a] != Owner))// && g.Agents[a]->IsTagged)
{
AverageHeading += g.Agents[a].Orientation;
++NeighborCount;
}
}
//if the neighborhood contained one or more vehicles, average their
//heading vectors.
if (NeighborCount > 0)
{
AverageHeading /= (float)NeighborCount;
AverageHeading -= Owner.Orientation;
}
return AverageHeading;
}
private Vector2 Separation()
{
if (Owner.GetLabel(AIConsts.ISGROUPMEMBER) == 0)
return Vector2.Zero;
Group g = Owner.ParentWorld.GetGroup(Owner.GetLabel(AIConsts.GROUPID));
Vector2 SteeringForce = Vector2.Zero;
for (int a = 0; a < g.Agents.Count; ++a)
{
//make sure this agent isn't included in the calculations and that
//the agent being examined is close enough.
if ((g.Agents[a] != Owner)) // && g.Agents[a]->IsTagged())
{
Vector2 ToAgent = Owner.Position - g.Agents[a].Position;
if (ToAgent == Vector2.Zero)
continue;
//scale the force inversely proportional to the agent's distance
//from its neighbor.
SteeringForce +=Vector2.Normalize(ToAgent) / ToAgent.Length();
}
}
return SteeringForce;
}
private bool AccumulateForce(ref Vector2 sf, Vector2 ForceToAdd)
{
//first calculate how much steering force we have left to use
float MagnitudeSoFar = sf.Length();
float magnitudeRemaining = MaxForce - MagnitudeSoFar;
//return false if there is no more force left to use
if (magnitudeRemaining <= 0.0) return false;
//calculate the magnitude of the force we want to add
float MagnitudeToAdd = ForceToAdd.Length();
//now calculate how much of the force we can really add
if (MagnitudeToAdd > magnitudeRemaining)
{
MagnitudeToAdd = magnitudeRemaining;
}
//add it to the steering force
if (ForceToAdd != Vector2.Zero)
sf += (Vector2.Normalize(ForceToAdd) * MagnitudeToAdd);
return true;
}
private Vector2 ObstacleAvoidance()
{
Vector2 _steer=Vector2.Zero;
foreach (var item in Owner.ParentWorld.Map.ClusterGrid.Grids[0].Cells)
{
if (item.Type == 0)
{
//Check feeler 1
Ray _ra = new Ray(Owner.Position.ToVector3(), Owner.Feelers[1].WorldDirection.ToVector3());
Vector3 tl = (item.Position - new Vector2(Owner.ParentWorld.Map.ClusterGrid.Grids[0].CellSize / 2)).ToVector3();
BoundingBox _box = new BoundingBox(
tl,
tl + new Vector3(Owner.ParentWorld.Map.ClusterGrid.Grids[0].CellSize,
Owner.ParentWorld.Map.ClusterGrid.Grids[0].CellSize, 0));
float? inte = _ra.Intersects(_box);
if (inte.HasValue)
{
if (inte < Owner.Feelers[1].Length)
{
_steer = new Vector2(
Owner.Feelers[1].WorldDirection.Y,
-Owner.Feelers[1].WorldDirection.X);
}
}
//Check feeler2
_ra = new Ray(Owner.Position.ToVector3(), Owner.Feelers[2].WorldDirection.ToVector3());
tl = (item.Position - new Vector2(Owner.ParentWorld.Map.ClusterGrid.Grids[0].CellSize / 2)).ToVector3();
_box = new BoundingBox(
tl,
tl + new Vector3(Owner.ParentWorld.Map.ClusterGrid.Grids[0].CellSize,
Owner.ParentWorld.Map.ClusterGrid.Grids[0].CellSize, 0));
inte = _ra.Intersects(_box);
if (inte.HasValue)
{
if (inte < Owner.Feelers[1].Length)
{
_steer = new Vector2(-Owner.Feelers[2].WorldDirection.Y, Owner.Feelers[1].WorldDirection.X);
}
}
}
}
return _steer;
}
public Vector2 Wander()
{
float JitterThisTimeSlice = WanderJitter * timeElapsed;
//first, add a small random vector to the target's position (RandomClamped
//returns a value between -1 and 1)
_wanderTarget += new Vector2(RandomClamped() * JitterThisTimeSlice,
RandomClamped() * JitterThisTimeSlice);
if (_wanderTarget != Vector2.Zero) //reproject this new vector back on to a unit circle
_wanderTarget.Normalize();
//increase the length of the vector to the same as the radius
//of the wander circle
_wanderTarget *= WanderRadius;
//move the target into a position WanderDist in front of the agent
Vector2 _newtarget = _wanderTarget + new Vector2(WanderDistance, 0);
//project the target into world space
Vector2 _target = VectorUtil.PointToWorldSpace(_newtarget,
Owner.Orientation,
Owner.Position);
return _target - Owner.Position;
}
public Vector2 Arrive(Vector2 targetPosition,Deceleration decelspeed)
{
Vector2 toTarget = targetPosition - Owner.Position;
//calculate the distance to the target
float dist = toTarget.Length();
if (dist > 0)
{
//because Deceleration is enumerated as an int, this value is required
//to provide fine tweaking of the deceleration..
const float DecelerationTweaker = 0.3f;
//calculate the speed required to reach the target given the desired
//deceleration
float speed = dist / ((float)decelspeed * DecelerationTweaker);
//make sure the velocity does not exceed the max
speed = Math.Min(speed, MaxSpeed);
//from here proceed just like Seek except we don't need to normalize
//the ToTarget vector because we have already gone to the trouble
//of calculating its length: dist.
Vector2 DesiredVelocity = toTarget * speed / dist;
return (DesiredVelocity - Velocity);
}
return Vector2.Zero;
}
private Vector2 Seek(Vector2 seek)
{
Vector2 tpos = seek - Owner.Position;
Vector2 DesiredVelocity = Vector2.Zero;
if (tpos != Vector2.Zero)
DesiredVelocity = Vector2.Normalize(seek - Owner.Position) * MaxSpeed;
return (DesiredVelocity - Velocity);
}
float RandomClamped()
{
return rand.Next() - rand.Next();
}
}
} |