Particle Gravity

Miscellaneous Forums/Blitz Showcase/Particle Gravity

Here is the code of my particle-gravity program.
Copy it and try out!!

With "anzahl" you can change the number of particles,
speed and startspeed should be clear,
and with barriere you can change the behaviour of particles, when they collide.

Please ask me questions if you want to.
And if you have ideas to improve it, tell them to me.
Every kind of feedback is welcome!!


Graphics 1024,768

speed# = 2
startspeed# = 1
zuf# = 0
abstandsfaktor = 2
anzahl = 130
barriere = 0.1

Global partx#[anzahl]
Global party#[anzahl]
Global partxs#[anzahl]
Global partys#[anzahl]
Global rot[anzahl]
Global gruen[anzahl]
Global blau[anzahl]

For x = 0 To (anzahl-1)

partx[x] = Rnd(0,1024)
party[x] = Rnd(0,768)

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]
party[x] = party[x] + partys[x]

SetColor rot[x],gruen[x],blau[x]
DrawRect  (partx[x]-(0)),(party[x]-(0)),2,2

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)

If partx[x] < partx[y] Then
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

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

partxs[x] = durchx*0.1
partys[x] = durchy*0.1

End If
End If

Next
Next

If MouseDown (1) Then

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
DrawText yspeed,MouseX(),MouseY()+25

xspeed = 0
yspeed = 0

End If

Flip

If Not KeyDown(key_space)
Cls
End If

Until KeyHit (key_escape)

End 


Sorry, it's not structured very well *lol*

[ CODEBOX ] [ /CODEBOX ] :-)

Hi, ok , here's another version of the program ( a bit better, I think).
But has anyone an idea how to hold the particles on the screen?

Graphics 1024,768

speed# = 1                         
startspeed# = 1
zuf# = 0
abstandsfaktor =1
anzahl = 100
barriere = 0.5

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,1024)
party[x] = Rnd(0,768)

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)),(party[x]-(dys#-250)),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)   ' 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


Flip


If Not KeyDown(key_space)               
Cls
End If

Until KeyHit (key_escape)

End        ' end *g*



Please help me to improve it! *g*


thx

Lol, lets work on that a bit ;)

1) Edit and blank out the above post.
2) Add those tags to the original post, removing the spaces :)

I can't check the code right now as i'm at work, but do the above and you'll get more replies :)

Quite a neat code snippet :) Good Work.

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*