Repository navigation
Expand file tree
/
Copy pathGS.Engine.Particles2D.pas
More file actions
718 lines (595 loc) · 19.9 KB
/
Copy pathGS.Engine.Particles2D.pas
File metadata and controls
718 lines (595 loc) · 19.9 KB
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
unit GS.Engine.Particles2D;
interface
Uses Classes,
FMeX.Ticks,
GS.Geometry.Direction;
type
TGSCustomParticleEngine2D = class;
TGSCustomParticleEngineClass = Class of TGSCustomParticleEngine2D;
TGSParticle2D = record
X,Y : Single; //Current Position
TimeToLiveInMilliSec : Double; //Total time that Particle have to live. (Constant)
TimeRelevantInMilliSec : Double; //Time to live ammount for particle.
TimeStart : Int64; //Internal use for estimate time.
VelX,VelY : Double; //Current Speed In pixels per seconds.
TagOne : Cardinal; //General Usage Tag 1. (color, object link.)
TagTwo : Cardinal; //General Usage Tag 2.
ColX,ColY : Double; //Coord of last collision.
Collided : Boolean; //If true, it is in collided situation.
end;
TGSForce2d = Class
Public
ForceVector : TVector;
Constructor Create;
end;
TGSForceList = class(Tlist)
private
function GetForce(index: cardinal): TGSForce2d;
procedure SetForce(index: cardinal; const Value: TGSForce2d);
Public
Procedure AddForce(Fx,Fy : Double);
//Procedure AddForce(F: TVector);
//Procedure AddForcePonctual(Fx,Fy : Integer); //remove automATICALY AFTER APPLY
//Procedure ReplaceAllForceByOneResultForce;
Property Forces[index : cardinal] : TGSForce2d read GetForce Write SetForce; Default;
end;
//this class must be heritate to define shape for collision. A particle is mathematicaly discreet, so, it need at least a line, to collide.
TGSCollidable2d = Class
Public
Procedure Resolve(var aParticle : TGSParticle2D); virtual; Abstract;
// Procedure Draw(re : ofRenderer); Virtual; Abstract;
end;
TGSCollidableList = class(Tlist)
private
function GetCollidable(Index: Cardinal): TGSCollidable2d;
procedure SetCollidable(Index: Cardinal; const Value: TGSCollidable2d);
Public
Procedure AddCollideShape(aCollidable : TGSCollidable2d);
Procedure AddCollideLine(x,y,x2,y2 : Double; EnergyRestitution : Double = 0.70);
Property Collidables[Index : Cardinal] : TGSCollidable2d read GetCollidable Write SetCollidable; Default;
end;
TGSCollidable2d_StaticLine = class(TGSCollidable2d)
private
FEnergyRestitut: Double;
Public
X1,Y1,X2,Y2 : Double;
Constructor Create(ax,ay,ax1,ay1 : Double; EnergyRestitution : Double = 0.70); Virtual;
// Procedure Draw(re : ofRenderer); Override;
Procedure Resolve(var aParticle : TGSParticle2D); Override;
//For simplify calulus, we introduce a single var which make "aproximation" of vector calculus for particle dynamic.
//This will simply added on velocity.
Property EnergyRestitutionOnCollide : Double read FEnergyRestitut Write FEnergyRestitut;
end;
TGSCustomParticleEngine2D = class
private
FTimeManage: Boolean;
FCollisionManage: Boolean;
FForceManageEnabled: Boolean;
FConstraintEnabled: Boolean;
FMaximalVelocity: Single;
function GetParticleCount: Cardinal;
procedure SetParticleCount(const Value: Cardinal);
function GetParticle(Index: Cardinal): TGSParticle2D;
procedure SetParticle(Index: Cardinal; const Value: TGSParticle2D);
function GetIsTimeElapsed: Boolean;
protected
Procedure ResetTimeEval;
Procedure ResetPositionAndVelocity;
Procedure ResetCollisitionStatus;
Public
Position : TPt;
Offset : TPt;
ParticleList : array of TGSParticle2D;
Object_ForceList : TGSForceList; //Global Force list. Apply to all particle.
Object_CollideList : TGSCollidableList; //Global Collision test. Apply to all particle.
constructor Create(aParticleCount : Cardinal); virtual;
destructor Destroy; Override;
Procedure initialize; Virtual;
Procedure Update; virtual;
Function AddParticle(ax,ay,avelx,avely : Double) : TGSParticle2D;
// Procedure Draw(re : ofRenderer);
property ParticleCount : Cardinal read GetParticleCount Write SetParticleCount;
property Particles[Index : Cardinal] : TGSParticle2D read GetParticle Write SetParticle; Default;
property TimeManageEnabled : Boolean read FTimeManage write FTimeManage;
PRoperty IsTimeElapsed : Boolean read GetIsTimeElapsed;
property CollisionManageEnabled : Boolean read FCollisionManage Write FCollisionManage;
property ForceManagerEnabled : Boolean read FForceManageEnabled Write FForceManageEnabled;
Property ConstraintEnabled : Boolean read FConstraintEnabled Write FConstraintEnabled;
Property MaximalVelocity : Single read FMaximalVelocity Write FMaximalVelocity;
end;
TGSParticleEngine2d = class(TGSCustomParticleEngine2D) //General use. (especially for complexe entity, such video game hero which need collision or gravity :))
end;
//StarField. (simple, left to right)
TGSParticleEngine2D_StarField = class(TGSCustomParticleEngine2D)
private
FMaxVelocity: Cardinal;
public
Constructor Create(aParticleCount : Cardinal); Override;
Procedure Initialize; override;
Procedure Update; Override;
property BehaviourMaxVelocity : Cardinal read FMaxVelocity Write FMaxVelocity;
end;
//Base emiter : Emit all in one loop, then, reemit on end of life time.
TGSCustomParticleEngine2D_DiscretEmiter = Class(TGSCustomParticleEngine2D)
Private
FParticleDuration: Cardinal;
public
Constructor Create(aParticleCount : Cardinal); Override;
Procedure Initialize; override;
property ParticleDefaultDurationMilliSec : Cardinal read FParticleDuration Write FParticleDuration;
end;
//Explode.
TGSParticleEngine2D_Explode = class(TGSCustomParticleEngine2D_DiscretEmiter)
Private
FMaxVelocity: Cardinal;
FMaxDurationInMS: Cardinal;
FQQ: Boolean;
FAD: Integer;
FADD: Double;
public
Constructor Create(aParticleCount : Cardinal); Override;
Procedure Initialize; override;
property BehaviourMaxVelocity : Cardinal read FMaxVelocity Write FMaxVelocity;
property BehaviourMaxDurationInMS : Cardinal read FMaxDurationInMS Write FMaxDurationInMS;
property BehaviourQuickSpeedMeansQuickDie : Boolean read FQQ Write FQQ;
//AngleDistribution : Default value is 360 degree. That is a "opening angle", it will distribute the particle's angle on 360 toward Start angle (0 by default)
property AngleDistribution : Integer read FAD write FAD; //Usage : Put 30 degree value and you'll otain something such as "reactor burst effect"
property AngleDistributionDirection : Double read FADD write FADD; //0 by default.
end;
//Explode with (re)emition. (Cool for magical star.)
TGSParticleEngine2D_ExplodeAdLib = class(TGSParticleEngine2D_Explode)
Private
public
Constructor Create(aParticleCount : Cardinal); Override;
Procedure Update; Override;
end;
//Emitter - Emits particle. (cool for smoke behing engine)
TGSParticleEngine2D_Emiter = class(TGSCustomParticleEngine2D_DiscretEmiter)
Private
FEmitParticleCount: Cardinal;
FinternalCount : cardinal;
public
Constructor Create(aParticleCount : Cardinal); Override;
Procedure Initialize; Override;
Procedure Update; Override;
Property EmitParticleQuantityByLoop : Cardinal Read FEmitParticleCount Write FEmitParticleCount;
end;
//Snow or leaf fallen.
//rain, fire ?
//Particle with collision ? (Bullet)
//Particle with gravity (implenting the example in PROTOTYPES)
//Image ? (with each pixels (or Each little quarter of pict.) is a particle --> Nice effect.)
implementation
uses Math;
{ TGSCustomParticleEngine2D }
function TGSCustomParticleEngine2D.AddParticle(ax, ay, avelx,
avely: Double): TGSParticle2D;
var a : TGSParticle2D;
begin
a.X:=ax;
a.Y:=ay;
a.VelX:=avelx;
a.VelY:=avely;
end;
constructor TGSCustomParticleEngine2D.Create(aParticleCount: Cardinal);
begin
ParticleCount := aParticleCount;
FTimeManage := True;
FCollisionManage := False;
FForceManageEnabled := False;
FConstraintEnabled:= False;
Object_ForceList := TGSForceList.Create;
Object_CollideList := TGSCollidableList.Create;
Offset := Point(100,100,0);
FMaximalVelocity := 50;
end;
destructor TGSCustomParticleEngine2D.Destroy;
begin
SetLength(ParticleList,0);
Object_ForceList.Free;
inherited;
end;
{
procedure TGSCustomParticleEngine2D.Draw(re : ofRenderer);
var i : integer;
begin
if TimeManageEnabled then
begin
for i:=0 to ParticleCount-1 do
begin
if ParticleList[i].TimeRelevantInMilliSec>0 then
re.Rectangle( ParticleList[i].X-3,Particles[i].Y-3,
ParticleList[i].X+3,Particles[i].Y+3);
end;
end
else
begin
for i:=0 to ParticleCount-1 do
begin
re.Rectangle( ParticleList[i].X-3,Particles[i].Y-3,
ParticleList[i].X+3,Particles[i].Y+3);
end;
end;
end;
}
function TGSCustomParticleEngine2D.GetIsTimeElapsed: Boolean;
var i : integer;
begin
Result := TimeManageEnabled;
if Result then
for i := 0 to ParticleCount-1 do
begin
if ParticleList[i].TimeRelevantInMilliSec > 0 then
begin
Result := false;
Break;
end;
end;
end;
function TGSCustomParticleEngine2D.GetParticle(
Index: Cardinal): TGSParticle2D;
begin
Assert(Index<High(ParticleList)+1);
Result := ParticleList[Index];
end;
function TGSCustomParticleEngine2D.GetParticleCount: Cardinal;
begin
Result := High(ParticleList)+1;
end;
procedure TGSCustomParticleEngine2D.initialize;
begin
ResetTimeEval;
ResetPositionAndVelocity;
ResetCollisitionStatus;
end;
procedure TGSCustomParticleEngine2D.ResetCollisitionStatus;
var i : integer;
begin
for i := 0 to ParticleCount-1 do
ParticleList[i].Collided:=false;
end;
procedure TGSCustomParticleEngine2D.ResetPositionAndVelocity;
var i : integer;
begin
for i := 0 to ParticleCount-1 do
begin
ParticleList[i].VelX:=0;
ParticleList[i].VelY:=0;
ParticleList[i].X:=0;
ParticleList[i].Y:=0;
end;
end;
procedure TGSCustomParticleEngine2D.ResetTimeEval;
var i : integer;
begin
for i := 0 to ParticleCount-1 do
ParticleList[i].TimeStart := TheCadencer.Tic;
end;
procedure TGSCustomParticleEngine2D.SetParticle(Index: Cardinal;
const Value: TGSParticle2D);
begin
Assert(Index<=High(ParticleList));
ParticleList[Index] := Value;
end;
procedure TGSCustomParticleEngine2D.SetParticleCount(const Value: Cardinal);
begin
SetLength(ParticleList,Value);
end;
procedure TGSCustomParticleEngine2D.Update;
var i,j : integer;
m : Cardinal;
begin
if FForceManageEnabled then
begin
for i := 0 to ParticleCount-1 do
begin
For j:=0 to Object_ForceList.Count-1 do
begin
//ParticleList[i].VelX:=(ParticleList[i].VelX+Object_ForceList[j].ForceVector.X);
//ParticleList[i].VelY:=(ParticleList[i].VelY+Object_ForceList[j].ForceVector.Y);
ParticleList[i].VelX:=(ParticleList[i].VelX+ Object_ForceList[j].ForceVector.X * TheCadencer.TimeScaler);
ParticleList[i].VelY:=(ParticleList[i].VelY+ Object_ForceList[j].ForceVector.Y * TheCadencer.TimeScaler);
if ParticleList[i].VelX>FMaximalVelocity then
ParticleList[i].VelX := FMaximalVelocity;
if ParticleList[i].VelY>FMaximalVelocity then
ParticleList[i].VelY := FMaximalVelocity;
end;
end;
end;
if FCollisionManage then
begin
for i := 0 to ParticleCount-1 do
begin
for j:= 0 to Object_CollideList.Count-1 do
begin
particleList[i].Collided:=False;
Object_CollideList[j].Resolve(particleList[i]); //It the shape which is in charge of the particle behaviour.
if particleList[i].Collided then
Break;
end;
end;
end;
if FTimeManage then
begin
for i := 0 to ParticleCount-1 do
begin
If ParticleList[i].TimeRelevantInMilliSec>0 then
begin
ParticleList[i].X:=ParticleList[i].X+TheCadencer.TimeSliceValue(ParticleList[i].VelX);
ParticleList[i].Y:=ParticleList[i].Y+TheCadencer.TimeSliceValue(ParticleList[i].VelY);
ParticleList[i].TimeRelevantInMilliSec := ParticleList[i].TimeRelevantInMilliSec - TheCadencer.DeltaTime; // (m-ParticleList[i].TimeStart);
end;
end;
end
else
begin
for i := 0 to ParticleCount-1 do
begin
ParticleList[i].X:=ParticleList[i].X+TheCadencer.TimeSliceValue(ParticleList[i].VelX);
ParticleList[i].Y:=ParticleList[i].Y+TheCadencer.TimeSliceValue(ParticleList[i].VelY);
end;
end;
end;
{ TGSParticleEngine2D_StarField }
constructor TGSParticleEngine2D_StarField.Create(aParticleCount: Cardinal);
begin
inherited;
FMaxVelocity := 6;
end;
procedure TGSParticleEngine2D_StarField.Initialize;
var i,rx,ry : integer;
begin
Inherited;
TimeManageEnabled:=False;
Randomize;
for i := 0 to ParticleCount-1 do
begin
rx := Random(Round(Offset.X));
ry := Random(Round(Offset.Y));
ParticleList[i].X:=Position.X+rx;
ParticleList[i].Y:=Position.Y+ry;
ParticleList[i].VelX:=Random(FMaxVelocity)+2; //velocity : 8 unity secs. with a minimum of 2
ParticleList[i].VelY:=0;
end;
end;
procedure TGSParticleEngine2D_StarField.Update;
var i : integer;
begin
Inherited Update;
for i := 0 to ParticleCount-1 do
begin
if ParticleList[i].X>Offset.X then
ParticleList[i].X:=Position.x;
if ParticleList[i].Y>Offset.Y then
ParticleList[i].Y:=Position.Y;
if ParticleList[i].X<Position.X then
ParticleList[i].X:=Position.X;
if ParticleList[i].Y<Position.Y then
ParticleList[i].Y:=Position.Y;
end;
end;
{ TGSParticleEngine2D_DiscretEmiter }
constructor TGSCustomParticleEngine2D_DiscretEmiter.Create(
aParticleCount: Cardinal);
begin
inherited;
ParticleDefaultDurationMilliSec := 1000; //1 sec.
end;
procedure TGSCustomParticleEngine2D_DiscretEmiter.Initialize;
var i : integer;
begin
Inherited;
For i:=0 to ParticleCount-1 do
begin
ParticleList[i].TimeToLiveInMilliSec:=ParticleDefaultDurationMilliSec;
ParticleList[i].TimeRelevantInMilliSec:=ParticleDefaultDurationMilliSec;
ParticleList[i].TimeStart := TheCadencer.Tic;
end;
end;
{ TGSParticleEngine2D_Explode }
constructor TGSParticleEngine2D_Explode.Create(aParticleCount: Cardinal);
begin
inherited Create(aParticleCount);
FMaxVelocity := 5;
FMaxDurationInMS := 2000; //2 Sec.
FQQ := False; //Quicker is the paticle, Faster it die.
FAD := 360;
FADD := 0;
end;
procedure TGSParticleEngine2D_Explode.Initialize;
var i : integer;
v : TVector;
pp : single;
begin
Inherited;
Randomize;
TimeManageEnabled:=True;
v.X:=10;
v.Y:=0;
pp := Pi/180;
For i:=0 to ParticleCount-1 do
begin
ParticleList[i].X:=Position.X;
ParticleList[i].Y:=Position.Y;
vNorm(v,Random(FMaxVelocity)+10+(Random(1000)/1000));
//--> implementer here : to see again...
if i < (ParticleCount div 2) then
vAngle(v,FADD + Random(FAD) * pp + (Random(1000)/1000))
else
vAngle(v,FADD - Random(FAD) * pp + (Random(1000)/1000));
ParticleList[i].VelX:= v.X;
ParticleList[i].VelY:= v.Y;
if FQQ Then
begin
ParticleList[i].TimeToLiveInMilliSec := (Random(Round(FMaxDurationInMS)) / (vNorm(v)/10)) + 100; //100ms min. indexed on speed : More it speed, quicker it's die.
end
else
ParticleList[i].TimeToLiveInMilliSec := Random(Round(FMaxDurationInMS))+100; //100ms min
ParticleList[i].TimeRelevantInMilliSec := ParticleList[i].TimeToLiveInMilliSec;
end;
end;
{ TGSParticleEngine2D_ExplodeEx }
constructor TGSParticleEngine2D_ExplodeAdLib.Create(
aParticleCount: Cardinal);
begin
inherited Create(aParticleCount);
BehaviourMaxDurationInMS := 1000;
BehaviourQuickSpeedMeansQuickDie := False;
end;
procedure TGSParticleEngine2D_ExplodeAdLib.Update;
var i : integer;
v : TVector;
pp : single;
begin
inherited;
v.X:=1;
v.Y:=0;
pp := PI/180;
For i:=0 to ParticleCount-1 do
begin
if ParticleList[i].TimeRelevantInMilliSec<=0 then //Reemit if terminated.
begin
ParticleList[i].X:=Position.X;
ParticleList[i].Y:=Position.Y;
ParticleList[i].TimeRelevantInMilliSec := ParticleList[i].TimeToLiveInMilliSec;
ParticleList[i].TimeStart:=TheCadencer.Tic;
vNorm(v,Random(BehaviourMaxVelocity)+1+(Random(1000)/1000));
if i< (ParticleCount div 2) then
vAngle(v,FADD + Random(FAD)* pp +(Random(100)/1000))
else
vAngle(v,FADD - Random(FAD)* pp +(Random(100)/1000));
//vAngle(v,Random(360)+(Random(1000)/1000));
//vAngle(v,45+(Random(1000)/1000)); //<-- Cool effect (motor burst ?)
//vAngle(v,FADD + (Random(100)/1000));
ParticleList[i].VelX:=v.X;
ParticleList[i].VelY:=v.Y;
end;
end;
end;
{ TGSParticleEngine2D_DiscretEmiter }
constructor TGSParticleEngine2D_Emiter.Create(aParticleCount: Cardinal);
begin
inherited;
EmitParticleQuantityByLoop:=5;
end;
procedure TGSParticleEngine2D_Emiter.Initialize;
var i : Integer;
begin
inherited;
For i:=0 to ParticleCount-1 do
begin
ParticleList[i].TimeToLiveInMilliSec:=0;
ParticleList[i].TimeRelevantInMilliSec:=0;
end;
TimeManageEnabled:=True;
ResetTimeEval;
FinternalCount := 0;
end;
procedure TGSParticleEngine2D_Emiter.Update;
var i : Integer;
ic : integer;
begin
inherited;
if FinternalCount=ParticleCount-1 then
FinternalCount:=0;
i:=FinternalCount;
ic:= 0;
While (i<ParticleCount) And (ic<EmitParticleQuantityByLoop) do
begin
if ParticleList[i].TimeRelevantInMilliSec<=0 then
begin
Inc(ic);
ParticleList[i].TimeToLiveInMilliSec:=ParticleDefaultDurationMilliSec;
ParticleList[i].TimeRelevantInMilliSec:=ParticleDefaultDurationMilliSec;
ParticleList[i].TimeStart:=GetTickCount;
ParticleList[i].X:=Position.X;
ParticleList[i].Y:=Position.Y;
FinternalCount:=i;
end;
Inc(i);
end;
end;
{ TGSForce2d }
constructor TGSForce2d.Create;
begin
vInit(forceVector);
ForceVector.X := 1;
end;
{ TGSForceList }
procedure TGSForceList.AddForce(Fx, Fy: Double);
var v : TGSForce2d;
begin
v:=TGSForce2d.Create;
v.ForceVector.X:=fx;
v.ForceVector.Y:= fy;
Add(v);
end;
function TGSForceList.GetForce(index: cardinal): TGSForce2d;
begin
Result := TGSForce2D(Get(Index));
end;
procedure TGSForceList.SetForce(index: cardinal; const Value: TGSForce2d);
begin
Put(Index,Value);
end;
{ TGSCollidableList }
procedure TGSCollidableList.AddCollideLine(x, y, x2, y2: Double; EnergyRestitution : Double = 0.70);
begin
AddCollideShape(TGSCollidable2d_StaticLine.Create(x,y,x2,y2, EnergyRestitution));
end;
procedure TGSCollidableList.AddCollideShape(aCollidable: TGSCollidable2d);
begin
Assert(assigned(aCollidable));
Add(aCollidable);
end;
function TGSCollidableList.GetCollidable(Index: Cardinal): TGSCollidable2d;
begin
result := TGSCollidable2d(get(index));
end;
procedure TGSCollidableList.SetCollidable(Index: Cardinal;
const Value: TGSCollidable2d);
begin
Put(Index,Value);
end;
{ TGSCollidable2d_Line }
constructor TGSCollidable2d_StaticLine.Create(ax, ay, ax1, ay1: Double; EnergyRestitution : Double = 0.70);
begin
inherited Create;
x1 := ax;
y1 := ay;
x2 := ax1;
y2 := ay1;
FEnergyRestitut := EnergyRestitution;
end;
{
procedure TGSCollidable2d_StaticLine.Draw(re: ofRenderer);
begin
re.Line(X1,Y1,X2,Y2);
end;
}
procedure TGSCollidable2d_StaticLine.Resolve(var aParticle: TGSParticle2D);
var ix,iy : double;
n1,n2 : Double;
tvx,tvy : Double;
begin
//We firstly solve velocities vector parameter : Particule and line.
tvx := TheCadencer.TimeSliceValue(aParticle.VelX);
tvy := TheCadencer.TimeSliceValue(aParticle.VelY);
if (tvx<>0) or (tvy<>0) then
begin
If vIntersect(x1,y1,x2,y2,aParticle.X,aParticle.Y,aParticle.X+tvx,aParticle.Y+tvy,ix,iy) then
begin
//Particle has no mass : then, it will bounce again a line, with a EnergyRestitute parameters.
//and the line will not be affected.
vMirror(ix+aParticle.VelX,iy+aParticle.VelY,X1,Y1,X2,Y2,n1,n2);
aParticle.VelX:=(n1-ix)*FEnergyRestitut;
aParticle.VelY:=(n2-iy)*FEnergyRestitut;
aParticle.X:=ix;
aParticle.Y:=iy;
aParticle.ColX:=ix;
aParticle.ColY:=iy;
aParticle.Collided:=true;
end;
end;
end;
end.