How to remove particle effects properly?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Raskol
Prole
Posts: 9
Joined: Mon Mar 03, 2014 1:40 pm
Location: Budapest

How to remove particle effects properly?

Post by Raskol »

Hey everyone. I checked the forum to see if anyone had the same problem but couldn't find it(this one might be close) so I decided to open a topic for it. Here is my issue:

I'm making this little turret-game where you have to shoot down incoming missiles. All of them emit a smoke-trail particle effect(I've usedadnzzzzZ's editor and the attached example for this). I managed to position these correctly but I couldn't figure out how can I stop the particle effects individually. What happens is that they disappear without any fading. I guess this is because I call the function when the missiles are getting stored into their table, and after the removal function there is no emitter anymore. Just a thought. Where should I start drawing the particles? Should I store them in a table just like I did with the missiles?

I've attached the project file. Sorry for my messy code, I'm more of a designer dude. I try to learn proper game-coding with love2d. Some of the snippets are used from tutorials so it's kind of chaotic but I tried to comment it properly.

Thanks in advance
Attachments
turretgame.love
(296.75 KiB) Downloaded 125 times
User avatar
SneakySnake
Citizen
Posts: 94
Joined: Fri May 31, 2013 2:01 pm
Contact:

Re: How to remove particle effects properly?

Post by SneakySnake »

Currently, the game only has one particle system that you are drawing for each enemy.
Instead, you should have a separate particle system for each enemy.
When an enemy gets destroyed, call stop() on the particle system of that enemy.
A good time to remove the particle system would be if it has 0 particles left.
User avatar
adnzzzzZ
Party member
Posts: 305
Joined: Sun Dec 26, 2010 11:04 pm
Location: Porto Alegre, Brazil

Re: How to remove particle effects properly?

Post by adnzzzzZ »

The way I do it is something like this:

Code: Select all

-- Emitters that are time based should be removed once their time is up
            local lifetime = self.particle_systems[i].ps:getEmitterLifetime()
            if lifetime > 0 then
                local t = love.timer.getTime() - self.particle_systems[i].last_get_time
                if t > lifetime then self.particle_systems[i].ps:stop() end
                if t > 3*lifetime then
                    doThingToRemoveParticleSystem()
                end
            end
Ignore the code that isn't relevant, but I simply check for the time that has passed since the particle system has started and remove it after it's been long enough that all the particles in it have already faded out on their own. If you remove the particle system immediately after it's done (t > lifetime, where t = current_time - time_of_particle_system_creation), then the particles that are still being processed will also get removed, which means they won't fade out. So give it a little time (in my case I do 3*lifetime) and then remove it.
User avatar
Raskol
Prole
Posts: 9
Joined: Mon Mar 03, 2014 1:40 pm
Location: Budapest

Re: How to remove particle effects properly?

Post by Raskol »

Thanks for the answers guys.
Currently, the game only has one particle system that you are drawing for each enemy.
This makes a lot of sense, I'm going to re-think the code and will come back whining here if I'm still stuck.
I simply check for the time that has passed since the particle system has started and remove it after it's been long enough that all the particles in it have already faded out on their own.
This is certainly useful in other cases but in my case the particle systems aren't time-based, they need to be removed after destruction which is based on the player's actions so I don't really know how long the particles should be there precisely. Or did I misunderstand you here?
User avatar
ArchAngel075
Party member
Posts: 319
Joined: Mon Jun 24, 2013 5:16 am

Re: How to remove particle effects properly?

Post by ArchAngel075 »

I actually attempted to modify your code to allow for the first method (count particles in system) and had a semi-working outcome

I had assigned each enemy a particle system (enemy.ps) and in the block where the rem_enemy table is looped over, i count the number of particles in the system and if it is 0 i properly deleted the enemy. On adding to the rem_enemy table the particle system was changed to stop emitting.

The only issue was because of how the collision code was done the still fading out enemy was "there" while fading...(I had edited the draw function to not draw the enemy whos in the rem_enemy table, but still draw the particle system)

so instead of storing the particle system in the enemy object rather storing in an external table linking the particle system to the enemy would be best...

I lost the edit i had made after seeing my initial edit didnt work out well enough.
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 6 guests