Make my own particle system

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
JME
Prole
Posts: 7
Joined: Mon Mar 04, 2013 7:49 pm
Location: Gothenburg, Sweden

Make my own particle system

Post by JME »

I am doing a weather simulator for my game and I thought the built-in particle system would be excellent for rendering snowflakes. It turns out those particles cannot have values changed after they have appeared so if the player press a button so the screen moves in a certain direction it now looks like it starts to blow in the other direction(the X position of the snowflakes remains the same).

I have an image of a snowflake I would like to render 60 or so times at once, on different positions. When a snowflake reaches the end of the screen it should spawn at the other end. That is on both axes so when Y > ScreenHeight then Y = -SnowflakeHeight and if X > ScreenWith then X = -SnowflakeWith and if X < -SnowflakeWith then X = Screenwith.

I tried to make a function that draws all snowflakes and loop that function in the draw function but I am terrible at looping and it crached upon starting the game. :(
If you understand me or not, that is not my problem.
User avatar
Sasha264
Party member
Posts: 131
Joined: Mon Sep 08, 2014 7:57 am

Re: Make my own particle system

Post by Sasha264 »

  1. Easy way: use existing particle system along with https://love2d.org/wiki/love.graphics.translate that way you can move all particles at once when player press "a" button and particles need to be moved all at once.
  2. Laggy way: do not use any particles or batches or instances, just plain draw call for every snowflake. Will lag if you need more then 1000 (maybe 10000 max on top pc) snowflakes.
  3. Hard way: do not use existing particle system, use https://love2d.org/wiki/love.graphics.drawInstanced to draw your own "particles". With custom format for attached mesh https://love2d.org/wiki/Mesh:attachAttribute you can specify snowflake position, direction, speed, oscillation period, whatever you want, and use that in vertex shader to calculate actual snowflake position every frame. Then with https://love2d.org/wiki/Mesh:setVertexAttribute you can change parameters for exact one particle (for example that one that reached bottom border) to whatever you want after particle was created and lived for some time.
pauljessup
Party member
Posts: 356
Joined: Wed Jul 03, 2013 4:06 am

Re: Make my own particle system

Post by pauljessup »

Eh? You can specify the x and y, using love.graphics.draw(ParticleSystem, x, y), where ParticleSystem is the object created with something like

Code: Select all

          self.imgs=love.graphics.newImage("game/img/snowflake.png")
            ParticleSystem=love.graphics.newParticleSystem(self.imgs, 450)
            ParticleSystem:setSpeed(0.03)
            ParticleSystem:setLinearAcceleration(3, 6, 8, 12)
            ParticleSystem:setEmissionRate(20)
and then, when you want to start emitting particles you do

Code: Select all

ParticleSystem:emit(1)
Then in the update, you want to move and emit with each update, and this will add more snow, and these can all be done relative to the screen location with the love.graphics.draw call above

Code: Select all


            ParticleSystem:moveTo(love.math.random(map.camera.x-320, map.camera.x+320), love.math.random(map.camera.y-200, map.camera.y+200))
            ParticleSystem:update(dt)
note- this is mostly pseudo code to give you an idea on how a falling snow could work. I think you might still add spin and stuff too. I've done this same basic thing, and this is roughly what I did. It's nice, because it's easy to reuse most of the code with slight changes for falling ashes from a fire, or rain falling, etc.

when it's done just call-
ParticleSystem:stop()

and then when you want to start it up again, just call emit above.
User avatar
JME
Prole
Posts: 7
Joined: Mon Mar 04, 2013 7:49 pm
Location: Gothenburg, Sweden

Re: Make my own particle system

Post by JME »

I made it my own way. Thanks for trying to help me anyway.
If you understand me or not, that is not my problem.
Post Reply

Who is online

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