-
Notifications
You must be signed in to change notification settings - Fork 20
/
Steering.pas
553 lines (471 loc) · 18.2 KB
/
Steering.pas
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
{Copyright (c) 2008 Ville Krumlinde
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.}
unit Steering;
//"Steering" movement
//Based on http://opensteer.sourceforge.net/
{$include zzdc_globalopt.inc}
interface
uses ZClasses,ZMath,Meshes;
type
TSteeringBehaviour = class(TZComponent)
protected
OutVector : TZVector3f;
procedure DefineProperties(List: TZPropertyList); override;
public
TargetModel : TModel;
TargetCategory : integer;
Weight : single;
Kind : (sbkSeekModel,sbkSeparation,sbkNoise,sbkFleeModel,sbkExpression,sbkWallAvoidance);
Expression : TZExpressionPropValue;
{$ifndef minimal}
function GetDisplayName: AnsiString; override;
{$endif}
end;
TSteeringController = class(TCommand)
private
Model : TModel;
ForwardV : TZVector3f;
CurrentAngle : single;
function InBoidNeighborhood(Other : TModel; const MinDistance,MaxDistance,CosMaxAngle : single) : boolean;
procedure SteerForSeek(const Target : TZVector3f; out Result : TZVector3f);
procedure SteerForSeparation(const MaxDistance : single; const CosMaxAngle : single; Flock : TZArrayList; out Result : TZVector3f);
procedure SteerToNoise(out Result : TZVector3f);
procedure SteerForFlee(const Target : TZVector3f; out Result : TZVector3f);
procedure SteerExpression(B : TSteeringBehaviour; out Result : TZVector3f);
procedure SteerWallAvoidance(WallModels : TZArrayList; out Result : TZVector3f);
procedure ApplySteeringForce(const Force : TZVector3f);
function AccumulateForce(var RunningTot : TZVector3f; const ForceToAdd : TZVector3f) : boolean;
class function LineIntersection2D(const A, B, C, D: TZVector2f; out Dist: single;
out HitPoint: TZVector2f): boolean;
protected
procedure DefineProperties(List: TZPropertyList); override;
public
Behaviours : TZComponentList;
MaxForce : single;
MaxSpeed : single;
Mass : single; //default 1 = tröghet i acceleration
Radius : single; //Storlek
AdjustHeading : boolean; //if true, rotera z i den riktning som är forward
procedure Execute; override;
end;
implementation
uses ZApplication, ZExpressions;
procedure TSteeringController.ApplySteeringForce(const Force: TZVector3f);
var
Acceleration,NewVelocity : TZVector3f;
InvMass,Rot : single;
begin
//Clamp sväng-radie
// VecTruncateLength3(Force,MaxForce,ClippedForce);
//Modifiera acceleration med massa
InvMass := 1.0 / Mass;
VecScalarMult3(Force,InvMass,Acceleration);
//Velocity += acc * time
VecCopy3(Model.Velocity,NewVelocity);
VecScalarMult3(Acceleration,ZApp.DeltaTime,Acceleration);
VecAdd3(NewVelocity,Acceleration,NewVelocity);
//Clamp velocity med maxspeed
VecTruncateLength3(NewVelocity,MaxSpeed,NewVelocity);
//Skriv velocity till model
VecCopy3(NewVelocity,Model.Velocity);
if AdjustHeading then
begin
// if not VecIsNull3( NewVelocity ) then
// if VecLength3(NewVelocity)>0.9 then
begin
Rot := ArcTan2(NewVelocity[1], NewVelocity[0]);
//todo: rotation vectors should be in radians
//todo: rotation skulle behöva smoothas in
//men om aktuell rot är 0.9 och ny är 0.1 så fungerar det ej
//då svänger den åt fel håll
Rot := (1/(3.14*2)) * (Rot-3.14/2);
Model.Rotation[2] := Rot;
//Temp1 = Temp1-ShooterModel.Rotation.Z;
end;
end;
end;
procedure TSteeringController.DefineProperties(List: TZPropertyList);
begin
inherited;
List.AddProperty({$IFNDEF MINIMAL}'Behaviours',{$ENDIF}(@Behaviours), zptComponentList);
{$ifndef minimal}List.GetLast.SetChildClasses([TSteeringBehaviour]);{$endif}
List.AddProperty({$IFNDEF MINIMAL}'Mass',{$ENDIF}(@Mass), zptFloat);
List.GetLast.DefaultValue.FloatValue := 1.0;
List.AddProperty({$IFNDEF MINIMAL}'MaxSpeed',{$ENDIF}(@MaxSpeed), zptFloat);
List.AddProperty({$IFNDEF MINIMAL}'MaxForce',{$ENDIF}(@MaxForce), zptFloat);
List.GetLast.DefaultValue.FloatValue := 1.0;
List.AddProperty({$IFNDEF MINIMAL}'AdjustHeading',{$ENDIF}(@AdjustHeading), zptBoolean);
List.AddProperty({$IFNDEF MINIMAL}'Radius',{$ENDIF}(@Radius), zptFloat);
end;
procedure TSteeringController.Execute;
var
I : integer;
B : TSteeringBehaviour;
V,BV : TZVector3f;
begin
//Jobba mot currentmodel
Self.Model := Meshes.CurrentModel;
{$ifndef minimal}
if Model=nil then
Exit;
{$endif}
//Beräkna forward-vector
VecCopy3(Model.Velocity,Self.ForwardV);
VecNormalize3(Self.ForwardV);
//Current heading
Self.CurrentAngle := ArcTan2(ForwardV[1], ForwardV[0]);
FillChar(V,SizeOf(V),0);
for I := 0 to Behaviours.Count-1 do
begin
B := TSteeringBehaviour(Behaviours[I]);
case B.Kind of
sbkSeekModel :
SteerForSeek(B.TargetModel.Position,BV);
sbkSeparation :
SteerForSeparation(Radius*4,-0.707, ZApp.Models.Get(B.TargetCategory) ,BV);
sbkNoise :
SteerToNoise(BV);
sbkFleeModel :
SteerForFlee(B.TargetModel.Position,BV);
sbkExpression :
SteerExpression(B, BV);
sbkWallAvoidance :
SteerWallAvoidance(ZApp.Models.Get(B.TargetCategory), BV);
end;
VecScalarMult3(BV,B.Weight,BV);
//Avbryt så fort vi nått max
//Detta gör att behaviours ligger i prioriteringsordning
//Om WallAvoidance ligger först så kan den stöta ut alla andra
if not AccumulateForce(V, BV) then
Break;
end;
ApplySteeringForce(V);
end;
function TSteeringController.AccumulateForce(var RunningTot : TZVector3f; const ForceToAdd : TZVector3f) : boolean;
var
MagnitudeSoFar,MagnitudeRemaining,MagnitudeToAdd : single;
Tmp : TZVector3f;
begin
//calculate how much steering force the vehicle has used so far
MagnitudeSoFar := VecLength3(RunningTot);
//calculate how much steering force remains to be used by this vehicle
MagnitudeRemaining := Self.MaxForce - MagnitudeSoFar;
//return false if there is no more force left to use
if MagnitudeRemaining <= 0.0 then
begin
Result := False;
Exit;
end;
//calculate the magnitude of the force we want to add
MagnitudeToAdd := VecLength3(ForceToAdd);
//if the magnitude of the sum of ForceToAdd and the running total
//does not exceed the maximum force available to this vehicle, just
//add together. Otherwise add as much of the ForceToAdd vector is
//possible without going over the max.
if MagnitudeToAdd < MagnitudeRemaining then
VecAdd3(RunningTot,ForceToAdd,RunningTot)
else
begin
//add it to the steering force
Tmp := ForceToAdd;
VecNormalize3(Tmp);
VecScalarMult3(Tmp,MagnitudeRemaining,Tmp);
VecAdd3(RunningTot,Tmp,RunningTot);
end;
Result := True;
end;
procedure TSteeringController.SteerForSeek(const Target : TZVector3f; out Result : TZVector3f);
var
DesiredVelocity : TZVector3f;
begin
VecSub3(Target,Model.Position,DesiredVelocity);
VecTruncateLength3(DesiredVelocity,Self.MaxSpeed,DesiredVelocity);
VecSub3(DesiredVelocity,Model.Velocity,Result);
end;
procedure TSteeringController.SteerForFlee(const Target: TZVector3f; out Result: TZVector3f);
var
DesiredVelocity : TZVector3f;
begin
VecSub3(Model.Position,Target,DesiredVelocity);
VecTruncateLength3(DesiredVelocity,Self.MaxSpeed,DesiredVelocity);
VecSub3(DesiredVelocity,Model.Velocity,Result);
end;
function TSteeringController.InBoidNeighborhood(Other : TModel; const MinDistance,MaxDistance,CosMaxAngle : single) : boolean;
var
Offset,UnitOffset : TZVector3f;
Forwardness,DistanceSquared : single;
begin
if Other=Model then
Result := False
else
begin
VecSub3(Other.Position,Model.Position,Offset);
DistanceSquared := VecLengthSquared3(Offset);
// definitely in neighborhood if inside minDistance sphere
if DistanceSquared < (MinDistance * MinDistance) then
Result := True
else
begin
// definitely not in neighborhood if outside maxDistance sphere
if DistanceSquared > (MaxDistance * MaxDistance) then
Result := False
else
begin
// otherwise, test angular offset from forward axis
VecDiv3(Offset,Sqrt(DistanceSquared),UnitOffset);
Forwardness := VecDot3(ForwardV,UnitOffset);
Result := Forwardness > CosMaxAngle;
end;
end;
end;
end;
procedure TSteeringController.SteerForSeparation(const MaxDistance,
CosMaxAngle: single; Flock: TZArrayList; out Result: TZVector3f);
var
Offset,Temp : TZVector3f;
Neighbors : integer;
Other : TModel;
I : integer;
DistanceSquared : single;
begin
// steering accumulator and count of neighbors, both initially zero
FillChar(Result,SizeOf(Result),0);
Neighbors := 0;
// for each of the other vehicles...
for I := 0 to Flock.Count-1 do
begin
Other := TModel(Flock[I]);
if InBoidNeighborhood(Other,Self.Radius*3,MaxDistance,CosMaxAngle) then
begin
// add in steering contribution
// (opposite of the offset direction, divided once by distance
// to normalize, divided another time to get 1/d falloff)
VecSub3(Other.Position,Model.Position,Offset);
DistanceSquared := VecLengthSquared3(Offset);
if DistanceSquared<Epsilon then
//Avoid div by zero
DistanceSquared:=Epsilon;
VecDiv3(Offset,-DistanceSquared,Temp);
VecAdd3(Result,Temp,Result);
// count neighbors
Inc(Neighbors);
end;
end;
// divide by neighbors, then normalize to pure direction
if Neighbors > 0 then
begin
VecDiv3(Result,Neighbors,Result);
VecNormalize3(Result);
end;
end;
procedure TSteeringController.SteerToNoise(out Result: TZVector3f);
var
Angle : single;
begin
//Beräkna en kurs som är max +/- 25% av aktuell kurs
Angle := ZMath.PerlinNoise3( Model.Position[0],Model.Position[1],ZApp.Time) * (PI*2);
Angle := CurrentAngle + Angle*0.25;
// Angle := System.Random * (PI*2);
Result[0] := cos(Angle);
Result[1] := sin(Angle);
Result[2] := 0;
end;
procedure TSteeringController.SteerExpression(
B : TSteeringBehaviour; out Result: TZVector3f);
begin
ZExpressions.RunCode(B.Expression.Code);
Result := B.OutVector;
end;
//-------------------- LineIntersection2D-------------------------
//
// Given 2 lines in 2D space AB, CD this returns true if an
// intersection occurs and sets dist to the distance the intersection
// occurs along AB. Also sets the 2d vector point to the point of
// intersection
//-----------------------------------------------------------------
class function TSteeringController.LineIntersection2D(const A,B,C,D : TZVector2f;
out Dist : single;
out HitPoint : TZVector2f) : boolean;
var
rTop,rBot : single;
sTop,sBot : single;
r,s : single;
Tmp : TZVector2f;
begin
Result := False;
rTop := (A[1]-C[1])*(D[0]-C[0])-(A[0]-C[0])*(D[1]-C[1]);
rBot := (B[0]-A[0])*(D[1]-C[1])-(B[1]-A[1])*(D[0]-C[0]);
sTop := (A[1]-C[1])*(B[0]-A[0])-(A[0]-C[0])*(B[1]-A[1]);
sBot := (B[0]-A[0])*(D[1]-C[1])-(B[1]-A[1])*(D[0]-C[0]);
if (rBot=0) or (sBot=0) then
//lines are parallel
Exit;
r := rTop/rBot;
s := sTop/sBot;
if (r > 0) and (r < 1) and (s > 0) and (s < 1) then
begin
Dist := Vec2DDistance(A,B) * r;
VecSub2(B,A,Tmp);
Tmp := VecScalarMult2(Tmp,R);
HitPoint := VecAdd2(A,Tmp);
//point = A + r * (B - A);
Result := True;
end
else
Dist := 0;
end;
procedure TSteeringController.SteerWallAvoidance(WallModels: TZArrayList; out Result: TZVector3f);
//Från boken "Programming game AI by example"
type
TWall2D =
record
P1,P2 : TZVector2f; //Start and endpoint of wall
end;
const
FeelerLength = 0.75;
var
Feelers : array[0..2] of TZVector2f;
Feeler : PZVector2f;
Walls : array[0..3] of TWall2D;
Bounds : PCollisionCoordinates;
I,J,K,ClosestWall : integer;
ModelPos2D,HitPoint,ClosestPoint,OverShoot,Tmp : TZVector2f;
DistToThis,DistToClosest,T1 : single;
function GetWallNormal(const W : TWall2d) : TZVector2f;
var
Tmp : TZVector2f;
begin
VecSub2(W.P2,W.P1,Tmp);
VecNormalize2(Tmp);
Result[0] := -Tmp[1];
Result[1] := Tmp[0];
end;
begin
FillChar(Result,SizeOf(Result),0);
ModelPos2D := Vector2f(Model.Position[0],Model.Position[1]);
//Skapa "känselsprön"
{ Feelers[0] := VecAdd2(ModelPos2D,
VecScalarMult2(
VecScalarMult2( Vector2f(ForwardV[0],ForwardV[1]) , FeelerLength ) ,
VecLength2( Vector2f(Self.Model.Velocity[0],Self.Model.Velocity[1]) ) ) );}
// Feelers[0] := VecAdd2(ModelPos2D, Vector2f(Self.Model.Velocity[0],Self.Model.Velocity[1]) );
// Feelers[0] := VecAdd2(ModelPos2D, VecScalarMult2( Vector2f(ForwardV[0],ForwardV[1]) , FeelerLength ) );
//Forward feeler måste scalas med length(velocity) för att hinna bromsa
//när model rör sig snabbt
T1 := VecLength2( Vector2f(Self.Model.Velocity[0],Self.Model.Velocity[1]) );
Feelers[0] := VecAdd2(ModelPos2D,
VecScalarMult2(
VecScalarMult2( Vector2f(ForwardV[0],ForwardV[1]) , FeelerLength ),
T1)
);
//Right feeler
T1 := Self.CurrentAngle + PI*0.25;
Feelers[1] := VecAdd2(ModelPos2D, VecScalarMult2( Vector2f(Cos(T1),Sin(T1)) , FeelerLength ) );
//Left feeler
T1 := Self.CurrentAngle - PI*0.25;
Feelers[2] := VecAdd2(ModelPos2D, VecScalarMult2( Vector2f(Cos(T1),Sin(T1)) , FeelerLength ) );
DistToClosest := 100000; //default to high value
for I := 0 to WallModels.Count-1 do
begin
//obs, collisionstyle måste vara rect2d
TModel(WallModels[I]).UpdateCollisionCoordinates;
Bounds := @TModel(WallModels[I]).CollisionCoordinates;
Walls[0].P1 := Vector2f(Bounds.Rect.Right,Bounds.Rect.Top);
Walls[0].P2 := Vector2f(Bounds.Rect.Left,Bounds.Rect.Top);
Walls[1].P1 := Vector2f(Bounds.Rect.Left,Bounds.Rect.Bottom);
Walls[1].P2 := Vector2f(Bounds.Rect.Right,Bounds.Rect.Bottom);
Walls[2].P1 := Vector2f(Bounds.Rect.Left,Bounds.Rect.Top);
Walls[2].P2 := Vector2f(Bounds.Rect.Left,Bounds.Rect.Bottom);
Walls[3].P1 := Vector2f(Bounds.Rect.Right,Bounds.Rect.Bottom);
Walls[3].P2 := Vector2f(Bounds.Rect.Right,Bounds.Rect.Top);
Feeler := @Feelers[0];
for J := 0 to High(Feelers) do
begin
ClosestWall := -1;
for K := 0 to High(Walls) do
begin
if not LineIntersection2D(ModelPos2D,
Feeler^,
Walls[K].P1,
Walls[K].P2,
DistToThis,
HitPoint) then
Continue;
if DistToThis<DistToClosest then
begin
//is this the closest found so far? If so keep a record
DistToClosest := DistToThis;
ClosestWall := K;
ClosestPoint := HitPoint;
end;
end;
//if an intersection point has been detected, calculate a force
//that will direct the agent away
if ClosestWall>-1 then
begin
//calculate by what distance the projected position of the agent
//will overshoot the wall
VecSub2(Feeler^,ClosestPoint,OverShoot);
//create a force in the direction of the wall normal, with a
//magnitude of the overshoot
Tmp := VecScalarMult2( GetWallNormal(Walls[ClosestWall]) , VecLength2(OverShoot));
//Gör om 2d vector till 3d
Result := Vector3f(Tmp[0],Tmp[1],0);
end;
Inc(Feeler);
end;
end;
end;
{ TSteeringBehaviour }
{$ifndef minimal}
const
SteerKindNames : array [0..5] of string = (('SeekModel'),('SeparationCategory'),
('Noise'),('FleeModel'),('Expression'),('WallAvoidance2D'));
{$endif}
procedure TSteeringBehaviour.DefineProperties(List: TZPropertyList);
begin
inherited;
List.AddProperty({$IFNDEF MINIMAL}'Kind',{$ENDIF}(@Kind), zptByte);
{$ifndef minimal}List.GetLast.SetOptions(SteerKindNames);{$endif}
{$ifndef minimal}List.GetLast.NeedRefreshNodeName:=True;{$endif}
List.AddProperty({$IFNDEF MINIMAL}'TargetModel',{$ENDIF}(@TargetModel), zptComponentRef);
{$ifndef minimal}List.GetLast.SetChildClasses([TModel]);{$endif}
List.AddProperty({$IFNDEF MINIMAL}'TargetCategory',{$ENDIF}(@TargetCategory), zptInteger);
List.AddProperty({$IFNDEF MINIMAL}'Weight',{$ENDIF}(@Weight), zptFloat);
List.GetLast.DefaultValue.FloatValue := 1.0;
List.AddProperty({$IFNDEF MINIMAL}'Expression',{$ENDIF}(@Expression), zptExpression);
{$ifndef minimal}
List.GetLast.DefaultValue.ExpressionValue.Source :=
'//OutVector : result steer vector';
{$endif}
//Outvector for expressions
List.AddProperty({$IFNDEF MINIMAL}'OutVector',{$ENDIF}(@OutVector), zptVector3f);
List.GetLast.NeverPersist := True;
end;
{$ifndef minimal}
function TSteeringBehaviour.GetDisplayName: AnsiString;
begin
Result := inherited GetDisplayName;
Result := Result + ' ' + AnsiString(SteerKindNames[ ord(Kind) ]);
end;
{$endif}
initialization
ZClasses.Register(TSteeringController,SteeringControllerClassId);
{$ifndef minimal}ComponentManager.LastAdded.ImageIndex := 58;{$endif}
ZClasses.Register(TSteeringBehaviour,SteeringBehaviourClassId);
{$ifndef minimal}ComponentManager.LastAdded.NeedParentComp := 'SteeringController';{$endif}
end.