Particle system - framerate dependant?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Particle system - framerate dependant?

Post by suliman »

Hi
So my particles (smoke, explosions) look very different depending on the framerate. When framerate goes down the particles emission gets proportionally sparser and when it goes out the emissions get denser (so it looks smoother).

1. But the emitters are set to emit X particles per second right? So this should be framerate-independant... Im confused. Any solution?
2. How about particle lifetime? Are those also actually controlled by number of updates rather than actual time passed?
3. Since i use createSphereEmitter() is must drop it when im done right? But if i do it crashes at sceneManager->drawAll() with:

Code: Select all

Unhandled exception at 0x08d29e18 in game.exe: 0xC000001D: Illegal Instruction.
So right now i remove it instead of dropping it. Seems to work alright but this seems wrong...

Thanks!
Erik
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Particle system - framerate dependant?

Post by CuteAlien »

About 3. - you should drop() as soon as you did set it. Can't tell why it crashes in your case without seeing the full code.

Don't know about 1. and 2. right now, have to write some test-program for that when I find some time.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Re: Particle system - framerate dependant?

Post by suliman »

Nr 3 : You are right! I did drop it right away after creation:)

About 1 and 2, i really need to solve this. Anyone? If not using fixed framerate, i dont see how any particle engines can be used unless this functionality exists... Effects looks COMPLETELY different depending on framerate now.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Particle system - framerate dependant?

Post by CuteAlien »

Well, 1 and 2 should be fps independent. If they are not there's a bug. That's why I can't help without having a test application first which reproduces the problem. If you have one already it would be easier to debug - otherwise can't do anything until I find time to write it myself and then hope I can reproduce this.

Without looking at the code - I suppose one problem might be distributing the emitting over the second. As it can't know when starting to emit how the fps will be in the future.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Re: Particle system - framerate dependant?

Post by suliman »

But doesnt the particle system look at time passed since last call? Otherwise how can they (even theoretically) be emissions per second?

To update my physics etc i use this to get timeDelta (time passed for the tick/update loop)

Code: Select all

void world::doTime()
{
    static u32 timeOld=0;
    if(!timeOld)
        timeOld=myDevice->getTimer()->getRealTime();
 
    u32 time=myDevice->getTimer()->getRealTime();
    delta = ((float)time - (float)timeOld) / 1000.f;
    timeOld=time;
 
    gameDelta=delta*(float)gameSpeed;
 
    if(!myDevice->isWindowActive() || delta>0.3){
        gameDelta=0;
        delta=0;
    }
}
Using this i know how far to move items/to move camera/ etc per update. Otherwise it wouldbe be framerate-independant.

The particle systems must use something similar in their update right?
What about this that i found in the emitter code? Could it be something useful?

Code: Select all

class IParticleEmitter : public virtual io::IAttributeExchangingObject
{
public:
 
    //! Prepares an array with new particles to emitt into the system
    /** \param now Current time.
    \param timeSinceLastCall Time elapsed since last call, in milliseconds.
    \param outArray Pointer which will point to the array with the new
    particles to add into the system.
    \return Amount of new particles in the array. Can be 0. */
    virtual s32 emitt(u32 now, u32 timeSinceLastCall, SParticle*& outArray) = 0;
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Re: Particle system - framerate dependant?

Post by suliman »

I might be able to do it manually each tick using my own deltaTime. Like this for each particle system:

Code: Select all

 
pc->getEmitter()->setMinParticlesPerSecond(pc->orignialEm*deltaTime);
pc->getEmitter()->setMaxParticlesPerSecond(pc->orignialEm*deltaTime);
 
But really i must be missing something? This should be somewhere in the engine already right?
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Particle system - framerate dependant?

Post by CuteAlien »

Yes, it should check time and there is code in there to do that. Which is why I need an example first that reproduced the bug so I can debug what's really going on. AFAIK each emitter coded that stuff on it's own. So the code for that should be in CParticleSphereEmitter.cpp if you want to take a look yourself. It's really hard to tell why it's not working when I don't even have an example yet for the part that doesn't work...
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Re: Particle system - framerate dependant?

Post by suliman »

Ok but do you want the code? Or a binary or the entire project? (that will be some work...)
But i havent changed anything in the engine regarding the particle systems.

This is the code:

1. This is how i create them:

Code: Select all

void world::createEffect( vect3d pos,char * textureName,float emissionTime,float force,float spawnMin,float spawnMax,float lifeMin,float lifeMax,float sizeMin,float sizeMax,SColor startColorMin/*=white*/,SColor startColorMax/*=white*/,SColor fadeColor/*=SColor(0,0,0,0)*/,float fadeColorTime/*=100*/,float gravity/*=0*/,float gravityDelay/*=1000*/,float endScale/*=1*/,float radius/*=1*/,int blendType/*=1*/,vector3df speedVector/*=vector3df(0,0,0)*/,float maxAngles/*=360*/,vector3df systemSpeed/*=vector3df(0,0,0)*/,gameObject * followThis/*=0*/ )
{
    objectList.push_back(new gameObject);
    gameObject * o=objectList.back();
    o->curIt=objectList.end();
    o->curIt--;
 
    theNewObject=o;
    o->type=-1; //is effect
    o->pos=pos;
    o->team=-1;
    o->removeMe=0;
    o->speed=systemSpeed;
    o->lockedTarget=0;
    o->shootingUnit=0;
    o->node=0;
    o->followThis=followThis;
 
    o->psTimeLeft=emissionTime/1000.0;
    o->timeLeft=emissionTime/1000.0+0.5+lifeMax/1000.0;
 
    o->ps = gfx.smgr->addParticleSystemSceneNode(false);
    o->ps->setPosition(pos);
 
    if(!speedVector.getLength())
        speedVector=vector3df(0,force,0);
 
    scene::IParticleEmitter* em = o->ps->createSphereEmitter(
        vect3d(0,0,0),  //position offset
        radius,         //radius of emitter
        speedVector,     //dir/speed
        spawnMin,spawnMax,           //emissions min/max
        startColorMin,startColorMax, //start color min/max
        lifeMin,lifeMax,
        maxAngles, //maxAngleDegrees
        dim2df(sizeMin,sizeMin),dim2df(sizeMax,sizeMax));
 
    o->ps->setEmitter(em);
    em->drop();
 
    scene::IParticleAffector* paf;
 
    paf = o->ps->createFadeOutParticleAffector(fadeColor,fadeColorTime);
    o->ps->addAffector(paf);
    paf->drop();
 
    if(gravity){
        paf = o->ps->createGravityAffector(vect3d(0,gravity,0),gravityDelay);
        o->ps->addAffector(paf);
        paf->drop();
    }
 
    if(endScale!=1){
        paf = o->ps->createScaleParticleAffector(dim2df(endScale,endScale));
        o->ps->addAffector(paf);
        paf->drop();
    }
 
 
    o->ps->setMaterialFlag(video::EMF_LIGHTING, false);
    sprintf(tempText,"gfx/particles/%s.png",textureName);
    o->ps->setMaterialTexture(0, gfx.driver->getTexture(tempText));
 
    if(blendType==1)
        o->ps->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR);
    if(blendType==2){
        o->ps->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL);
    }   
}
2. During update this is all I do (move). Otherwise i dont deal with them.

Code: Select all

        
gameObject->ps->setPosition(gameObject->pos);
 
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Particle system - framerate dependant?

Post by CuteAlien »

What I need is code which I can debug. Aka the minimal example that reproduces the bug. I guess in this case it's probably a camera, an emitter and the typical game-loop. Likely not much code and if I find an hour where I have enough energy and no other tasks I can write that. And hope it reproduces this (or maybe it doesn't in which case I wasted an hour). If you want to save me that hour and increase the chance I take a look at that first instead of my other TODO's then you can help and write such code.

Posting code-snippets which can't be compiled on their own or need external data which we don't have is usually not enough. And code which does all kind of other stuff which is not related to the bug is somewhat annoying (it's easier to reduce code to only contain the bug than to write new code and experiment until you figure out a program that reproduces a bug based on some report).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Re: Particle system - framerate dependant?

Post by suliman »

Ill run some more tests tomorrow... I might have an idea of whats wrong.
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: Particle system - framerate dependant?

Post by REDDemon »

The particle system is Framerate and CPU architecture dependant, not sure if you can call it a bug but that's true.
If particle positions are updated by incremental delta time that's already the issue:

Adding 1 time X, or 10 times X/10 makes a great difference. In the second case due to round error particles move slightly less and appear more compact (more sparse with low fps). Also smooth motions becomes more linear at low fps and potentially bad looking, in addition if you track the position of 1 particle at different framerates you will find the particle will ends up in pretty different positions.

The only way to fix that is by interpolating particles across 2 positions or using a fixed step time update.

Just look at code of AttractionAffector:

Code: Select all

 
core::vector3df direction = (Point - particlearray[i].pos).normalize();
direction *= Speed * timeDelta;  //argh!!! delta time, varyis and cause different error!
 
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
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Re: Particle system - framerate dependant?

Post by suliman »

My problem is mainly with emission rates. Below 60 fps the particles becomes so scarce it is no longer a solid stream but i can se the individual particles...

Even if i try to compensate this by manually changing the emissionrate by using delta (time passed since last gameLoop) as described in my earlier post, it still looks the same... Maybe there is an invisible max to spawn rates... Or its just that it can only emit particles once per loop (the resolution of the updates of the particle systems happens to seldom)...

see this image: ( i force lower fps for testing purposes by adding Sleep() in the main-loop but create the particle system in the exact same way). Clearly it does NOT emitt 500 particles/sec in all the 3 cases...
Image
Last edited by suliman on Mon Oct 12, 2015 3:45 am, edited 2 times in total.
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Re: Particle system - framerate dependant?

Post by suliman »

Another way forward: where is the particle-systems (and/or emitters) actually updated? (particles move, new ones are emitted etc) And can i see the code? I dont find it.

Is it baked into any of these functions perhaps?

Code: Select all

driver->beginScene(true, true, video::SColor(255,100,100,100));
smgr->drawAll();
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Particle system - framerate dependant?

Post by mongoose7 »

Maybe your particles are moving too quickly. It is true that you can't create particles without calling code. So, if the code is called 30 times a second, you can only create particles 30 times each second. So maybe you should slow the particles down or create a broader spread.
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Re: Particle system - framerate dependant?

Post by suliman »

how does the emitter actually work then?

If i set it to emit 300 particles/sec and my current framerate is 30 fps, will it create 10 particles per update then? (to reach 300 emitted/sec).

This may be useful in explosions when particles go different ways, but in my case i do trails for aircraft wings so all particles are simply "left behind" in the air behind the wing tip... Possibly it creates many particles each update but they end up on top of each other so it looks as they are few.
Post Reply