Page 1 of 1

Particle System won't emit properly.

Posted: Sun Mar 29, 2020 3:14 am
by Azzla
Hi. I've been working on my first LOVE2D project for a week or so and I'm trying to add some simple particle effects to the game.

The problem is that every emission call during the game results in particles shooting out on a specific axis, while the particles should just shoot out of the zombie's position in a radial manner. I've tried messing around with these functions listed on the documentation:

Code: Select all

pSystem:setEmissionArea()
pSystem:setDirection()
However both have not solved the issue as setDirection() only allows me to change which axis the particles fly out towards. I want them to fly out radially from my zombie instead. A terrible pseudo-code example to demonstrate the idea: pSystem:setDirection(random_angle_relative_to_center_of_zombie_object). Currently my code for the particle system is as follows:

Code: Select all

particles = {}

function spawnParticle(x, y, speed, pSprite, pFX)
  pFX.x = x
  pFX.y = y
  pFX.pSystem = love.graphics.newParticleSystem(pSprite, 20)
  
  pFX.pSystem:setParticleLifetime(.5,1)
  pFX.pSystem:setLinearAcceleration(-80,-80,80,80)
  pFX.pSystem:setSpeed(speed/2, speed)
  table.insert(particles, pFX)
  
  return pFX
end

function particleUpdate(dt)
  for i,p in ipairs(particles) do
    p.pSystem:update(dt)
  end
end
I am just calling a simple pSystem:emit(numOfParticles) function whenever a bullet collides with a zombie. Any help with this issue will be appreciated, thanks.

Re: Particle System won't emit properly.

Posted: Sun Mar 29, 2020 1:15 pm
by MrFariator
By shooting out particles radially, I assume you mean that the particles need to emit in a circle from origin? I have not messed around with löve's particle system much, but as far as I've understood it, to get a radial splash of particles going you need to do something along the lines of:

Code: Select all

-- initialization
local numberOfParticles = 100
local psystem = love.graphics.newParticleSystem(image_goes_here, numberOfParticles)
psystem:setParticleLifetime ( 10,10 )
psystem:setSizeVariation ( 1 )
psystem:setSpeed ( 20, 20 )
psystem:setColors ( 1, 1, 1, 1, 1, 1, 1, 0 )

-- spawn particles
local direction = 0
local increment = (math.pi*2) / numberOfParticles
for i = 1, numberOfParticles do
  psystem:setDirection ( direction )
  psystem:emit (1)
  direction = direction + increment
end
So essentially instead of using setLinearAcceleration(), you manually go through the angles to which you want particles to spawn, and emit the particles one by one.

Example of output (but 30 particles instead of 100, and speed 10 instead of 20; not a perfectly looped gif hence the stutter):
Image

Re: Particle System won't emit properly.

Posted: Sun Mar 29, 2020 2:31 pm
by pgimeno
I believe you're supposed to use ParticleSystem:setLinearAcceleration for that, see example in love.graphics.newParticleSystem.

Re: Particle System won't emit properly.

Posted: Mon Mar 30, 2020 4:17 am
by Azzla
MrFariator wrote: Sun Mar 29, 2020 1:15 pm

Code: Select all

-- spawn particles
local direction = 0
local increment = (math.pi*2) / numberOfParticles
for i = 1, numberOfParticles do
  psystem:setDirection ( direction )
  psystem:emit (1)
  direction = direction + increment
end
So essentially instead of using setLinearAcceleration(), you manually go through the angles to which you want particles to spawn, and emit the particles one by one.
Exactly the effect I was looking for. Thanks! :D