Basic Particle System in Visual Basic 6

Daniel Marton Story
Copyright Dec 2, 2004

Page 1
Particle Systems in a game or any type of application adds a greatspecial effect. From spell casting in a game to wind movement studies,particles can be complex or basic, and will still look great. Eachparticle has a X velocity and a Y velocity other variables can be addedto hold data, like maybe if you were showing the movement of when theparticles 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.

Private Type Ptype

X as long

Y as long

Xv as long

Yv as long

LifeSpan as long

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.

Const Max = 250

Dim Particle(Max) as Ptype

Dim bRunning as boolean

Dim Xstart as long, Ystart as long

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 Trueor False) will tell the loop of the particle system to stop if theprogram 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 togo through every particle to change the particle's position on theform/display using the particle's velocity.

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

Particle(i).Yv = Rnd * -15 -Sets a random value between 0and -15 for the Y velocity of the particle. (You can play around withthis line to get differ results on the characteristics of the particleswhen 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 theupdate loop to update each particle and to draw it on the form/display.

Private Sub ParticleLoop()

Do While bRunning

DoEvents

For i = 0 To Max

PSet (Particle(i).X, Particle(i).Y), BackColor

Particle(i).X = Particle(i).X + Particle(i).Xv

Particle(i).Y = Particle(i).Y + Particle(i).Yv

Particle(i).Yv = Particle(I).Yv + 0.96

If Particle(i).Y >= ScaleHeight Then

Particle(i).Yv = -(Particle(I).Yv * 0.46)

Particle(i).Y = ScaleHeight

End If

Particle(i).LifeSpan = Particle(i).LifeSpan - 1

If Particle(i).LifeSpan <= 0 Then

Particle(i).LifeSpan = Rnd * 500

Particle(i).X = Xstart

Particle(i).Y = Ystart

Particle(i).Yv = Rnd * -15

Particle(i).Xv = Rnd * -10 + 5

End If

PSet (Particle(i).X, Particle(i).Y), RGB(0, 0, 0)

Next i

Refresh

Loop

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).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).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.)