|
Particles-V5.2 |
|
You'll need to have, at least, V5.2 - 6.0 registered to benefit from this tutorial
This tutorial will explain, in detail, how to use the in-built particle system used in Game Maker. It will give step by step instructions on how to use the various aspects of the particle system. This tutorial was done in a registered version of 5.2 so you will need at least that to follow it.
Components of the Particle System For a successful particle system, there are, at the minimum, five needed components that will appear in three different events of an object. They are.
--oOo--
Basic Particle System We'll do a simple example in order to better understand the way the particle system works: a side scrolling star-field that starts from the right, goes to the left and fades from white to blue. The first step is to tell the computer that you wish to create a particle system. That is done within the create event of an object with a single line.
This will assign the ID (identification) 'ps' to this system so that we can define particles and the emitter later on. You can have multiple systems within one create event, but we'll do that later. And that's all.
--oOo--
Particles Next we need to define the characteristics of the particle itself. We start by assigning an ID, 'pt1' to the particle like so.
We can next assign the characteristics using that ID. You can see the help file for a complete list but once you understand the basics you should be able to understand them all. So we want a white to blue particle that travels at a random speed between 3 and 6, horizontally going from right to left and we want it to last completely across the screen. For that we need to add four lines.
You'll notice that the particle ID is in all the lines. This tells the computer which particle to assign the characteristics to. The first line tells it to color the particle starting with white and switch halfway through the life of the particle to blue. You can also use part_type_color(ID,color,color,color) to assign three different colors to the particle as well.
The second line will set a random speed between 3 and 6. This will make some of the stars travel at a greater speed than others. Together with the color, this will give some dimension to the star-field. This will make some of the stars travel at a greater speed than others. Together with the color, this will give some dimension to the star-field.
The third line will set the direction. The first and second value set the minimum and maximum values, in this case set to 180 so that the particles will always travel right to left. The third and fourth values do the same thing as in the previous line.
The fourth line sets a random life span between the first two values given. The time between them is so that the particle turns blue at different times (halfway through the life of the particle.)
There are other things you can assign to the particles, like gravity, but once you have a basic understanding you should be able to figure those out for yourself. So far your particle system in the create event should look like this.
--oOo--
Emitter The third thing needed is the emitter, something that will emit the particle from it. Add the following code.
The first line assigns the ID 'em' to the particle system. The 'ps' in the brackets tells it the ID of the particle system that it is assigned to.
The second line seems complicated, but can be broken down. It sets the region for the particles to be created in. The first value, the 'ps', is the ID of the particle system. The second value, the 'em', is the ID of the emitter called in the previous line. The next two values are the minimum and maximum x values of the region. The next two after that are the minimum and maximum y values of the region. In this case we want it to be created on a line on the far right of the screen, so both x values are set for the right side of the screen, 640, and the y values are set for the entire height of the room, 0 and 480. The value after that sets the shape of the distribution within the x and y values in this case a line since on the values (x) is the same. It can also be set for an ellipse: (ps_shape_ellipse), a diamond: (ps_shape_diamond), or a rectangle: (ps_shape_rectangle). The last value sets the distribution type. You have two options here: ps_distr_linear will set an even distribution along the region whereas ps_distr_gaussian will have the majority of the particles created near the middle of the region.
The third line will set the amount of particles to be created. The first value is the ID of the particle system. The second value is the ID of the particle system. The third value is the ID of the particle itself. The fourth value is the number of particles to be created per step. You can set it to a negative number to create particle every few steps as well. A value of -3 will create a particle every third step. For our example set it to 1 so that a particle will be created every step.
On a side note, if you wish to create one stream of particles, say for an explosion use the following code in place of the part_emitter_stream code.
Your entire code for the create event should look like this.
--oOo--
System Step And that is all we need for the create event. The next step is to tell the particle system to run every step. We do that with one line of code in the step event of the object.
The game will now know to do a step, each step, within the particle system with the ID 'ps.'
--oOo--
System Draw Next we have to draw the particle system on the screen. That is also done with one line of code in the draw event.
This tells the game to draw the particle system with the ID 'ps' at the x and y coordinates 0,0. Because the region was set in the previous lines of the create event, there is no need to set the x and y here to anything else.
And that's the basics of the particle system. Place the object in a room of 640X480, change the background color to black and run it. It should look like the first room on the accompanying GMD. Now to see about other things you can do.
--oOo--
Attractors You can also set a region for the particles to be attracted to, that is the particles will move towards that region. Take the star field example you just made and place the following code at the end of the code in the create event.
The first line will assign the ID 'pa' to be used in the particle system with the ID 'ps'.
The second line will set the attractor's position. The first value is the ID of the particle system. The second value is the ID of the attractor created in the first line. The third and fourth values are the x and y coordinates for the attractor.
The third line will set the force that the particles are attracted to the attractor. The first and second values are the ID's of the particle system, then the attractor. The third value is the amount of force exerted on the particles. The fourth value is the maximum distance at which the attractor will have an affect. Any particles past the distance from the attractor will not be affected. The fifth value is the type of force to be used. ps_force_constant means the speed of the particles will not change. ps_force_linear means the force will grow linearly as the particles approaches the attractor. ps_force_quadratic means the force grows quadratic. The last value indicates if the particle will accelerate towards the attractor (true) or move at a constant speed (false).
--oOo--
Deflectors Deflectors will do just that, deflect the particles away when encountered. Place the following code at the end of the create event in the star-field example. Make sure the attractor code is not present.
The first line creates the deflector in the particle system with the ID 'ps' and assigns it to the ID 'df' which will be used in following lines.
The second line sets the region. The first value is the ID of the particle system. The second value is the ID of the deflector called in the previous line. The last four values are the minimum x, maximum x, minimum y, and maximum y in that order. This defines the actual region that will deflect the particles.
The third line describes the kind of deflector to be used. Again the first and second values are the ID of the particle system and deflector. The third value is the type of deflection to be used. You have two options here. ps_deflect_horizontal is used when you have defined a vertical wall and ps_deflect_vertical is used when you have defined a horizontal wall.
--oOo--
Destroyers Destroyers will destroy any particles created by the assigned particle system is encounters. Open up the star-field example and add the following lines at the end of the code in the create event, again making sure that the previous examples codes are not there as well.
The first line does the same as all previous examples. It creates the destroyer in the particle system with the ID 'ps' and assigns it the ID 'de'.
The second line defines the region. The first and second lines are the ID's of the particle system and the destroyer. The next four lines are the minimum x, maximum x, minimum y, and maximum y defining the region where the destroyer is. The last value describes the shape of the region. You can select either ps_shape_rectangle, ps_shape_ellipse or ps_shape_diamond.
--oOo--
Changers Changers are a little more complicated than previous examples. They allow you to change one particle to another. You can either change a single aspect of the particles, the motion or the shape, or you can change the entire particle. Please note, if you intend to change the entire particle, then you must define all needed aspects of the new particles.
The first step is to define the aspects of the new particle. Place the following code at the end of the code in the create event of the star-field example, again making sure the other codes of previous examples are not there.
This will create a second particle, assigned to the ID 'pt2' and define its speed, shape and direction. In this case a diamond shape with a speed between 6 and 9. The direction is the same of the first particles. Next, add the following code to define the changer itself...
The first line creates the changer in the particle system with the ID 'ps' and assigns it the ID 'pc'.
The second line defines the region where the change is to take place. The first and second values are the ID's of the particle system and changer. The next four values are the minimum x, maximum x, minimum y and maximum y coordinates that define the region where the change is to take place. The final value is the shape of the region. Again you are allowed ps_shape_rectangle, ps_shape_ellipse, or ps_shape_diamond.
The third line defines the kind of changer. The first and second values define the ID's of the particle system and the changer. The third value defines the kind of changer. You can have ps_change_motion, ps_change_shape, or ps_change_all.
The fourth line defines which particles to change. The first and second values define the ID's of the particle system and the changer. The third value is the particle to be changed and the fourth value is the particle to be changed into.
--oOo--
Sprite Based Systems You can also have particle systems that use sprites instead of pixels. Here is the entire code to be placed in the create event.
The step event and draw event are kept the same as in previous examples. You'll notice that there are really only two lines added. Underneath where the particle system is created is the following line.
This simply says that the particle system will be sprite based. The first value is the ID of the particle system and the second value is set to true. You can set it to false if you don't want the particle system sprite based, but that is the default so you wouldn't really need the line in there at all. The second line added appears beneath the line that creates the particle itself.
This will assign the sprite and conditions to be used. The first value is the ID of the particle. The second value is the ID (name) of the sprite. The third value is whether or not the sprite is to be animated, set to true or false. The fourth value defines whether or not the sprite is to be stretched over its lifetime. The final value defines whether or not to start with a random image from the sprite.
--oOo--
Multiple Particle Systems Another aspect of the particle system is the ability to assign as many particles as you want to each particle system. Looking at the star-field example you can see that it is a bit bland. By adding another layer of particles to the system, we can add another dimension to it. Take the star-field example and add the following to the end of it's create code.
The first part create a second particle with an ID of 'pt2' that will stay blue, pushing it further away tan the first set of particles and travel slower as well. The life of the particle is set higher than the first particle since it will take more time to reach the left side of the screen.
The second part creates a second emitter within the particle system with the ID of 'em2'. Most of the aspects of the first emitter are kept the same except for the distribution. That is changed from ps_distr_linear to ps_distr_gaussian. This will create most of the particles along the middle of the region. This is purely for effect. The final change is that the number of particles create per step, in the part_emitter_stream line, is changed to -5, or one particle every five steps. Since the particles move slower, we don't need as many created.
--oOo--
Final Notes and Tips These are but a few things to get you going in how to use particles systems. There are a few more functions available, such as creating more particles upon the death of a particle, and other such things. Once you get an understanding of the above, the rest of it should be fairly easy to understand. You can also see the Particle Example created by Prof. Mark Overmars available with the Game Maker program itself.
To assign a deflector to the shape of a sprite, create the particle system within the object with that sprite and use the in-built variables bbox_left, bbox_right, bbox_top and bbox_bottom to set it to the bounding box of the sprite.
To set the emitter to the x, y origin of a moving object, move the part_emitter_region line to the step event and set the min x, max x, min y and max y to x, x, y, y.
To set a variable direction-facing right that goes below 0 you will need to 'trick' the system. For a direction between 315 and 45 set the direction to the following.
To give the illusion of a particle that fades, set the final color to the background color.
Don't forget that the particles are drawn so, just like anything else that uses the draw function, any sprite assigned to the object will not appear. If you wish for the sprite to appear, use a sprite_draw command in the draw event. Have it after the part_system_draw line so the sprite will be drawn in front of the particles.
--oOo--
Conclusion Once an understanding is achieved as to the different aspects of the particle system, it really isn't the difficult. Experiment with it for different effects but above all, have fun.
Eugene Perry (Ablach) - Revision #1 |