Page 1 of 1

Need some help with particle systems and things

Posted: Mon Apr 20, 2015 12:15 am
by luaiscool
I've been messing around with making a game, simple space. Heres what I have:

Code: Select all

player = {}
player.x = 400
player.y = 400
player.r = 0
player.image = nil
player.rspeed = 2
player.speed = 100

particles = {}
particles.fire = {}
particles.fire.buffer = 32
particles.fire.system = nil

function love.load()
	
	love.graphics.setBackgroundColor( 50, 50, 50)
	player.image = love.graphics.newImage("player.png")
	particles.fire.image = love.graphics.newImage("fire.png")
	particles.fire.system = love.graphics.newParticleSystem(particles.fire.image, particles.fire.buffer);
	particles.fire.system:setParticleLifetime(2, 3); -- Particles live at least 2s and at most 5s.
    particles.fire.system:setEmissionRate(5);
    particles.fire.system:setSizeVariation(1);
    particles.fire.system:setLinearAcceleration(-20, 0, 0, 0); -- Random movement in all directions.
	
end
function love.update(dt)
particles.fire.system:update(dt);
if love.keyboard.isDown('up') then
    player.x = player.x + math.cos(player.r) * player.speed * dt
    player.y = player.y + math.sin(player.r) * player.speed * dt
  end
  if love.keyboard.isDown('down') then
    player.x = player.x - math.cos(player.r) * player.speed * dt
    player.y = player.y - math.sin(player.r) * player.speed * dt
  end
  if love.keyboard.isDown('left') then
    player.r = player.r - player.rspeed * dt
  end
  if love.keyboard.isDown('right') then
    player.r = player.r + player.rspeed * dt
  end  
end
function love.draw(dt)
	love.graphics.draw(player.image, player.x, player.y, player.r, 1, 1, 32, 27)
	love.graphics.draw(particles.fire.system, player.x - 8, player.y - 16, player.r);
end
This is what happens:
[youtube]https://www.youtube.com/watch?v=1j9WyrvdZx8[/youtube]

Re: Need some help with particle systems and things

Posted: Mon Apr 20, 2015 4:18 am
by Snake174rus
You need to update particle system position in update function.

Code: Select all

X = x0 + (x - x0) * cos(a) - (y - y0) * sin(a);
Y = y0 + (y - y0) * cos(a) + (x - x0) * sin(a);

particles.fire.system:setPosition( X, Y )

(x0;y0) - player center
(x;y) - point from particles starts
a - angle in radians
And draw:

Code: Select all

love.graphics.draw(particles.fire.system, 0, 0, player.r);

Re: Need some help with particle systems and things

Posted: Mon Apr 20, 2015 10:56 pm
by luaiscool
Snake174rus wrote:You need to update particle system position in update function.

Code: Select all

X = x0 + (x - x0) * cos(a) - (y - y0) * sin(a);
Y = y0 + (y - y0) * cos(a) + (x - x0) * sin(a);
Where do I declare these x and y variables and what do I set them too?

Re: Need some help with particle systems and things

Posted: Tue Apr 21, 2015 2:55 am
by Snake174rus
Try that:

Code: Select all

local X = (player center x) + ((particle position x) - (player center x)) * cos(a) - ((particle position y) - (player center y)) * sin(a);
local Y = (player center y) + ((particle position y) - (player center y)) * cos(a) + ((particle position x) - (player center x)) * sin(a);

particles.fire.system:setPosition( X, Y )

particles.fire.system:update(dt);
if love.keyboard.isDown('up') then
    player.x = player.x + math.cos(player.r) * player.speed * dt
    player.y = player.y + math.sin(player.r) * player.speed * dt
  end
  if love.keyboard.isDown('down') then
    player.x = player.x - math.cos(player.r) * player.speed * dt
    player.y = player.y - math.sin(player.r) * player.speed * dt
  end
  if love.keyboard.isDown('left') then
    player.r = player.r - player.rspeed * dt
  end
  if love.keyboard.isDown('right') then
    player.r = player.r + player.rspeed * dt
  end  
end
Download example for TANK from HERE at the end of article (RU).
Look how I'm doing the exhaust gases.

Direct link