Particle effect from primitive shapes

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
FreeTom
Prole
Posts: 8
Joined: Mon Aug 17, 2015 1:08 pm

Particle effect from primitive shapes

Post 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!
User avatar
eqnox
Prole
Posts: 44
Joined: Fri Jul 31, 2015 2:36 pm

Re: Particle effect from primitive shapes

Post by eqnox »

seems cool! good work.
User avatar
Deano45
Prole
Posts: 2
Joined: Sat Jun 15, 2013 1:11 pm

Re: Particle effect from primitive shapes

Post by Deano45 »

Very Cool! Thanks
"I'm normally not a praying man, but if you're up there, please save me Superman." - Homer Simpson
User avatar
adnzzzzZ
Party member
Posts: 305
Joined: Sun Dec 26, 2010 11:04 pm
Location: Porto Alegre, Brazil

Re: Particle effect from primitive shapes

Post by adnzzzzZ »

This is really cool. Good job
User avatar
qubodup
Inner party member
Posts: 775
Joined: Sat Jun 21, 2008 9:21 pm
Location: Berlin, Germany
Contact:

Re: Particle effect from primitive shapes

Post by qubodup »

Nice!

lg.newImage("cat.png") -- made possible by lg = love.graphics
-- Don't force fullscreen (it frustrates those who want to try your game real quick) -- Develop for 1280x720 (so people can make HD videos)
paul54000
Prole
Posts: 22
Joined: Mon Nov 28, 2016 12:19 am

Re: Particle effect from primitive shapes

Post by paul54000 »

the code don't work
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Particle effect from primitive shapes

Post 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
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
paul54000
Prole
Posts: 22
Joined: Mon Nov 28, 2016 12:19 am

Re: Particle effect from primitive shapes

Post 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 ?
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Particle effect from primitive shapes

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Particle effect from primitive shapes

Post 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
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests