#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Explosion/Sparks

edited June 2014 in General Posts: 142

I would like to have sparks emit from a certain area say, when a ball falls on something. I have simplified a code to create a circumstance similar to mine. Any advice on how to make this happen?

``````function setup()
balls={}
counter=250
limitCounter = 200
lineX = WIDTH/2
lineY = 200
end

function draw()

background(40, 40, 50)

counter=counter+1
if counter>limitCounter then
if limitCounter < 181 and limitCounter > 80 then
limitCounter = limitCounter - 10
elseif limitCounter < 81 and limitCounter > 40 then
limitCounter = limitCounter - 5
elseif limitCounter < 41 and limitCounter > 30 then
limitCounter = limitCounter - 1
elseif limitCounter < 31 then
limitCounter = limitCounter - 0.1
end
counter=0
create()
end
fill(255,255,255,255)
rect(lineX,lineY,400,50)
for a,b in pairs(balls) do
ellipse(b.x,b.y,30)
b.y=b.y-5
if b.x>lineX and b.x<lineX+400 and b.y>240 and b.y<250 then
table.remove(balls,a)
sound(SOUND_PICKUP, 8734)
end
if b.y<0 then
sound("Game Sounds One:Wrong")
table.remove(balls,a)
end
end
end

function create()
table.insert(balls,vec2(math.random(50,WIDTH-50),HEIGHT))
end

function touched(t)
if lineX~=nil then
lineX=lineX+t.deltaX
end
end
``````
Tagged:

• Posts: 425

I've implemented a very basic particle engine but I bet you could make it better:

``````--# Main

function setup()
balls={}
counter=250
limitCounter = 200
lineX = WIDTH/2
lineY = 200
explosions={}
end

function draw()

background(40, 40, 50)

counter=counter+1
if counter>limitCounter then
if limitCounter < 181 and limitCounter > 80 then
limitCounter = limitCounter - 10
elseif limitCounter < 81 and limitCounter > 40 then
limitCounter = limitCounter - 5
elseif limitCounter < 41 and limitCounter > 30 then
limitCounter = limitCounter - 1
elseif limitCounter < 31 then
limitCounter = limitCounter - 0.1
end
counter=0
create()
end
fill(255,255,255,255)
rect(lineX,lineY,400,50)
for a,b in pairs(balls) do
ellipse(b.x,b.y,30)
b.y=b.y-5
if b.x>lineX and b.x<lineX+400 and b.y>240 and b.y<250 then
table.insert(explosions,Explosion(b.x,b.y))
table.remove(balls,a)
sound(SOUND_PICKUP, 8734)
end
if b.y<0 then
sound("Game Sounds One:Wrong")
table.remove(balls,a)
end
end
for i=#explosions,1,-1 do
if not explosions[i].alive then
table.remove(explosions,i)
end
end
for i=1,#explosions do
explosions[i]:draw()
end
end

function create()
table.insert(balls,vec2(math.random(50,WIDTH-50),HEIGHT))
end

function touched(t)
if lineX~=nil then
lineX=lineX+t.deltaX
end
end
--# Explosion
Explosion = class()

function Explosion:init(x,y)
self.numSparks=40
self.sparks={}
self.alive=true
local vel=vec2(0,20)
for i=1,self.numSparks do
table.insert(self.sparks,Spark(x,y,vel))
end
end

function Explosion:draw()
for i=#self.sparks,1,-1 do
if not self.sparks[i].alive then
table.remove(self.sparks,i)
end
end
if #self.sparks==0 then self.alive=false end
for i=1,#self.sparks do
self.sparks[i]:draw()
end
end

Spark=class()
function Spark:init(x,y,vel)
self.pos=vec2(x,y)
self.vel=vel*math.random(60,140)/100
self.damp=0.9
self.alive=true
end
function Spark:draw()
pushStyle()
self.vel = self.vel * self.damp
self.pos = self.pos + self.vel