Basic Particle System in Visual Basic 6

Daniel Marton Story
Copyright Dec 2, 2004

URL: http://www.vbgamer.com/tutorial.asp?ndx=43


Page 1 Particle Systems in a game or any type of application adds a great
special effect. From spell casting in a game to wind movement studies,
particles can be complex or basic, and will still look great. Each
particle has a X velocity and a Y velocity other variables can be added
to hold data, like maybe if you were showing the movement of when the
particles have gravity, you may want to add mass of the particle.

To begin, we need to make a User Defined Type (UDT) with the properties for each particle.

  1. Private Type Ptype
  2.   X as long
  3.   Y as long
  4.   Xv as long
  5.   Yv as long
  6.   LifeSpan as long
  7. End Type

Now we need to add the other variables for when we need them later.

  1. Const Max  =  250
  2. Dim Particle(Max) as Ptype
  3. Dim bRunning as boolean
  4. Dim Xstart as long, Ystart as long
  5. Dim i as long

Now when the form loads we want to set the properties of each particle.

  1. Private Sub Form_Load()
  2.   Show
  3.  
  4.   Xstart =  ScaleWidth  2
  5.   Ystart = ScaleHeight  2
  6.   For i = 0 To Max
  7.     Particle(i).LifeSpan = Rnd * 500
  8.     Particle(i).X = Xstart
  9.     Particle(i).Y = Ystart
  10.     Particle(i).Yv = Rnd * -15
  11.     Particle(i).Xv = Rnd * -10 + 5
  12.   Next i
  13.   bRunning =  True
  14.   ParticleLoop
  15. End Sub



Now it is time to start the main part of a particle system is the
update loop to update each particle and to draw it on the form/display.

  1. Private Sub ParticleLoop()
  2.   Do While bRunning
  3.     DoEvents
  4.     For i = 0 To Max
  5.       PSet (Particle(i).X, Particle(i).Y), BackColor
  6.       Particle(i).X = Particle(i).X + Particle(i).Xv
  7.       Particle(i).Y = Particle(i).Y + Particle(i).Yv
  8.       Particle(i).Yv = Particle(I).Yv + 0.96
  9.       If Particle(i).Y >= ScaleHeight Then
  10.         Particle(i).Yv = -(Particle(I).Yv * 0.46)
  11.         Particle(i).Y =  ScaleHeight
  12.       End If
  13.       Particle(i).LifeSpan = Particle(i).LifeSpan - 1
  14.       If Particle(i).LifeSpan <= 0 Then
  15.         Particle(i).LifeSpan = Rnd * 500
  16.         Particle(i).X = Xstart
  17.         Particle(i).Y = Ystart
  18.         Particle(i).Yv = Rnd * -15
  19.         Particle(i).Xv = Rnd * -10 + 5
  20.       End If
  21.       PSet (Particle(i).X, Particle(i).Y), RGB(0, 0, 0)
  22.     Next i
  23.     Refresh
  24.   Loop
  25. End Sub


Now all we need is the code to close the program.

  1. Private Sub Form_Unload(Cancel As Integer)
  2.     bRunning = False
  3. End Sub


Complete Code:

  1. Private Type Ptype
  2.   X as long
  3.   Y as long
  4.   Xv as long
  5.   Yv as long
  6.   LifeSpan as long
  7. End Type
  8. Const Max  =  250
  9. Dim Particle(Max) as Ptype
  10. Dim bRunning as boolean
  11. Dim Xstart as long, Ystart as long
  12. Dim i as long
  13. Private Sub Form_Load()
  14.   Show
  15.   Xstart =  ScaleWidth  2
  16.   Ystart = ScaleHeight  2
  17.   For i = 0 To Max
  18.     Particle(I).LifeSpan = Rnd * 500
  19.     Particle(i).X = Xstart
  20.     Particle(i).Y = Ystart
  21.     Particle(i).Yv = Rnd * -15
  22.     Particle(i).Xv = Rnd * -10 + 5
  23.   Next i
  24.   bRunning =  True
  25.   ParticleLoop
  26. End Sub
  27. Private Sub ParticleLoop
  28.   Do While bRunning
  29.     DoEvents
  30.     For i = 0 To Max
  31.       PSet (Particle(i).X, Particle(i).Y), BackColor
  32.       Particle(i).X = Particle(i).X + Particle(i).Xv
  33.       Particle(i).Y = Particle(i).Y + Particle(i).Yv
  34.       Particle(i).Yv = Particle(i).Yv + 0.96
  35.       If Particle(i).Y >= ScaleHeight Then
  36.         Particle(i).Yv = -(Particle(i).Yv * 0.46)
  37.         Particle(i).Y =  ScaleHeight
  38.       End If
  39.       Particle(i).LifeSpan = Particle(i).LifeSpan - 1
  40.       If Particle(i).LifeSpan <= 0 Then
  41.         Particle(i).LifeSpan = Rnd * 500
  42.         Particle(i).X = Xstart
  43.         Particle(i).Y = Ystart
  44.         Particle(i).Yv = Rnd * -15
  45.         Particle(i).Xv = Rnd * -10 + 5
  46.       End If
  47.       PSet (Particle(i).X, Particle(i).Y), RGB(0, 0, 0)
  48.     Next i
  49.     Refresh
  50.   Loop
  51. End Sub
  52. Private Sub Form_Unload(Cancel As Integer)
  53.   bRunning = False
  54. End Sub