Particle System Update
Whilst updated the game The Light Watchman from XNA 3.1 to 4.0 we came across several systems that couldn’t be straight converted. One of these systems was the Particle Effects which I had written to use Point Sprites in XNA 3.1. For those of you that are unaware of how Point Sprites work, a single vertex containing a point and size are passed to the GPU. From this the GPU is able to generate a quad containing texture coordinates which can be rendered as a normal primitive.
That was the XNA 3.1. However, which XNA 4.0, Point Sprites have been removed. The reasoning behind this and some ways to get around this problem can be found here.
The new system has been rewritten to use triangles and triangle lists (as opposed to point lists). During initialization, the index and vertex buffers are created based on the Max particles value (Vertex Buffer = max particles * 4 and the Index Buffer = max particles * 6).
The particle array is a fixed sized array that uses a pair of pointers for rendering and creating particles. The first pointer, first active particle, denotes the first particle in the array that needs to be rendered. The second pointer, first free particle, denotes the first unused particle in the array so new particles can be added in the correct place. The array uses modulo arithmetic to handle these cases. First of all we can have the following:
0 1 - First Active Particle 2 3 - First Free Particle
In this first case, particles 1 and 2 are active while particles 3 and 0 are free. Using modulo arithmetic we can also have the following:
0 1 - First Free Particle 2 3 - First Active Particle
In this case, particles 3 and 0 are active while 1 and 2 are free.
Rendering these two lists has to be handled in different ways to make sure all the active particles are rendered correctly. With the first list (Fig 1) rendering is fairly straight forward, we would render 2 particles (4 primitives) starting from particle 1. With the second list, however, rendering is a little more involved as the particle list will need to be rendered twice. The particles at the end of the list, first active particle (3) to max particles (4), will be rendered first, with the draw call being set up accordingly. Then, with the second pass, the start of the list will be rendered from 0 to the first free particle.
One on the key considerations that were undertaking during the development of the particle system was where to store the emitter settings. Of the two solutions available, the first was to the particle emitter a base class with an Initialize Settings abstract function. The second solution uses a factory function to create the emitter with the desired effects. Whilst the inheritance solution is more flexible (a second function can be overridden for creating the particles) there is a size and speed hit for using virtual functions.
As the particle system is just going into a game, and not into an engine, we don’t require the additional flexibility required from the inheritance solution. This allows us to create the settings object external to the particle emitter and reuse the object to create new emitters.





