Page 1 of 1

Particle effect from primitive shapes

Posted: Mon Aug 17, 2015 1:31 pm
by FreeTom
Hi all,

I've been playing with Love2D for about a month now and couldn't find code for a nice simple example of a particle effect made from basic shapes rather than loaded images. So, now I've managed to make one, I thought I'd post it for the benefit of others:

Code: Select all

------------------------------------------------------
--	Simple fire effect with canvas and particle system
------------------------------------------------------

-- Returns canvas of given width and height containing a white circle
function initCanvas (width, height)
	local c = love.graphics.newCanvas(width, height)
	love.graphics.setCanvas(c) -- Switch to drawing on canvas 'c'
	love.graphics.setBlendMode("alpha")
	love.graphics.setColor(255, 255, 255, 255)
	love.graphics.circle("fill", width / 2, height / 2, 10, 100)
	love.graphics.setCanvas() -- Switch back to drawing on main screen
	return c
end

-- Returns particle system with given image and maximum particles to mimic fire
function initPartSys (image, maxParticles)
	local ps = love.graphics.newParticleSystem(image, maxParticles)
	ps:setParticleLifetime(0.1, 0.5) -- (min, max)
	ps:setSizeVariation(1)
	ps:setLinearAcceleration(-200, -2000, 200, 100) -- (minX, minY, maxX, maxY)
	ps:setColors(234, 217, 30, 128, 224, 21, 21, 0) -- (r1, g1, b1, a1, r2, g2, b2, a2 ...)
	return ps
end

function love.load ()
	love.window.setTitle("Left-click to make fire!")
	local canvas = initCanvas(20, 40)
	psystem = initPartSys (canvas, 1500)
end

function love.mousemoved (x, y, dx, dy)
	-- To move the particles along with the emitter, do this offset in love.graphics.draw instead of setPosition
	psystem:setPosition(x, y)
end

function love.update (dt)
	-- Only emit particles when left mouse button is down
	if love.mouse.isDown("l") then
		psystem:setEmissionRate(3000)
	else
		psystem:setEmissionRate(0)
	end
	
	-- Particle system should usually be updated from within love.update
	psystem:update(dt)
end

function love.draw ()
	-- Try different blend modes out - https://love2d.org/wiki/BlendMode
	love.graphics.setBlendMode("additive")
	
	-- Redraw particle system every frame
	love.graphics.draw(psystem)
end

In conclusion: hurrah!

Re: Particle effect from primitive shapes

Posted: Mon Aug 17, 2015 10:28 pm
by eqnox
seems cool! good work.

Re: Particle effect from primitive shapes

Posted: Sun Oct 04, 2015 12:21 pm
by Deano45
Very Cool! Thanks

Re: Particle effect from primitive shapes

Posted: Sun Oct 04, 2015 7:55 pm
by adnzzzzZ
This is really cool. Good job

Re: Particle effect from primitive shapes

Posted: Thu Oct 08, 2015 7:47 pm
by qubodup
Nice!


Re: Particle effect from primitive shapes

Posted: Tue Nov 29, 2016 11:18 pm
by paul54000
the code don't work

Re: Particle effect from primitive shapes

Posted: Tue Nov 29, 2016 11:51 pm
by zorg
paul54000 wrote:the code don't work
The code in the OP was probably made for 0.9.2, and the current version is 0.10.2

Re: Particle effect from primitive shapes

Posted: Wed Nov 30, 2016 12:47 am
by paul54000
zorg wrote:
paul54000 wrote:the code don't work
The code in the OP was probably made for 0.9.2, and the current version is 0.10.2
why different version of Löve are not retrocompatible ?

Re: Particle effect from primitive shapes

Posted: Wed Nov 30, 2016 4:23 am
by zorg
paul54000 wrote:
zorg wrote:
paul54000 wrote:the code don't work
The code in the OP was probably made for 0.9.2, and the current version is 0.10.2
why different version of Löve are not retrocompatible ?
Major versions (like 9.x.x and 10.x.x) aren't; though that doesn't mean that with some work, one couldn't update the code.

Re: Particle effect from primitive shapes

Posted: Wed Nov 30, 2016 5:12 pm
by Positive07
This just has two problems, first one is that the "additive" [wiki]BlendMode[/wiki] has been renamed "add", and second that the "l" [wiki]MouseConstant[/wiki] is now 1

So with those changes the code is

Code: Select all

------------------------------------------------------
--   Simple fire effect with canvas and particle system
------------------------------------------------------

-- Returns canvas of given width and height containing a white circle
function initCanvas (width, height)
   local c = love.graphics.newCanvas(width, height)
   love.graphics.setCanvas(c) -- Switch to drawing on canvas 'c'
   love.graphics.setBlendMode("alpha")
   love.graphics.setColor(255, 255, 255, 255)
   love.graphics.circle("fill", width / 2, height / 2, 10, 100)
   love.graphics.setCanvas() -- Switch back to drawing on main screen
   return c
end

-- Returns particle system with given image and maximum particles to mimic fire
function initPartSys (image, maxParticles)
   local ps = love.graphics.newParticleSystem(image, maxParticles)
   ps:setParticleLifetime(0.1, 0.5) -- (min, max)
   ps:setSizeVariation(1)
   ps:setLinearAcceleration(-200, -2000, 200, 100) -- (minX, minY, maxX, maxY)
   ps:setColors(234, 217, 30, 128, 224, 21, 21, 0) -- (r1, g1, b1, a1, r2, g2, b2, a2 ...)
   return ps
end

function love.load ()
   love.window.setTitle("Left-click to make fire!")
   local canvas = initCanvas(20, 40)
   psystem = initPartSys (canvas, 1500)
end

function love.mousemoved (x, y, dx, dy)
   -- To move the particles along with the emitter, do this offset in love.graphics.draw instead of setPosition
   psystem:setPosition(x, y)
end

function love.update (dt)
   -- Only emit particles when left mouse button is down
   if love.mouse.isDown(1) then
      psystem:setEmissionRate(3000)
   else
      psystem:setEmissionRate(0)
   end
   
   -- Particle system should usually be updated from within love.update
   psystem:update(dt)
end

function love.draw ()
   -- Try different blend modes out - https://love2d.org/wiki/BlendMode
   love.graphics.setBlendMode("add")
   
   -- Redraw particle system every frame
   love.graphics.draw(psystem)
end
And here is a lovefiddle because I can