Qweqwe rwef wrfg erg ergergggaw erg aweg wegwe grg

  • 20 марта 2011 г.
  • 1822 Слова
In this tutorial, we will create a particle engine for a 2D game. A particle engine (or particle system) is a technique that is used to simulate many effects including fire, sparks, flowing water, falling snow, dust, or abstract effects, like glowing trails or effects from a magic spell. You can really do a lot with a particle engine. Often, they are done in 3D, but for thistutorial, we will look at how to implement one in 2D. As an example of what you can use particle effects for, take a look at the screenshot below, which is from an Asteroids clone that I recently made, which uses a particle system:
In this image, the ship's engine exhaust and the trail for the bonus are visible, which were both implemented with a particle engine. Particle engines were also used forother effects in the game as well. In this tutorial, we will create a particle engine similar to the trail for the bonuses in the Asteroids game above.
Particle Engine Images
The first thing we will need to do is to get images for the particle engine. You can use any images you want, though you are free to use the images below if you want.
Download these images and add them to the content of yourproject like we have done before.
Anatomy of a Particle Engine
The next thing we want to do is discuss a little bit about how particle engines work. A particle engine has three major components: particles, a particle emitter, and the engine itself. A particle is a small point in your game that will be drawn with an image. A particle often has a position in the game world, as well as a velocity,an angle that it is rotated at, an angular velocity which indicates how quickly the particle is spinning, and usually a color and texture for drawing. In our particle engine, we will have lots of particles, which all add up to produce an interesting effect. Also, particles almost always have some way of determining how long they should live for, so the particles can be removed as new ones aregenerated.
A particle emitter is essentially the location that the particles are coming from. Often the particle emitter is also responsible for determining how many particles will be created at any given time.
The particle engine manages the state of the previous two components, and is responsible for removing dead particles from the system.
Our first task will be to create a Particleclass, which will be used in our particle engine. The first step will be to create the class itself. To create this class, right click on your project in the Solution Explorer, and choose Add > New Item from the popup menu. The Add New Item dialog will appear. Select the Class template, and change the name (near the bottom) to Particle.cs. Click on the Add button when you have done this. The newclass file will open up in the main editor window.
The first change we will want to make is to make the class public, so near the top of the file, find the line that says:
class Particle
and change it to say:
public class Particle
The Particle's Properties
We now need to add some properties and instance variables to the class to keep track of important information about our particle. Add thefollowing as instance variables:
public Texture2D Texture { get; set; } // The texture that will be drawn to represent the particle
public Vector2 Position { get; set; } // The current position of the particle
public Vector2 Velocity { get; set; } // The speed of the particle at the current instance
public float Angle { get; set; } // The current angle ofrotation of the particle
public float AngularVelocity { get; set; } // The speed that the angle is changing
public Color Color { get; set; } // The color of the particle
public float Size { get; set; } // The size of the particle
public int TTL { get; set; } // The 'time to live' of the particle
All of these parameters should make sense to you,...
tracking img