gamemaker draw sprite on 3d surface
In this article, nosotros'll begin with the nuts of the GameMaker Studio particle arrangement and end with advanced techniques and implementations. Remember of this equally a crash form to get yous familiar and comfortable with i of GameMaker Studio's most powerful built-in features. The demo and downloadable project file will allow you to follow forth with the article to see exactly what the particle system is all about.
Particle Organisation Overview
The GameMaker Studio particle system is a cheap and like shooting fish in a barrel way to create flashy effects for your game project. Through a combination of particles and emitters, you lot can rapidly create impressive explosions, fume, claret, shrapnel, and endless other effects. Similar effects can be achieved by using individual objects, but the computing toll of the built-in particle effects organization is far cheaper.
To use the particle system in your projection, you'll need to understand GML (GameMaker Language), but in one case you become familiar with the particle system, it is just a matter of filling in the blanks and experimentation.
Check out the demo below to see what we can attain:
Particle System Basics
Particle Systems in GameMaker Studio consist of three parts: the arrangement itself, the particle, and the emitter. You can create multiple systems, and each system tin can comprise multiple particles and emitters. Think of the system as a container, with the particles and the emitters defined within.
Implementing a particle effect in GameMaker Studio is a four step process.
- First, you lot must define the particle system itself.
- Then, you define the actual particles that will be used within that system.
- Next, you accept to define the emitter that will create your defined particles.
- Finally, you take to determine when and where the particle emitter will appear in your game.
To teach you the nuts of the GameMaker Studio particle organisation, nosotros'll start out by creating this very simple dark-green particle effect.
Creating the Particle System
Creating the particle organization is as simple as defining a variable. We create an object called obj_first_particle and place the following code in the object'due south Create event:
FirstParticleSystem = part_system_create();
The particle system will adopt the depth value of the object that the organization is defined in, but yous can as well ready the depth separately with GML:
part_system_depth(FirstParticleSystem,0);
Remember that objects and particle systems with a loftier depth value are fatigued to the screen first. With a depth of 0, our green particle organisation volition appear above objects with a depth greater than 0, and will announced below objects with a depth less than 0.
Particle systems are drawn to the screen with a base position relative to (0,0). If for some reason yous desire to create an showtime for all future positions of this particle arrangement, you tin can use the following code to create a new base position (where ind is the particle arrangement):
part_system_position(ind, x, y);
With a new base position set to (ten,10), a particle created at (25,25) volition instead be fatigued to (35,35). Irresolute the base of operations position of a particle arrangement is rarely necessary, but y'all may notice it useful in your specific projection.
Creating the Particle Type
At present that the system has been gear up, it is time to ascertain the actual particle that the emitter will create. Particles can contain a large number of parameters that dictate how the particle volition look and behave. The get-go step is to create a variable for the particle, and we exercise this in the Create event of the obj_first_particle object:
first_particle = part_type_create();
Side by side, we begin defining the private parameters of the particle. Since we practice not plan on altering this particle during runtime, we can place all of this code in the Create consequence of the obj_first_particle object.
part_type_shape determines the base of operations shape of the particle. At that place are 14 default particle shapes bachelor in GameMaker Studio, and you can define your own shapes as well. We'll cover this in the avant-garde section below, but for now let'south but start with a basic square.
part_type_shape(first_particle,pt_shape_square);
For a full list of the bachelor default shapes, check the official GameMaker documentation.
With part_type_scale, we can set the base of operations X and Y scale values of the particle shape. Since we want a perfect square shape, we use the post-obit code:
part_type_scale(first_particle,1,1);
part_type_size allows us to modify the size of the particle at creation and as well as over time. The format for this lawmaking is part_type_size(ind, size_min, size_max, size_incr, size_wiggle).
-
indis the particle variable. -
size_minandsize_maxdetermine the range of the particle size when it is first created. If you want a compatible size, but enter the aforementioned value for both the min and the max. -
size_incris a value that allows the particle to grow or compress over time. This value determines the speed of growth, so if you don't want your sprite to change size, you lot tin can use a value of0. -
size_wiggleis slightly more complicated, and then nosotros'll comprehend that in the advanced techniques section beneath.
Here'south the particle size code used in our green particle effect:
part_type_size(first_particle,0.ten,0.xv,-.001,0);
The particle will be created with a size somewhere between 0.ten and 0.15 to create variety, and the sprite volition slowly compress at a speed of -0.001. This speed value depends greatly on your room speed, so yous will likely demand to experiment with values to get the desired results. We will not exist using any size wiggle, so we gear up the value to 0.
Particles in GameMaker Studio tin actually change colors over time. This is accomplished with part_type_color2 and part_type_color3. If yous don't want your sprite to change colors, and so you can just employ part_type_color1. For our green particle effect, nosotros want information technology to start out with a vivid yellow/green color and then modify to a solid light-green colour, then we use part_type_color2:
part_type_color2(first_particle,8454143,65280);
The 2 colors I selected are specific numerical values that I use regularly, simply if you want to utilise more traditional hex values, you can use the format $RRGGBB.
Particles tin besides become more than or less transparent over time with part_type_alpha2 and part_type_alpha3. If you lot want a consequent alpha value, use part_type_alpha1. For our green particle upshot, nosotros desire the particle to start completely opaque and fade by 25% as it stays on the screen, and then we need two blastoff values:
part_type_alpha2(first_particle,one,0.75);
In GameMaker, alpha is a value from 0 to 1. A completely invisible object volition take an blastoff value of 0, while a completely opaque object volition have an alpha value of 1.
Particle speed is determined just similar particle size. Particles are created within a range of speed values, and that value can increment or decrease. The format for this code is part_type_speed(ind, speed_min, speed_max, speed_incr, speed_wiggle), where ind is the particle variable, speed_min and speed_max is the speed range, speed_incr is the charge per unit at which the particle speed changes, and speed_wiggle is a parameter that we'll comprehend after on.
The speed code for our greenish particle is:
part_type_speed(first_particle,0.ane,0.5,0,0);
Our particle will offset moving with a speed value somewhere between 0.1 and 0.5. This speed volition remain constant, so nosotros use a value of 0, and we will again not be implementing speed wiggle, so we use a value of 0.
While a particle's speed parameter determines how fast it moves, the direction parameter determines where information technology moves. The direction code is in the following format: part_type_direction(ind, dir_min, dir_max, dir_incr, dir_wiggle) and once again we prepare the variable, starting range, an incremental value, and a wiggle value. For the green particle effect, we want our particle to outset moving in any management, and we want that direction to remain constant:
part_type_direction(first_particle,0,359,0,0);
The range of 0 to 359 ensures that the particle has a chance to move in any direction (an angle between 0 and 359 degrees). If you wanted a particle to move up and merely upward, and then you would use a range of xc to xc).
The gravity of our particle result is what makes it the nigh interesting. While our speed and direction parameters are fix to create a particle that starts by moving in one management at a abiding speed, the gravity parameter kicks in and alters the particle over fourth dimension. With a format of part_type_gravity(ind, grav_amount, grav_direction), the gravity parameter is very simple:
part_type_gravity(first_particle,0.02,90);
By applying a slight gravitational pull of 0.02 in an upward management (xc degrees), nosotros tin create a particle that appears to float. Combined with our size and blastoff parameters, the particle shrinks and becomes more transparent over time, accompanied past the gravitational elevator.
The orientation of the particle shape is besides of import to the appearance of the effect, so we employ part_type_orientation(ind, ang_min, ang_max, ang_incr, ang_wiggle, ang_relative) to rotate the square over fourth dimension.
-
indis the particle variable. -
ang_minandang_maxmake up one's mind the starting value of the shape'due south rotation value. -
ang_incris used to increment or decrement the shape'southward orientation over fourth dimension. -
ang_relativeis a Boolean value to determine if the orientation should be set relative to the motion of the particle (true) or non (imitation).
We want our green particle to rotate slightly to the left, so we use the following code:
part_type_orientation(first_particle,0,359,ten,0,true);
One of the about important parameters of a particle is the lifespan value. This value determines the minimum and maximum time that a particle will be drawn to the screen. With two identical min and max values, all particles of that type will exist for the same amount of time. We want our greenish particles to take variety, so nosotros will use a range of 100 to 150 for the lifespan value:
part_type_life(first_particle,100,150);
The final parameter for particles is a uncomplicated Boolean to determine whether the particles should alloy together with an additive alloy effect:
part_type_blend(first_particle,truthful);
Creating the Particle Emitter
The first step in defining an emitter is to create a variable. We define this emitter in the Create upshot of the obj_first_particle object.
first_emitter = part_emitter_create(FirstParticleSystem);
Next, we define the emitter region with part_emitter_region(ps, ind, xmin, xmax, ymin, ymax, shape, distribution).
-
psis the particle system that the emitter belongs to andindis the emitter variable. - The ten and y min and max values determine the size of the emitter region.
-
shapedetermines the shape of the emitter region (ps_shape_rectangle, ps_shape_ellipse, ps_shape_diamond, ps_shap_line). -
distributionis a distribution curve (ps_dist_linear, ps_distr_gaussian, ps_distr_invgaussian).
Nosotros'll cover the shape and distribution parameters in further particular in the advanced techniques section. For now, we'll use the default ellipse shape and Gaussian distribution values:
part_emitter_region(FirstParticleSystem, first_emitter, x-20, x+20, y-twenty, y+twenty, ps_shape_ellipse, ps_distr_gaussian);
This code creates an elliptical emitter region that is 40 pixels alpine and 40 pixels wide, and centred on the x and y values of the obj_first_particle object. Particles created by the emitter will appear inside this defined region.
Activating the Particle Emitter
The adjacent step is to determine one of two emitter types: Outburst or Stream. A Burst emitter creates a specified amount of a certain particle whenever it is triggered. A Stream emitter creates a specified corporeality of a certain particle once every footstep.
We'll take a look at the more versatile Burst emitters in the advanced techniques section, and then for at present let'due south just use the Stream emitter:
part_emitter_stream(FirstParticleSystem,first_emitter,first_particle,ane);
We place this code in the Create event of the obj_first_particle object, resulting in the emitter creating one particle each pace every bit soon every bit the object is created. With a room speed of 30, our emitter will create 30 particles per 2d; to create 60 particles per second, yous would only use a value of ii instead of i.
And with that, our simple green particle upshot is complete! The usefulness of this outcome is limited, simply it's important to start small before diving into the more complicated aspects of the GameMaker Studio particle system. Once you empathize the basics of the particle system, you can start implementing more avant-garde particle systems.
Advanced Particle System Techniques
Particle Wiggle
Wiggle is a simple, still powerful parameter that can drastically change the advent of your particles. The wiggle parameter causes the particle to oscillate betwixt the min and max values for the lifetime of the particle. The value can be between 0 and twenty and determines the speed of the jerk.
The "Fire Bubble" example in the embedded demo uses a wiggle value of 0.forty in the part_type_size parameter:
part_type_size(fire_bubble_part,0.25,0.75,-0.01,0.twoscore);
Burst Emitters and Moving Objects
Ane of the virtually mutual implementations of particle systems involves particles that emanate from backside a moving object, such equally a fume trail on a rocket. Achieving this effect in GameMaker Studio requires a Burst emitter to exist placed in an object's Footstep event.
The included instance uses the same green particle organisation as earlier, but with a slightly modified emitter. Instead of triggering a Stream emitter in the object's Create result, a Outburst emitter is placed in the object's Step event. The current position of the mouse cursor is checked against the previous position of the cursor, and if there is a change in the cursor'due south position, the Flare-up emitter is triggered to release five particles:
10 = mouse_x; y = mouse_y; part_emitter_region(MouseParticle,green_mouse_emitter,x,ten,y,y,0,0); if x != old_x || old_y != y { part_emitter_burst(MouseParticle,green_mouse_emitter,green_mouse_particle,5); } old_x = ten; old_y = y; Emitter Region Shapes
By using the dissimilar emitter shapes and distribution curves, y'all tin can create vastly different furnishings. Linear curve distribution combined with a line-shaped particle emitter can create a convincing rain consequence.
part_emitter_region(RainParticle, rain_emitter, -100, room_width, y, y, ps_shape_line, ps_distr_linear);
The emitter shape is defined past a line that begins 100 pixels to the left of the room origin and is extended to the width of the room. A linear distribution bend is used to distribute the rain particles evenly across the emitter region. It is too useful to use part_system_update to advance the pelting particle several steps in the Create event. This lawmaking gives the impression that the rain was falling before yous loaded the room, even though the particle organisation didn't be in memory notwithstanding.
repeat (room_speed * 3) { part_system_update(RainParticle); } Particle System Stride and Death Effects
Individual particles within a arrangement tin also spawn other particles on Step and Decease events. The example shows a imperial spark particle that spawns smaller dust particles as it travels to the right, and spawns a smoke particle at the terminate of its lifespan:
part_type_step(spark_particle,one,dust_particle); part_type_death(spark_particle,5,smoke_particle);
Custom Particle Shapes
By using part_type_sprite(ind, sprite, animate, stretch, random), y'all can use custom sprites instead of the congenital-in GameMaker particle types.
-
indis the particle variable. -
spriteis the sprite variable to be used. -
breathingis a Boolean to decide if the sub-images should be animated. -
stretchis a Boolean that matches the blitheness length to the lifespan of the particle. -
randomis a Boolean to decide whether the starting sub-image should be randomly selected.
part_type_sprite(heart_particle,spr_heart,false,false,faux);
Retentiveness Management
The most of import thing to remember virtually the GameMaker Studio particle system is that you need to manually remove items from memory when not in use. Unlike standard game objects, particle systems will remain in retention fifty-fifty if you change rooms. The easiest way to handle this is to identify memory management code in the room changing events of your game objects.
-
part_system_clear(ind): Clears all emitters and particles belonging to a specific particle organisation. -
part_system_clear(ind): Clears all instances of a specific particle type. -
part_emitter_clear(ps, ind): Clears all particles belonging to a specific emitter. -
part_emitter_destroy(ps, ind): Destroys a specific emitter in a specific particle system. -
part_emitter_destroy_all(ps): Destroys all emitters in a specific particle system. -
part_type_destroy(ind): Destroys a specific particle blazon. -
part_system_destroy(ind): Destroys an entire particle arrangement, including all particles and emitters contained within.
Decision
Even with such a lengthy tutorial, we've nonetheless only scratched the surface of what kinds of effects the GameMaker Studio particle system is capable of. The key to mastering particles is familiarity and experimentation, and then leap in and beginning creating your own effects using the knowledge you've gained. Be certain to cheque out the official GameMaker documentation to view all the available particle organisation GML!
Did y'all find this mail useful?
tarkingtonsculd1974.blogspot.com
Source: https://gamedevelopment.tutsplus.com/articles/mastering-the-gamemaker-studio-particle-system--cms-22782
0 Response to "gamemaker draw sprite on 3d surface"
Postar um comentário