Re: How to do lightning
Posted: Fri Jul 29, 2022 6:50 pm
Was skeptical to give this a try since I already had a working one but ...
But just gave it a try and...:
It works like magic, lightning magic! My other version produces loops sometimes and kind of looks weird once in a while.
darkfrei wrote: ↑Thu Jul 14, 2022 10:30 am (not tested)
Code: Select all
local source ={x=0, y=0} local target ={x=800, y=600} local lightning= { source=source, target=target, mainLine={source, target}, } function addPoint (lightning, index) -- index = math.random(#lightning.mainLine -1) local x1=lightning.mainLine[index].x local y1=lightning.mainLine[index].y local x2=lightning.mainLine[index+1].x local y2=lightning.mainLine[index+1].y local t = 0.25 + 0.5*math.random() local x = x1+ t*(x2 - x1) local y = y1+ t*(y2 - y1) x = x + 0.25*(y2 - y1)* (math.random()-1) y = y + 0.25*(x2 - x1)* (math.random()-1) table.insert (lightning.mainLine, index+1, {x=x,y=y}) end
But just gave it a try and...:
Code: Select all
local source ={x=0, y=0}
local target ={x=300, y=love.graphics.getHeight()}
local lightning= {
source=source,
target=target,
mainLine={source, target},
}
function addPoint (lightning, index) --
index = math.random(#lightning.mainLine -1)
local x1=lightning.mainLine[index].x
local y1=lightning.mainLine[index].y
local x2=lightning.mainLine[index+1].x
local y2=lightning.mainLine[index+1].y
local t = 0.25 + 0.5*math.random()
local x = x1+ t*(x2 - x1)
local y = y1+ t*(y2 - y1)
x = x + 0.25*(y2 - y1)* (math.random()-1)
y = y + 0.25*(x2 - x1)* (math.random()-1)
table.insert (lightning.mainLine, index+1, {x=x,y=y})
end
function reload()
lightning.mainLine = {
lightning.mainLine[1],
lightning.mainLine[#lightning.mainLine]
}
for x = 1 , math.random(30,50) do--or 10,18 or anything
addPoint(lightning)
end
end
local t = .1
local time = 0
function love.update(dt)
time = time -dt
if time <= 0 then
time = t
reload()
end
end
function love.draw()
for xx, ii in ipairs(lightning.mainLine) do
local o = lightning.mainLine[xx-1]
local ox = o and o.x or ii.x
local oy = o and o.y or ii.y
love.graphics.line(ox,oy,ii.x,ii.y)
end
end