Post

 Resources 

Console

Basic Particle System in Visual Basic 6


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

  • X & Y - The two variables to hold the position of the particle for plotting on the form/display.
  • Xv & Yv - The two velocities of the particle.
  • LifeSpan - How long the particle will last until it is reset at the beginning point.
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

  • Const Max = 250 - This line can be change to add more or make less particles to draw.
  • Dim Particle(Max) as Ptype - Dim an array of particles to hold each particle's properties.
  • Dim bRunning as boolean -This Boolean (ether equals True
    or False) will tell the loop of the particle system to stop if the
    program is trying to shutdown.
  • Dim Xstart as long, Ystart as long -These two variables are to hold the starting position of a reset particle.
  • Dim i as long - This variable is for the For Next loop to
    go through every particle to change the particle's position on the
    form/display using the particle's velocity.
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


  • Show - This will make the form appear on screen.
  • Xstart = ScaleWidth 2 & Ystart = ScaleHeight 2 -This sets the starting position for the reset particles.
  • For I = 0 To Max -Begin to go through each particle and set the default properties.
  • Particle(i).LifeSpan = Rnd * 500 - Create a random life span for the particle.
  • Particle(i).X = Xstart & Particle(I).Y = Ystart -Sets the particle to the starting position.
  • Particle(i).Yv = Rnd * -15 -Sets a random value between 0
    and -15 for the Y velocity of the particle. (You can play around with
    this line to get differ results on the characteristics of the particles
    when they first come alive)
  • Particle(i).Xv = Rnd * -10 + 5 - Sets a random value between -5 and 5 for the X velocity of the particle.
  • (You can play around with this line to get differ results on the characteristics of the particles when they first come alive)
  • bRunning = True - The Program has just finished setting the properties of the particles and is ready to run the loop
  • ParticleLoop -Call the sub with the particle system loop

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


  • Do While bRunning -This line is to start the loop of updating the particles
  • DoEvents -You always want to add this in a long lasting loop. It tells windows to do outside events. Basically, everything won’t “freeze" or run very slow.
  • For i = 0 To Max -begin to start going through each particle in the array.
  • PSet (Particle(i).X, Particle(i).Y), BackColor -Erases the last spot were the particle was, by matching the form's back color.
  • Particle(i).X = Particle(i).X + Particle(i).Xv -Updates the particle's x position by adding its X velocity.
  • Particle(i).Y = Particle(i).Y + Particle(i).Yv -Updates the particle's y position by adding its Y velocity.
  • Particle(i).Yv = Particle(i).Yv + 0.96 -This adds gravity to the particle's Y velocity. (You can play around with this line to get differ results on the characteristics of the particles.)
  • If Particle(i).Y >= ScaleHeight Then - If the Particle is equal to or greater then the form's height then. basically, if the particle hits the bottom of the form then
  • Particle(i).Yv = -(Particle(i).Yv * 0.46) - This adds a bounce effect to the particle when it hits the bottom of the form by making the particle's Y velocity opposite and making it 46% less. (You can play around with this line to get differ results on the characteristics of the 'bounce' on the particles.)

  • Particle(i).LifeSpan = Particle(I).Life – 1 - Subtract one life from the particle.
  • If Particle(i).LifeSpan <= 0 Then - If the life span is equal to or less than 0 then. basically, If the particle as run out of life then
  • Particle(i).LifeSpan = Rnd * 500 - Create a random life span for the particle.
  • Particle(i).X = Xstart & Particle(I).Y = Ystart -Sets the particle to the starting position.
  • Particle(i).Yv = Rnd * -15 - Sets a random value between 0 and -15 for the Y velocity of the particle. (You can play around with this line to get differ results on the characteristics of the particles after running the first time.)
  • Particle(i).Xv = Rnd * -10 + 5 - Sets a random value between -5 and 5 for the X velocity of the particle.  (You can play around with this line to get differ results on the characteristics of the particles after running the first time.)
  • PSet (Particle(i).X, Particle(i).Y), RGB(0, 0, 0) - This will draw the particle on the form/display at the color of black. (You can play around with this line to get differ results on the color of the particles.)
  • Next I - Goes to next particle and updates it.
  • Refresh - Refresh the form.
  • Loop - Go back to the being of the loop
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


  • bRunning = False - Stops the loop from continuing.
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



8 comments

Tutorial Console

Tutorial by:

Dan


Date: 2004 Dec 2


8 comments

Latest comment


by: autoaim

Basicly when the mouse moves just set the Xstart and Ystart variables to the mouse position like so:

[code]
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Xstart = X
Ystart = Y
End Sub
[/code]

The Xstart and Ystart is what holds the particles starting postion; So when ever a particle dies; it respawns to that position on the form.

Post a Comment


Printer Friendly



Copyright © 2002 - 2004 Eric Coleman, Peter Kuchnio , et. al.
There have been 22 visitors within the last 20 minutes
RSS News Feed