Particle Gravity

Miscellaneous Forums/Blitz Showcase/Particle Gravity

Well,
I've made another change.
It should be more realistic, because now the distance
counts exponetial ( I am not good in physics, but it should be right).
So the particles build nice constellations, which you can change by changing the parameters.
Try out, and you know, if you can do this better, please show it to me!

Graphics 1024,768

speed# = 3                      
startspeed# =.1
zuf# = 0
abstandsfaktor =1
anzahl = 120
barriere = 0.1

Global partx#[anzahl]                    ' Declaration of positions of the particles
Global party#[anzahl]
Global partxs#[anzahl]
Global partys#[anzahl]

Global rot[anzahl]                       ' Declaration of color of the particles
Global gruen[anzahl]
Global blau[anzahl]


For x = 0 To (anzahl-1)                 ' Set particles position and color

partx[x] = Rnd(0,150)
party[x] = Rnd(0,150)

rot[x] = Rnd (1,255)

gruen[x] = Rnd (1,100)
blau[x] = Rnd (1,255)

partxs[x] = Rnd(-startspeed,startspeed)
partys[x] = Rnd(-startspeed,startspeed)


Next


Repeat

dxsv# = 0
dysv#= 0


For x = 0 To (anzahl-1)


partx[x] =partx[x] + partxs[x]                              ' Move particles
party[x] = party[x] + partys[x]

SetColor rot[x],gruen[x],blau[x]


DrawRect  (partx[x]-(dxs#-350-dax)),(party[x]-(dys#-250-day)),2,2       ' Draw them(dxs and dys shal hold them in the middle of the screen, but it doesn't work)


dxsv = dxsv + partx[x]
dysv = dysv + party[x]

SetColor 255,255,255

Next

dxs# = (dxsv/anzahl)
dys# = (dysv/anzahl)

For x = 0 To (anzahl-1)
For y = 0 To (anzahl-1)

abstand# = abstandsfaktor*(((partx[x]-partx[y])^2+(party[x]-party[y])^2)^.5)^2   ' Get the distance between particle[x] and particle[y]


If partx[x] < partx[y] Then                                              ' change it's acceleration because of gravity 
partxs[x] = partxs[x] +(.speed * (1/abstand)*Rnd(1-zuf,1+zuf))
End If
If partx[x] > partx[y] Then
partxs[x] = partxs[x] -(.speed * (1/abstand)*Rnd(1-zuf,1+zuf))
End If

If party[x] < party[y] Then
partys[x] = partys[x] +(.speed * (1/abstand)*Rnd(1-zuf,1+zuf))
End If
If party[x] > party[y] Then
partys[x] = partys[x] -(.speed * (1/abstand)*Rnd(1-zuf,1+zuf))
End If


If y <> x Then
If Abs(partx[x]-partx[y]) < 2 And Abs (party[x]-party[y]) < 2 Then       'particle-collision

durchx = (partxs[x]+partxs[y]) /2
durchy = (partys[x]+partys[y]) /2

If Abs(partxs[x]-partxs[y])>(speed*5) Or Abs (partys[x] - partys[y]) >(speed*5) Then ' change it's color when they collide
If rot[x] + 5< 255 Then
rot[x] = rot[x] +  5
End If
If gruen[x] + 2 < 210 Then
gruen[x] = gruen[x] + 2
End If
If blau[x] - 2 > 0 Then
blau[x] = blau[x] - 2
End If
End If


partxs[x] = partxs[y]
partys[x] = partys[y]

End If
End If

Next
Next


If MouseDown (1) Then                    ' show the gravity(x,y) on a certain point (I think it doesn't work *g*)

For x = 0 To (anzahl-1)
For y = 0 To (anzahl-1)

abstand# = abstandsfaktor*(((MouseX()-partx[y])^2+(MouseY()-party[y])^2)^.5)

If MouseX() < partx[y] Then
xspeed# = xspeed +(.speed * (1/abstand)*Rnd(1-zuf,1+zuf))
End If
If partx[x] > partx[y] Then
xspeed = xspeed -(.speed * (1/abstand)*Rnd(1-zuf,1+zuf))
End If

If party[x] < party[y] Then
yspeed# = yspeed +(.speed * (1/abstand)*Rnd(1-zuf,1+zuf))
End If
If party[x] > party[y] Then
yspeed = yspeed -(.speed * (1/abstand)*Rnd(1-zuf,1+zuf))
End If


Next
Next

DrawText xspeed,MouseX(),MouseY()+15         ' Draw this gravity 
DrawText yspeed,MouseX(),MouseY()+25

xspeed = 0
yspeed = 0

End If


If KeyDown (key_right) Then dax = dax - 10
If KeyDown (key_left) Then dax = dax + 10
If KeyDown (key_up) Then day = day + 10
If KeyDown (key_down) Then day = day - 10



Flip


If Not KeyDown(key_space)               
Cls
End If

Until KeyHit (key_escape)

End        ' end *g*