-
Notifications
You must be signed in to change notification settings - Fork 0
/
particlephysics.pas
1405 lines (1279 loc) · 45.2 KB
/
particlephysics.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
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
UNIT particlePhysics;
{$mode objfpc}{$H+}
INTERFACE
USES vectors,GL,serializationUtil;
TYPE
TParticle = record
p,v,a,
color:TVector3;
end;
FUpdateAcceleration =PROCEDURE(CONST progress:double) of object;
FUpdateAccelerationNoninteracting=PROCEDURE(CONST progress:double; CONST particleIndex:longint) of object;
TParticleEngine = class
private
//Pseudo constants
spherePoints: array [0..1023] of TVector3;
colorDelta : array [0..1023] of TVector3;
//State
lastModeTicks:double;
attractionMode:byte;
commonTargetColor:TVector3;
commonSaturation,commonHueOffset:TGLfloat;
fireworkGroups:array of array of longint;
lissajousParam:array[0..2] of byte;
Particle: array [0..1023] of TParticle;
gridPoint: array [0..1023] of TIntVec3;
PROCEDURE updateA_cyclic(CONST progress:double);
PROCEDURE updateA_cyclicMirror(CONST progress:double);
PROCEDURE updateA_groupedCyclic(CONST progress:double);
PROCEDURE updateA_cube(CONST progress:double; CONST particleIndex:longint);
PROCEDURE updateA_sphere(CONST progress:double; CONST particleIndex:longint);
PROCEDURE updateA_swirl(CONST progress:double; CONST particleIndex:longint);
PROCEDURE updateA_icosahedron(CONST progress:double; CONST particleIndex:longint);
PROCEDURE updateA_wave(CONST progress:double; CONST particleIndex:longint);
PROCEDURE calculateGridPositions;
PROCEDURE updateA_grid(CONST progress:double; CONST particleIndex:longint);
PROCEDURE updateA_lissajous(CONST progress:double; CONST particleIndex:longint);
PROCEDURE updateA_heart(CONST progress:double; CONST particleIndex:longint);
PROCEDURE updateA_vogler(CONST progress:double; CONST particleIndex:longint);
PROCEDURE updateCol_vogler(CONST progress,dt: double);
PROCEDURE updateA_clock(CONST progress:double; CONST particleIndex:longint);
PROCEDURE updateColors_clock(CONST dt:double);
PROCEDURE updateA_CIRCL(CONST progress:double);
PROCEDURE updateA_clusters(CONST progress:double);
PROCEDURE updateA_sheet(CONST progress:double);
PROCEDURE updateColors_sheet(CONST progress,dt:double);
PROCEDURE updateA_bicyclic(CONST progress:double);
PROCEDURE updateA_sliver(CONST progress: double);
PROCEDURE updateA_byDistance(CONST progress: double);
PROCEDURE lorenzAttractor(CONST dt:double);
PROCEDURE thomasAttractor(CONST dt:double);
PROCEDURE updateA_X1(CONST progress: double);
PROCEDURE updateA_X2(CONST progress: double);
PROCEDURE switchAttractionMode(CONST forcedMode:byte=255);
PROCEDURE MoveParticles(CONST modeTicks:double);
FUNCTION capSubSteps(CONST proposedSubSteps:double; CONST otherProposedSubSteps:double=0):longint;
public
lockCurrentSetup:boolean;
MODE_SWITCH_INTERVAL_IN_TICKS:longint;
TICKS_PER_SIMULATION_TIME_UNIT:double;
CONSTRUCTOR create;
DESTRUCTOR destroy; override;
FUNCTION update(VAR modeTicks:double):single;
PROCEDURE nextSetup(VAR modeTicks:double; CONST forcedMode:byte=255);
PROCEDURE DrawParticles(CONST ParticleList: GLuint; CONST particleRotX,particleRotY:GLfloat);
PROPERTY currentAttractionMode:byte read attractionMode;
FUNCTION loadFromStream(VAR stream:T_bufferedInputStreamWrapper):boolean; virtual;
PROCEDURE saveToStream(VAR stream:T_bufferedOutputStreamWrapper); virtual;
end;
CONST
GRID_TARGET=6;
ICOSAHEDRON_TARGET=15;
CLOCK_TARGET=20;
PYRAMID_TARGET=25;
FIREWORKS_TARGET=26;
C_IcosahedronNodes:array[0..11] of TVector3=(
( 0, 8.50650808352040E-001, 5.25731112119134E-001),
( 5.25731112119134E-001, 0, 8.50650808352040E-001),
( 8.50650808352040E-001, 5.25731112119134E-001, 0),
(-5.25731112119134E-001, 0, 8.50650808352040E-001),
(-8.50650808352040E-001, 5.25731112119134E-001, 0),
( 0,-8.50650808352040E-001, 5.25731112119134E-001),
( 8.50650808352040E-001,-5.25731112119134E-001, 0),
(-8.50650808352040E-001,-5.25731112119134E-001, 0),
( 0, 8.50650808352040E-001,-5.25731112119134E-001),
( 5.25731112119134E-001, 0,-8.50650808352040E-001),
(-5.25731112119134E-001, 0,-8.50650808352040E-001),
( 0,-8.50650808352040E-001,-5.25731112119134E-001));
ATTRACTION_MODE_COUNT=27;
ATTRACTION_MODE_NAME:array[0..ATTRACTION_MODE_COUNT-1] of string=
('Cyclic', //0
'Cube' , //1
'Sphere', //2
'Heart' , //3
'Wave' , //4
'Swirl' , //5
'Grid' , //6
'Rain' , //7
'Lissajous', //8
'Clusters', //9
'Flower', //10
'Grouped Lissajous', //11
'Sliver', //12
'Sheet', //13
'Fountain', //14
'Icosahedron', //15
'Grouped Cyclic', //16
'Lorenz Attractor', //17
'Bi-Cyclic', //18
'By Distance', //19
'Clock', //20
'Thomas Attractor', //21
'Knot 1', //22
'Knot 2', //23
'Mirrored Cyclic', //24
'Pyramid',
'Fireworks');
IMPLEMENTATION
USES math,sysutils,LCLProc;
CONST
WHITE:TVector3=(1,1,1);
FIBFAK=2*pi/sqr((sqrt(5)-1)/2);
FUNCTION accel(CONST v,p,target:TVector3; CONST springConstant,dampingFctor:double):TVector3;
begin result:=(target-p)*springConstant+v*(euklideanNorm(v)*dampingFctor); end;
PROCEDURE TParticleEngine.MoveParticles(CONST modeTicks: double);
PROCEDURE fixBrokenPositions;
FUNCTION anyInvalid(CONST v:TVector3):boolean; inline;
begin
result:=isNan(v[0]) or isInfinite(v[0])
or isNan(v[1]) or isInfinite(v[1])
or isNan(v[2]) or isInfinite(v[2]);
end;
VAR i:longint;
begin
for i:=0 to length(Particle)-1 do with Particle[i] do
if anyInvalid(p) or anyInvalid(v) then begin
p:=spherePoints[i]*5;
v:=ZERO_VECTOR;
a:=ZERO_VECTOR;
color:=WHITE*0.5+colorDelta[i];
end;
end;
VAR dt:double;
fallAndBounceDtSub:double;
fallAndBounceSubSteps:longint;
FUNCTION fallAndBounce(CONST i:longint; CONST speedDamingFactor:double=0.5):boolean;
CONST BLUE :TVector3=(0,0,1);
VAR acc:TVector3;
hitTime:double;
subStep:longint;
begin
result:=false;
for subStep:=1 to fallAndBounceSubSteps do with Particle[i] do begin
acc[0]:=0;
acc[1]:=-1;
acc[2]:=0;
v+=(acc-v*(euklideanNorm(v)*speedDamingFactor))*fallAndBounceDtSub;
if p[1]<=-1 then begin
p[1]:=-1;
v[1]:=1;
color:=BLUE;
result:=true;
end;
if (v[1]<0) and (p[1]+v[1]*fallAndBounceDtSub<=-1) then begin
hitTime:=(-1-p[1])/v[1];
v[1]:=0.9*abs(v[1]);
p[1]:=-1+v[1]*(fallAndBounceDtSub-hitTime);
color:=color*0.7+BLUE*0.3;
result:=true;
end else p+=v*fallAndBounceDtSub;
end;
end;
PROCEDURE moveTowardsTargets(CONST updateA:FUpdateAcceleration; CONST iMax_:longint=1023);
VAR i,imax:longint;
aMax:double=0;
vMax:double=0;
tmp,dtSub:double;
subSteps,k:longint;
begin
if iMax_>=length(Particle)
then imax:=length(Particle)-1
else imax:=iMax_;
updateA(min(1,lastModeTicks/MODE_SWITCH_INTERVAL_IN_TICKS));
for i:=0 to imax do with Particle[i] do begin
tmp:=vectors.sumOfSquares(a);
if (tmp>aMax) and not(isInfinite(tmp)) and not(isNan(tmp)) then aMax:=tmp;
tmp:=vectors.sumOfSquares(v);
if (tmp>vMax) and not(isInfinite(tmp)) and not(isNan(tmp)) then vMax:=tmp;
end;
aMax:=sqrt(aMax);
vMax:=sqrt(vMax);
subSteps:=capSubSteps(dt*sqrt(aMax*500),
dt* vMax*500);
dtSub:=dt/subSteps;
for k:=1 to subSteps do begin
if k>1 then updateA(min(1,(lastModeTicks+dtSub*(k-1)*TICKS_PER_SIMULATION_TIME_UNIT)/MODE_SWITCH_INTERVAL_IN_TICKS));
for i:=0 to imax do with Particle[i] do begin
v+=a*dtSub;
p+=v*dtSub;
end;
end;
for i:=imax+1 to length(Particle)-1 do with Particle[i] do fallAndBounce(i);
end;
PROCEDURE moveTowardsTargetsNoninteracting(CONST updateA:FUpdateAccelerationNoninteracting; CONST iMax_:longint=1023);
VAR i,imax:longint;
dtSub:double;
subSteps,k:longint;
totalSubSteps:int64=0;
begin
if iMax_>=length(Particle)
then imax:=length(Particle)-1
else imax:=iMax_;
for i:=0 to imax do with Particle[i] do begin
updateA(min(1,lastModeTicks/MODE_SWITCH_INTERVAL_IN_TICKS),i);
subSteps:=capSubSteps(dt*sqrt(euklideanNorm(a)*5000),
dt* euklideanNorm(v)* 500);
totalSubSteps+=subSteps;
dtSub:=dt/subSteps;
for k:=1 to subSteps do begin
if k>1 then updateA(min(1,(lastModeTicks+dtSub*(k-1)*TICKS_PER_SIMULATION_TIME_UNIT)/MODE_SWITCH_INTERVAL_IN_TICKS),i);
v+=a*dtSub;
p+=v*dtSub;
end;
end;
for i:=imax+1 to length(Particle)-1 do with Particle[i] do fallAndBounce(i);
end;
PROCEDURE updateColors_rainbow;
VAR tgt:TVector3;
i:longint;
begin
for i:=0 to length(Particle)-1 do with Particle[i] do begin
tgt:=hsvColor(i/length(Particle)+commonHueOffset,commonSaturation,1);
color+=(tgt-color)*(0.1*dt);
end;
end;
PROCEDURE updateColors_commmonTarget;
VAR i:longint;
begin
for i:=0 to length(Particle)-1 do with Particle[i] do
color+=(commonTargetColor+colorDelta[i]-color)*(dt);
end;
PROCEDURE updateColors_grid;
VAR i:longint;
c:TVector3;
begin
for i:=0 to length(Particle)-1 do with Particle[i] do begin
c:=commonTargetColor+colorDelta[i];
if gridPoint[i,lissajousParam[0] mod 3]=0 then c:=WHITE-c;
color+=(c-color)*(0.2*dt);
end;
end;
PROCEDURE updateColors_reds;
begin
commonTargetColor[2]-= commonTargetColor[2]*(0.3*dt);
commonTargetColor[1]-= commonTargetColor[1]*(0.2*dt);
commonTargetColor[0]-=(commonTargetColor[0]-0.5)*(0.1*dt);
updateColors_commmonTarget;
end;
PROCEDURE updateColors_byVerticalVelocity;
VAR tgt:TVector3;
i:longint;
begin
for i:=0 to length(Particle)-1 do with Particle[i] do begin
if v[1]>0 then tgt:=commonTargetColor else tgt:=WHITE-commonTargetColor;
color+=(tgt-color)*(dt);
end;
end;
PROCEDURE updateColors_byRadius;
VAR tgt:TVector3;
i:longint;
begin
for i:=0 to length(Particle)-1 do with Particle[i] do begin
tgt:=hsvColor(euklideanNorm(p)+commonHueOffset,1,1);
color+=(tgt-color)*(dt);
end;
end;
PROCEDURE updateColors_byVelocity;
VAR tgt:TVector3;
vNorm:double;
i:longint;
begin
for i:=0 to length(Particle)-1 do with Particle[i] do begin
vNorm:=5*euklideanNorm(v);
if vNorm<3 then begin
tgt[0]:=vNorm;
tgt[1]:=vNorm-1;
tgt[2]:=vNorm-2;
end else begin
tgt[0]:=1-(vNorm-3)/(vNorm-2);
tgt[1]:=tgt[0];
tgt[2]:=1;
end;
color+=(tgt-color)*(2*dt);
end;
end;
PROCEDURE fallingRain;
VAR i:longint;
begin
for i:=0 to length(Particle)-1 do with Particle[i] do begin
fallAndBounce(i);
if (p[1]<=-0.99) and (abs(v[1])<1E-2) then begin
p[0]:=random+random+random+random+random+random-3;
p[2]:=random+random+random+random+random+random-3;
p[1]:=2;
color:=commonTargetColor+(WHITE-commonTargetColor)*random;
v:=ZERO_VECTOR;
v[1]:=0.5-random;
end;
end;
end;
PROCEDURE fallingFountain;
VAR i:longint;
begin
for i:=0 to length(Particle)-1 do with Particle[i] do begin
fallAndBounce(i);
if (sqr(p[0])+sqr(p[1]+1)+sqr(p[2])<1E-2) then begin
v[1]+=5;
v+=randomOnSphere*0.2;
color:=commonTargetColor+(WHITE-commonTargetColor)*random;
end else if p[1]<=-0.99 then begin
v[0]-=dt*p[0];
v[2]-=dt*p[2];
end;
end;
end;
PROCEDURE fallingPyramid;
VAR i,i0,i1:longint;
begin
i0:=round(length(Particle)*lastModeTicks*2/MODE_SWITCH_INTERVAL_IN_TICKS);
i1:=round(length(Particle)*modeTicks *2/MODE_SWITCH_INTERVAL_IN_TICKS);
for i:=0 to length(Particle)-1 do with Particle[i] do begin
if max(abs(p[2]),abs(p[0]))<=1 then begin
p[1]+=max(abs(p[2]),abs(p[0]))-1;
if fallAndBounce(i) then begin
color:=WHITE-commonTargetColor;
v*=0.1;
end;
p[1]-=max(abs(p[2]),abs(p[0]))-1;
end else fallAndBounce(i);
if (i>=i0) and (i<=i1) then begin
//if (p[1]<=-0.99) and (abs(v[1])<1E-2) then begin
p[0]:=1-2*random;
p[2]:=1-2*random;
if random<0.2 then begin
if random<0.5
then p[2]:=-p[0]
else p[2]:=p[0];
end else begin
if abs(p[0])>abs(p[2])
then p[0]:=0.1*round(10*p[0])
else p[2]:=0.1*round(10*p[2]);
end;
p[1]:=2;
color:=commonTargetColor+(WHITE-commonTargetColor)*random;
v:=ZERO_VECTOR;
v[1]:=0.5-random;
end;
end;
end;
PROCEDURE fireworks;
CONST fallAcceleration:TVector3=(0,-1,0);
MAX_PARTICLES_IN_GROUP=256;
FUNCTION formNewFallingGroup:boolean;
VAR k,i,groupIndex:longint;
startingPoint:TVector3;
begin
groupIndex:=length(fireworkGroups);
setLength(fireworkGroups,groupIndex+1);
setLength(fireworkGroups[groupIndex],MAX_PARTICLES_IN_GROUP);
result:=false;
k:=0;
for i:=0 to length(Particle)-1 do if (Particle[i].p[1]<-1) and (k<MAX_PARTICLES_IN_GROUP) then begin
fireworkGroups[groupIndex][k]:=i;
inc(k);
result:=true;
end;
setLength(fireworkGroups[groupIndex],k);
if result then begin
startingPoint:=vectorOf((random+random+random+random+random+random-3)/2,
-1,
(random+random+random+random+random+random-3)/2);
for i in fireworkGroups[groupIndex] do with Particle[i] do begin
p:=startingPoint;
v:=ZERO_VECTOR;
color:=WHITE;
end;
end else setLength(fireworkGroups,groupIndex);
end;
PROCEDURE explodeGroup(CONST groupIndex:longint);
VAR a,b,v0Normed,dv:TVector3;
k:longint;
j:longint=0;
jMax:longint;
starDiv:longint;
color:TVector3;
explodeMode:byte;
begin
a:=randomOnSphere;
b:=cross(a,randomInSphere); b*=1/euklideanNorm(b);
v0Normed:=Particle[fireworkGroups[groupIndex][0]].v;
v0Normed*=1/euklideanNorm(v0Normed);
jMax:=length(fireworkGroups[groupIndex]);
starDiv:=max(3,min(17,trunc(jMax/5)));
color:=hsvColor(random,1,2);
explodeMode:=random(5);
for k in fireworkGroups[groupIndex] do begin
case explodeMode of
0: dv:=(a*sin(2*pi/starDiv*j)
+b*cos(2*pi/starDiv*j)
+v0Normed)*random;
1: begin
dv:=randomOnSphere;
dv-=a*(dv*a);
end;
2: dv:=randomOnSphere;
3: dv:=C_IcosahedronNodes[j mod length(C_IcosahedronNodes)]*(j/jMax);
4: dv:=a*sin(2*pi/jMax*j)
+b*cos(2*pi/jMax*j);
end;
Particle[k].v+=dv;
Particle[k].color:=color;
inc(j);
end;
setLength(fireworkGroups[groupIndex],0);
end;
VAR setupTime:double;
j:longint=0;
i:longint;
freeFalling:array[0..length(Particle)-1] of boolean;
begin
setupTime:=modeTicks/TICKS_PER_SIMULATION_TIME_UNIT;
if (setupTime-commonSaturation>0.5) and formNewFallingGroup then commonSaturation:=setupTime+random;
for i:=0 to length(fireworkGroups)-1 do begin
if Particle[fireworkGroups[i][0]].p[1]>random
then explodeGroup(i)
else begin fireworkGroups[j]:=fireworkGroups[i]; inc(j); end;
end;
setLength(fireworkGroups,j);
for i:=0 to length(Particle)-1 do freeFalling[i]:=true;
for j:=0 to length(fireworkGroups)-1 do for i in fireworkGroups[j] do with Particle[i] do begin
v-=fallAcceleration*dt;
p+=v*dt;
freeFalling[i]:=false;
end;
for i:=0 to length(Particle)-1 do if freeFalling[i] then with Particle[i] do begin
v+=(fallAcceleration-v*(2*vectors.sumOfSquares(v))) *dt;
p+=v*dt;
color-=color*(dt*0.5);
end;
end;
begin
fixBrokenPositions;
dt:=(modeTicks-lastModeTicks)/TICKS_PER_SIMULATION_TIME_UNIT;
fallAndBounceSubSteps:=capSubSteps(dt*dt*1000);
fallAndBounceDtSub:=dt/fallAndBounceSubSteps;
case attractionMode of
0: begin
moveTowardsTargets(@updateA_cyclic);
updateColors_rainbow;
end;
1: begin
moveTowardsTargetsNoninteracting(@updateA_cube,round(length(Particle)*modeTicks/MODE_SWITCH_INTERVAL_IN_TICKS*2));
updateColors_commmonTarget;
end;
2: begin
moveTowardsTargetsNoninteracting(@updateA_sphere,round(length(Particle)*modeTicks/MODE_SWITCH_INTERVAL_IN_TICKS*2));
updateColors_commmonTarget;
end;
3: begin
moveTowardsTargetsNoninteracting(@updateA_heart);
updateColors_reds;
end;
4: begin
moveTowardsTargetsNoninteracting(@updateA_wave);
updateColors_byVerticalVelocity;
end;
5: begin
moveTowardsTargetsNoninteracting(@updateA_swirl);
updateColors_byVelocity;
end;
GRID_TARGET: begin
moveTowardsTargetsNoninteracting(@updateA_grid);
updateColors_grid;
end;
7: fallingRain;
8: begin
moveTowardsTargetsNoninteracting(@updateA_lissajous);
updateColors_rainbow;
end;
9: begin
moveTowardsTargets(@updateA_clusters);
updateColors_byVelocity;
end;
10: begin
moveTowardsTargetsNoninteracting(@updateA_vogler);
updateCol_vogler(min(1,modeTicks/MODE_SWITCH_INTERVAL_IN_TICKS), dt);
end;
11: begin
moveTowardsTargets(@updateA_CIRCL);
updateColors_rainbow;
end;
12: begin
moveTowardsTargets(@updateA_sliver,round(length(Particle)*modeTicks/MODE_SWITCH_INTERVAL_IN_TICKS*2));
updateColors_byRadius;
end;
13: begin
moveTowardsTargets(@updateA_sheet);
updateColors_sheet(modeTicks/MODE_SWITCH_INTERVAL_IN_TICKS,dt);
end;
14: fallingFountain;
ICOSAHEDRON_TARGET: begin
moveTowardsTargetsNoninteracting(@updateA_icosahedron);//,round(length(Particle)*modeTicks/MODE_SWITCH_INTERVAL_IN_TICKS*2));
updateColors_byRadius;
end;
16: begin
moveTowardsTargets(@updateA_groupedCyclic);
updateColors_byVelocity;
end;
17: lorenzAttractor(dt);
18: begin
moveTowardsTargets(@updateA_bicyclic);
updateColors_byVerticalVelocity;
end;
19: begin
moveTowardsTargets(@updateA_byDistance);
//updateColors_byRadius;
updateColors_rainbow;
end;
CLOCK_TARGET: begin
moveTowardsTargetsNoninteracting(@updateA_clock);
updateColors_clock(dt);
end;
21: thomasAttractor(dt);
22: begin
moveTowardsTargets(@updateA_X1);
updateColors_byVelocity;
end;
23: begin
moveTowardsTargets(@updateA_X2);
updateColors_byVerticalVelocity;
end;
24: begin
moveTowardsTargets(@updateA_cyclicMirror);
updateColors_byRadius;
end;
PYRAMID_TARGET: fallingPyramid;
FIREWORKS_TARGET: fireworks;
end;
lastModeTicks:=modeTicks;
end;
FUNCTION TParticleEngine.capSubSteps(CONST proposedSubSteps:double; CONST otherProposedSubSteps:double=0): longint;
CONST HARD_UPPER_BOUND=500;
HARD_LOWER_BOUND=1;
begin
if proposedSubSteps<HARD_LOWER_BOUND then result:=HARD_LOWER_BOUND
else if (proposedSubSteps>HARD_UPPER_BOUND) or (otherProposedSubSteps>HARD_UPPER_BOUND)
then result:=HARD_UPPER_BOUND
else result:=ceil(max(proposedSubSteps,otherProposedSubSteps));
end;
PROCEDURE TParticleEngine.updateA_cyclic(CONST progress: double);
VAR i,k:longint;
r:double;
targetPosition:TVector3;
begin
for i:=0 to length(Particle)-1 do with Particle[i] do begin
k:=(i+1) mod length(Particle);
targetPosition:=Particle[k].p+Particle[k].v;
r:=euklideanNorm(targetPosition);
if r<0.5 then targetPosition*=0.5/r
else if r>2 then targetPosition*=2 /r;
a:=accel(v,p,targetPosition,50,-40)-p*1E-1;
end;
end;
PROCEDURE TParticleEngine.updateA_cyclicMirror(CONST progress: double);
CONST half=512;
FUNCTION mirrored(CONST vec:TVector3):TVector3;
begin
result:=vec;
result[1]:=-result[1];
end;
VAR i,k,modul:longint;
targetPosition:TVector3;
begin
modul:=1024;
if progress>0.2 then modul:=modul shr 1;
if progress>0.35 then modul:=modul shr 1;
if progress>0.5 then modul:=modul shr 1;
if progress>0.65 then modul:=modul shr 1;
if progress>0.8 then modul:=modul shr 1;
modul-=1;
for i:=0 to half-1 do with Particle[i] do begin
k:=((i+1) and modul) or (i and not(modul));
targetPosition:=Particle[k].p+Particle[k].v;
a:=accel(v,p,targetPosition,50,-50);
if sqr(p[0])+sqr(p[1])+sqr(p[2])>1 then a-=p;
Particle[i+half].p:=mirrored(p);
Particle[i+half].v:=mirrored(v);
Particle[i+half].a:=mirrored(a);
end;
end;
PROCEDURE TParticleEngine.updateA_groupedCyclic(CONST progress: double);
VAR i,k:longint;
targetPosition, allPointsCenter:TVector3;
begin
allPointsCenter:=ZERO_VECTOR;
for k:=0 to length(Particle)-1 do allPointsCenter+=Particle[k].p;
allPointsCenter*=1/length(Particle);
for i:=0 to length(Particle)-1 do begin
k:=((i+(i shr 5)+round(progress*100)) and 31) or (i and not(31));
with Particle[i] do begin
targetPosition:=Particle[k].p-allPointsCenter;
targetPosition*=1/euklideanNorm(targetPosition);
a:=accel(v,p,targetPosition,100,-10);
end;
end;
end;
PROCEDURE TParticleEngine.updateA_cube(CONST progress: double;
CONST particleIndex: longint);
CONST C_CubeNodes:array[0..7] of TVector3=((-1,-1,-1),(-1,-1,1),(1,-1,-1),(1,-1,1),(-1,1,-1),(-1,1,1),(1,1,-1),(1,1,1));
C_cubeTrip1:array[0..7] of longint=(0,1,3,2,6,7,5,4);
C_cubeTrip2:array[0..7] of longint=(0,2,6,4,5,7,3,1);
C_cubeTrip3:array[0..7] of longint=(0,4,5,1,3,7,6,2);
VAR k:longint;
tau:double;
targetPosition:TVector3;
subCube:byte;
begin
with Particle[particleIndex] do begin
subCube:=trunc(particleIndex/length(Particle)*3);
tau:=particleIndex/length(Particle)*24+progress*2;
k:=trunc(tau);
tau:=frac(tau);
case subCube of
0: targetPosition:=(C_CubeNodes[C_cubeTrip1[ k and 7]]*(1-tau)
+C_CubeNodes[C_cubeTrip1[(k+1) and 7]]* tau)*0.8;
1: targetPosition:=(C_CubeNodes[C_cubeTrip2[ k and 7]]*(1-tau)
+C_CubeNodes[C_cubeTrip2[(k+1) and 7]]* tau)*0.9;
else targetPosition:=(C_CubeNodes[C_cubeTrip3[ k and 7]]*(1-tau)
+C_CubeNodes[C_cubeTrip3[(k+1) and 7]]* tau);
end;
a:=accel(v,p,targetPosition,20,-20);
end;
end;
PROCEDURE TParticleEngine.updateA_heart(CONST progress: double;
CONST particleIndex: longint);
VAR tau,pr:double;
tp :TVector3;
begin
pr:=min(1,progress*1.2);
with Particle[particleIndex] do begin
tau:=(particleIndex/length(Particle)+progress)*2*pi;
tp [2]:=pr*0.75 *sin(tau)-0.25 *sin(3*tau);
tp [1]:=pr*0.8125*cos(tau)-0.3125*cos(2*tau)-0.125*cos(3*tau)-0.0625*cos(4*tau);
tp [0]:=0;
a:=accel(v,p,tp,10+40*pr,-20*pr)
end;
end;
PROCEDURE TParticleEngine.updateA_sphere(CONST progress: double;
CONST particleIndex: longint);
begin
with Particle[particleIndex] do a:=accel(v,p,spherePoints[particleIndex]*progress,10,-5);
end;
PROCEDURE TParticleEngine.updateA_swirl(CONST progress: double;
CONST particleIndex: longint);
FUNCTION accel(CONST v,p:TVector3):TVector3;
VAR q:TVector3;
f:double;
r:double;
begin
q:=p;
q[1]+=0.5;
r:=euklideanNorm(q);
f:=exp(-8*sqr(q[1]));
result:=q*(-1/r);
result[0]+=0.1*q[2]/r;
result[2]-=0.1*q[0]/r;
result-=v*(f*euklideanNorm(v));//*2*(exp(-8*sqr(p[2]))));
result[1]*=2;
end;
CONST r0 =0.08;
VAR angle:double;
begin
with Particle[particleIndex] do begin
a:=accel(v,p);
if (sqr(p[0])+sqr(p[1]+0.5)+sqr(p[2]))<sqr(r0) then begin
v[1]:=5*random;
angle:=random*2*pi;
v[2]:=v[1]*0.1*sin(angle);
v[0]:=v[1]*0.1*cos(angle);
p:=v*(r0/euklideanNorm(v));
p[1]-=0.5;
color:=WHITE;
end;
end;
end;
PROCEDURE TParticleEngine.updateA_icosahedron(CONST progress: double; CONST particleIndex: longint);
CONST C_IcosahedronEdges:array[0..29,0..1] of byte=
((8,10),
(5,7),
(2,9),
(1,6),
(3,5),(5,1),(1,3),(3,0),(0,2),(2,8),(8,0),(0,4),
(0,1),(1,2),(2,6),(6,9),(9,10),(10,11),(11,5),(5,6),(6,11),(11,7),(7,10),(10,4),(4,7),(7,3),(3,4),(4,8),(8,9),(9,11));
VAR k:longint;
tau,h:double;
targetPosition,d:TVector3;
begin
h:=-1+3*progress;
d:=C_IcosahedronNodes[lissajousParam[0]];
with Particle[particleIndex] do begin
tau:=(particleIndex/length(Particle));
tau*=length(C_IcosahedronEdges);
k:=trunc(tau);
tau:=frac(tau);
k:=k mod length(C_IcosahedronEdges);
targetPosition:=C_IcosahedronNodes[C_IcosahedronEdges[k,0]]*(1-tau)+
C_IcosahedronNodes[C_IcosahedronEdges[k,1]]*( tau);
tau:=targetPosition*d;
if tau>h then targetPosition-=d*(tau-h);
a:=accel(v,p,targetPosition,30,-30);
end;
end;
PROCEDURE TParticleEngine.updateA_wave(CONST progress: double;
CONST particleIndex: longint);
VAR r:double;
targetPosition:TVector3;
begin
with Particle[particleIndex] do begin
r:=sqrt(particleIndex/1023)*2;
targetPosition[0]:=sin(particleIndex*FIBFAK)*r;
targetPosition[2]:=cos(particleIndex*FIBFAK)*r;
targetPosition[1]:=-0.5+0.2*sin(3*r-progress*50);
a:=accel(v,p,targetPosition,10,-10);
end;
end;
CONST GRID_SIZE=0.2;
INV_GRID_SIZE=1/GRID_SIZE;
PROCEDURE TParticleEngine.calculateGridPositions;
CONST OCC_RAD=20;
VAR occupied:bitpacked array[-OCC_RAD..OCC_RAD,-OCC_RAD..OCC_RAD,-OCC_RAD..OCC_RAD] of boolean;
PROCEDURE clearOccupied;
VAR i,j,k:longint;
begin
for i:=-OCC_RAD to OCC_RAD do
for j:=-OCC_RAD to OCC_RAD do
for k:=-OCC_RAD to OCC_RAD do occupied[i,j,k]:=false;
end;
PROCEDURE markAsOccupied(CONST k:TIntVec3);
begin
if (k[0]>=-OCC_RAD) and (k[0]<=OCC_RAD) and
(k[1]>=-OCC_RAD) and (k[1]<=OCC_RAD) and
(k[2]>=-OCC_RAD) and (k[2]<=OCC_RAD) then occupied[k[0],k[1],k[2]]:=true;
end;
FUNCTION isOccupied(CONST k:TIntVec3):boolean;
begin
result:=(k[0]>=-OCC_RAD) and (k[0]<=OCC_RAD) and
(k[1]>=-OCC_RAD) and (k[1]<=OCC_RAD) and
(k[2]>=-OCC_RAD) and (k[2]<=OCC_RAD) and
occupied[k[0],k[1],k[2]];
end;
FUNCTION findVacantSpot(CONST k:TIntVec3):TIntVec3;
VAR radius:longint=0;
di,dj,dk:longint;
begin
while true do begin
for di:=-radius to radius do
for dj:=-radius to radius do
for dk:=-radius to radius do
if max(max(abs(di),abs(dj)),abs(dk))=radius then begin
result:=k;
result[0]+=di;
result[1]+=dj;
result[2]+=dk;
if not(isOccupied(result)) then exit(result);
end;
inc(radius);
end;
end;
VAR i:longint;
begin
clearOccupied;
for i:=0 to length(Particle)-1 do begin
gridPoint[i]:=findVacantSpot(roundVector(Particle[i].p*INV_GRID_SIZE));
markAsOccupied(gridPoint[i]);
end;
end;
PROCEDURE TParticleEngine.updateA_grid(CONST progress: double;
CONST particleIndex: longint);
begin
with Particle[particleIndex] do a:=accel(v,p,gridPoint[particleIndex]*GRID_SIZE,5,-20);
end;
PROCEDURE TParticleEngine.updateA_lissajous(CONST progress: double;
CONST particleIndex: longint);
VAR t,tau:double;
targetPosition:TVector3;
begin
t:=sqr(progress);
with Particle[particleIndex] do begin
tau:=(particleIndex/length(Particle)-t)*2*pi;
targetPosition[0]:=sin( lissajousParam[0]*tau);
targetPosition[1]:=sin(pi/6+lissajousParam[1]*tau);
targetPosition[2]:=sin(pi/3+lissajousParam[2]*tau);
a:=accel(v,p,targetPosition,20,-2);
end;
end;
PROCEDURE TParticleEngine.updateA_vogler(CONST progress: double;
CONST particleIndex: longint);
VAR k:longint;
targetPosition:TVector3;
refY:double;
downcurve:double;
radius:double;
begin
k:=trunc(progress*20);
downcurve:=0.4*k/20;
radius :=1/30*k/20;
k:=((k div 2)*21+13*(k and 1));
refY:=k*0.01-1;
with Particle[particleIndex] do begin
if particleIndex-k>0 then begin
targetPosition[0]:=sin((particleIndex-k)*FIBFAK)*sqrt((particleIndex-k))*radius;
targetPosition[2]:=cos((particleIndex-k)*FIBFAK)*sqrt((particleIndex-k))*radius;
targetPosition[1]:=refY-downcurve*sqr((particleIndex-k)/1023);
end else begin
targetPosition[0]:=0;
targetPosition[2]:=0;
if odd(particleIndex)
then targetPosition[1]:=refY+(particleIndex-k)*0.01
else begin
targetPosition[1]:=sin((1+refY+(particleIndex-k)*0.01)*0.5)-1+k*0.003;
targetPosition[2]:=cos((1+refY+(particleIndex-k)*0.01)*0.5)-1;
end;
end;
a:=accel(v,p,targetPosition,10,-10);
end;
end;
PROCEDURE TParticleEngine.updateCol_vogler(CONST progress, dt: double);
CONST GREEN:TVector3=(0,0.5,0);
ORANGE:TVector3=(1,0.5,0);
WHITE:TVector3=(0.8,0.9,1);
VAR i,k:longint;
begin
k:=trunc(progress*20);
k:=((k div 2)*21+13*(k and 1));
for i:=0 to length(Particle)-1 do with Particle[i] do begin
if i-k>0 then begin
if i-k>100 then color+=(WHITE-color)*(0.1*dt)
else color+=(ORANGE-color)*(0.2*dt);
end else
color+=(GREEN-color)*(dt);
end;
end;
PROCEDURE TParticleEngine.updateA_CIRCL(CONST progress: double);
CONST spring=20;
ATTENUATION=-5;
VAR i,k:longint;
tau:double;
targetPosition:TVector3;
begin
k:=0;
while k<length(Particle) do begin
tau:=(k/length(Particle)+progress)*2*pi;
targetPosition[0]:=sin( lissajousParam[0]*tau);
targetPosition[1]:=sin(pi/6+lissajousParam[1]*tau);
targetPosition[2]:=sin(pi/3+lissajousParam[2]*tau);
with Particle[k] do a:=accel(v,p,targetPosition,spring,ATTENUATION);
for i:=k+1 to k+31 do with Particle[i] do begin
targetPosition:=Particle[i-1].p*0.15+targetPosition*0.85;
a:=accel(v,p,targetPosition,spring,ATTENUATION)
end;
k+=32
end;
end;
CONST C_clusterChunk:array[0..12] of longint=(0,85,171,256,341,427,512,597,683,768,853,939,1024);
PROCEDURE TParticleEngine.updateA_clusters(CONST progress: double);
VAR i,k:longint;
tgt:TVector3;
r,spring:double;
begin
for k:=0 to 11 do begin
tgt:=C_IcosahedronNodes[k];
spring:=5;
with Particle[C_clusterChunk[k]] do begin
a:=tgt-p;
r:=euklideanNorm(a);
a:=a*(spring/r)-v*5;
tgt:=p*0.4+tgt*0.6;
end;
for i:=C_clusterChunk[k]+1 to C_clusterChunk[k+1]-1 do with Particle[i] do begin
spring*=0.999;
a:=tgt-p;
r:=euklideanNorm(a);
a:=a*(spring/r)-v*5;
tgt:=p*0.4+tgt*0.6;
end;
end;
end;
PROCEDURE TParticleEngine.updateA_sheet(CONST progress: double);
VAR ix,iy,k,n:longint;
allPointsCenter, targetPosition:TVector3;
begin
allPointsCenter:=ZERO_VECTOR;
for k:=0 to length(Particle)-1 do allPointsCenter+=Particle[k].p;
allPointsCenter*=1/length(Particle);
for ix:=0 to 31 do
for iy:=0 to 31 do with Particle[ix shl 5 or iy] do begin
targetPosition:=ZERO_VECTOR; n:=0;
if ix> 0 then begin targetPosition+=Particle[(ix-1) shl 5 or iy].p; n+=1; end;
if ix<31 then begin targetPosition+=Particle[(ix+1) shl 5 or iy].p; n+=1; end;
if iy> 0 then begin targetPosition+=Particle[ix shl 5 or (iy-1)].p; n+=1; end;
if iy<31 then begin targetPosition+=Particle[ix shl 5 or (iy+1)].p; n+=1; end;
targetPosition:=targetPosition*(1/n);
a:=accel(v,p,targetPosition,40,-2);
if n<4 then begin
targetPosition:=p-allPointsCenter;