Some ideas on particle systems

Discuss about anything related to the Irrlicht Engine, or read announcements about any significant features or usage changes.
Post Reply
tom_gamer
Posts: 15
Joined: Sat Jun 01, 2013 8:51 am

Some ideas on particle systems

Post by tom_gamer »

Hello,

lately, I played a bit with particle systems (while developing SpaceCombat). I got some ideas and I post them here. Maybe something of that is useful for your projects...


Have more than one texture
First of all, I wanted more than one texture in a particle system. I achieved this by create a patchwork texture assigned it to the particle system.
In step 2 I put four texture coordinates in the SParticle struct. So, a particle generator can set the texture coordinates for each particle.
Finally, I put some code in the particle scene node, that draws the particles with the texture coordinates from the SParticle struct.
Done!


Separate calculation and drawing
A particle system has two tasks: calculation and drawing. Because both tasks takes some time, it is worthwhile to separate these tasks. So you can put them in different processes that can be done on separate CPU cores.
So, we remove the doParticleSystem() from the OnRegisterSceneNode() in the particle scene node and make it public. Additionally double the particle array (double buffer). Now we can work on one of both arrays while drawing the other one.
You have to sync both tasks at the end of each frame. Here you have to swap the particle buffers. Note, that you also have to copy new generated particles.


Sort the particles by distance to camera
When you now have particles, that can be good visual separated (e.g. wreckage), then it is essential, that the particles are drawn from back to front. (Or else objects far away maybe drawn over near objects). Since you separated the calculation, computing power is no problem. This can be achieved by a particle affector.


Fade particles near the camera
When you create clouds or smoke with particles and want to move through, you get some flickering. This occurs, when a particle is in one frame in front of the camera and in the next one behind it. My solution: Sort the particles by the distance to the camera (we do it already) and fade the particles near the camera out. So you get a smooth fading, when moving through the particles. This can be achieved by a particle affector.


Start a particle system with existing particles
I wanted a weather effect (sand storm) with a particle system. But this storm should be there, when the game starts. When you use the build-in particle system scene node, initial there is no particle and they will be generated as the time runs. That wasn't the effect, I wanted. So I tweaked it a bit.
Create an init function, that runs several loops of the doParticleSystem() function. You have to increase the time, that this function takes. Since all affectors work on this time, the result is a fast-forward of the whole particle system. Now you have to save this last time stamp (--> initial time).
Whenever in the game the function doParticleSystem() is called, increase the given time by the initial time.
Done!
SpaceCombat
http://sourceforge.net/projects/spacecombatgame
(OpenSource space combat simulation)
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Some ideas on particle systems

Post by hendu »

Several of those waste CPU time unnecessarily, and would be handled much better by shaders.

For example you could make a sand storm entirely gpu-side, including fading close to camera.
tom_gamer
Posts: 15
Joined: Sat Jun 01, 2013 8:51 am

Re: Some ideas on particle systems

Post by tom_gamer »

Maybe you are right. :?
But in my game, it takes about 30ms to draw a complex scene and only 5ms to calculate the collsions, AI, scripts and so on (one one CPU core!). So I have plenty of CPU power but none of GFX power. So, I try to get as much as possible done on the CPU... :)

Note, I have two tasks running, one is drawing with Irrlicht, the other one does all calculations. Both tasks are sync'd at the end of each frame.
SpaceCombat
http://sourceforge.net/projects/spacecombatgame
(OpenSource space combat simulation)
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: Some ideas on particle systems

Post by REDDemon »

Fading should also be done when a particle is near the Z value not only the camera. This will make smooth particles also near walls (ofcourse not need in a space game XD)
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
Post Reply