@@ -6,7 +6,7 @@ local back={}
66
77local qX ,qY ,qdX ,qdY = {},{},{},{} -- quark data in SoA [size, X, Y, dx, dy, color]
88
9- local ptcclr = {{1 ,.26 , .26 },{.26 ,1 ,.26 },{.26 , .26 ,1 }}
9+ local ptcclr = {{1 ,0 , 0 },{0 ,1 ,0 },{0 , 0 ,1 }}
1010local apcclr = {{0 ,1 ,1 },{1 ,0 ,1 },{1 ,1 ,0 }}
1111
1212local blasts = {} -- data about annihilation blasts from particles and antiparticles colliding
@@ -16,7 +16,6 @@ local nextpair
1616local W ,H ,size
1717local quarkCount = 400
1818
19- local x ,y ,cutoff -- temp vars for optimization
2019local function spawnQuarkRandom (i )
2120 qX [i ]= rnd (W )- 10 -- X
2221 qY [i ]= rnd (H )- 10 -- Y
@@ -44,16 +43,23 @@ local function spawnQuarkEdge(i)
4443 qdY [i ]= sin (theta )* 300 -- dy
4544end
4645local function spawnParticlePair ()
47- x ,y = rnd (W )- 10 ,rnd (H )- 10
48- ptc [# ptc + 1 ]= {x = x ,y = y ,dist = 0 ,theta = rnd ()* MATH .tau ,v = 500 , c = rnd (3 )}
46+ ptc [# ptc + 1 ]= {
47+ x = rnd (W )- 10 ,
48+ y = rnd (H )- 10 ,
49+ dist = 0 ,
50+ theta = rnd ()* MATH .tau ,
51+ v = 500 ,
52+ c = rnd (3 ),
53+ }
54+ end
55+ local function spawnBlast (_x ,_y )
56+ blasts [# blasts + 1 ]= {x = _x ,y = _y ,t = 0 }
4957end
50- local function spawnBlast (_x ,_y ) blasts [# blasts + 1 ]= {x = _x ,y = _y ,t = 0 } end
5158
5259function back .init ()
5360 qX ,qY ,qdX ,qdY = {},{},{},{}
5461 blasts = {}
5562 ptc = {}
56- cutoff = {ceil (quarkCount / 3 ),ceil (2 * quarkCount / 3 )}
5763 nextpair = 0
5864 BG .resize (SCR .w ,SCR .h )
5965end
@@ -63,7 +69,7 @@ function back.resize(w,h)
6369 size = 2.6 * SCR .k
6470end
6571function back .update (dt )
66- -- Move far-away quarks
72+ -- Move far-away quarks
6773 for i = 1 ,quarkCount do
6874 qX [i ]= qX [i ]+ qdX [i ]* dt
6975 qY [i ]= qY [i ]+ qdY [i ]* dt
@@ -72,22 +78,22 @@ function back.update(dt)
7278 end
7379 end
7480
75- -- Particle pair attraction & destruction
81+ -- Particle pair attraction & destruction
7682 for i =# ptc ,1 ,- 1 do
7783 local p = ptc [i ]
7884 if p then
7985 p .dist = p .dist + p .v * dt
8086 p .v = p .v - p .dist ^ 2 * dt
8187
82- -- Destroy colliding particle pairs
88+ -- Destroy colliding particle pairs
8389 if p .dist <= 10 and p .v <= 0 then
8490 spawnBlast (p .x ,p .y )
8591 table.remove (ptc ,i )
8692 end
8793 end
8894 end
8995
90- -- Age blasts, delete old blasts
96+ -- Age blasts, delete old blasts
9197 for i =# blasts ,1 ,- 1 do
9298 if blasts [i ] then
9399 blasts [i ].t = blasts [i ].t + dt
@@ -97,28 +103,22 @@ function back.update(dt)
97103 end
98104 end
99105
100- -- Spawn particle pairs
106+ -- Spawn particle pairs
101107 nextpair = nextpair - dt
102108 if nextpair <= 0 then
103109 spawnParticlePair ()
104110 nextpair = rnd ()* 4
105111 end
106112end
107113function back .draw ()
108- gc .clear (0 .08 ,0 .04 ,0 .01 )
114+ gc .clear (.08 ,.04 ,.01 )
109115 translate (- 10 ,- 10 )
110- setColor (1 ,0 ,0 ,.8 )
111- for i = 1 ,cutoff [1 ] do -- draw red quarks
112- circle (' fill' ,qX [i ],qY [i ],size )
113- end
114- setColor (0 ,1 ,0 ,.8 )
115- for i = cutoff [1 ]+ 1 ,cutoff [2 ] do -- draw green quarks
116- circle (' fill' ,qX [i ],qY [i ],size )
117- end
118- setColor (0 ,0 ,1 ,.8 )
119- for i = cutoff [2 ]+ 1 ,quarkCount do -- draw blue quarks
120- circle (' fill' ,qX [i ],qY [i ],size )
121- end
116+
117+ -- Draw quarks in R/G/B
118+ setColor (1 ,0 ,0 ,.8 ) for i = 1 , math.floor (quarkCount / 3 ) do circle (' fill' ,qX [i ],qY [i ],size ) end
119+ setColor (0 ,1 ,0 ,.8 ) for i = math.floor (quarkCount / 3 )+ 1 , math.floor (quarkCount * 2 / 3 ) do circle (' fill' ,qX [i ],qY [i ],size ) end
120+ setColor (0 ,0 ,1 ,.8 ) for i = math.floor (quarkCount * 2 / 3 )+ 1 ,quarkCount do circle (' fill' ,qX [i ],qY [i ],size ) end
121+
122122 for i = 1 ,# ptc do
123123 local p = ptc [i ]
124124 push ()
0 commit comments