Programmer's Corner - Basic Particle System in Visual Basic 6

Backup and Security Solutions 10% off all products with promo code: VISI-P1YR
Get the Programmer's Corner FireFox Search Plug-In

Basic Particle System in Visual Basic 6

-- By Daniel M. Story


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.

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 True or False) when till 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 when wont to set the properties of each particle.

Private Sub Form_Load()
Show

Xstart =  ScaleWidth \ 2
Ystart = ScaleHeight \ 2

For i = 0 To Max
	Particle(i).LifeSpan = Rnd * 500

	Particle(i).X = Xstart
	Particle(i).Y = Ystart

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

bRunning =  True
ParticleLoop
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.

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

-End If
End the If Then Statement.

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

-End If
End the If Then Statement.

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

Private Sub Form_Unload(Cancel As Integer)
bRunning = False
End
End Sub

-bRunning = False
Stops the loop from continuing.

-End
Ends the program.


Complete Code:

Private Type Ptype
	X as long
	Y as long
	Xv as long
	Yv as long
	LifeSpan as long
End Type

Const Max  =  250
Dim Particle(Max) as Ptype
Dim bRunning as boolean
Dim Xstart as long, Ystart as long
Dim i as long 

Private Sub Form_Load()
Show

Xstart =  ScaleWidth \ 2
Ystart = ScaleHeight \ 2

For i = 0 To Max
	Particle(I).LifeSpan = Rnd * 500

	Particle(i).X = Xstart
	Particle(i).Y = Ystart

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

bRunning =  True
ParticleLoop
End Sub

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

Private Sub Form_Unload(Cancel As Integer)
bRunning = False
End
End Sub
Author Information:

Daniel M. Story

http://www.danstory.com

admin@danstory.com

Comments:

Add your comments here.

Name

Comment

You can also send feedback to feedback@programmers-corner.com

There are currently no comments available.