Basic4GL Particles

bmatthew1 14 May 2007 21:50

At the moment this Programme is just a copy of the Original, I hope to rewrite it in the future and use Vectors which will make it more efficient. The Original could display 500 Particles on the Screen but at the moment the Basic4GL version can only display 50 without suffering severe slowdown.

```
' Basic4GL Gravity Example...
'
' Set-up a 2D projection...
'
Const XSize = 500
Const YSize = 500
glMatrixMode (GL_PROJECTION)
glLoadIdentity ()
glOrtho (0, XSize, YSize, 0, 0, 1)
glMatrixMode (GL_MODELVIEW)
glDisable (GL_DEPTH_TEST)
' Variables & Constants...
'
Const GravConst# = 0.00002
Dim MaxDist, Np, N, Nn
Dim Dist#, DistX#, DistY#, Dist2#
Np = 50 ' Number of Particles
' Try Increasing the Number
MaxDist = 500 ' Area that Particles Exist in...
' Try Lowering the Number
Dim X#(Np), Y#(Np), Fx#(Np), Fy#(Np), X_Old#(Np), Y_Old#(Np), Grav#(MaxDist)
' Draw a Scene...
'
glClearColor (1.0, 1.0, 1.0, 1.0) ' Set Clear Colour to White
glClear (GL_COLOR_BUFFER_BIT) ' Clear the Screen
glPointSize (2) ' Increase Point Size to '2'
' Set-up...
'
Randomize() ' Generate Genuine Random Numbers
Grav#(0) = -1000.0 ' Try Increasing or Lowering this Number
' The Following Routine Positions the Particles at...
' Random Places on the Screen
For N = 1 to Np
X#(N) = (Rnd() % XSize)
Y#(N) = (Rnd() % YSize)
Next
For N = 1 to MaxDist
Grav#(N) = Grav#(N - 1) * 0.85
Next
' Main Loop...
'
Do
glClear(GL_COLOR_BUFFER_BIT)
For N = 1 to Np
If X#(N) < 0.0 then
X#(N) = X#(N) + MaxDist
EndIf
If X#(N) > MaxDist then
X#(N) = X#(N) - MaxDist
EndIf
If Y#(N) < 0.0 then
Y#(N) = Y#(N) + MaxDist
EndIf
If Y#(N) > MaxDist then
Y#(N) = Y#(N) - MaxDist
EndIf
X#(N) = X#(N) + Fx#(N)
Y#(N) = Y#(N) + Fy#(N)
For Nn = 1 to Np
If N <> Nn then
DistX# = X#(N) - X#(Nn)
DistY# = Y#(N) - Y#(Nn)
Dist# = Sqr(Pow(DistX#, 2) + Pow(DistY#, 2))
Dist# = Int(Dist#)
Dist2# = 0
If Dist# < MaxDist then
Dist2# = Grav#(Dist#)
EndIf
Fx#(N) = Fx#(N) + (DistX# * (Dist2# * GravConst#))
Fy#(N) = Fy#(N) + (DistY# * (Dist2# * GravConst#))
EndIf
Next
glBegin (GL_POINTS)
glcolor3f (0.0, 0.0, 0.0)
glVertex2f (X#(N), Y#(N))
glEnd ()
Next
SwapBuffers()
Loop
```