@Rodolphe, how expensive is your light beam? I was curious so I built one myself, it is not too bad but i think it could be optimized. Is this similar to how you did it?
--# Main
-- Light
function setup()
light = Lightbeam()
light:cast()
parameter.watch("1 / DeltaTime")
bodies = {}
for i = 1, 10 do
local m = vec2(math.random(WIDTH), math.random(HEIGHT))
local w, h = math.random(100), math.random(100)
bodies[i] = physics.body(POLYGON, vec2(m.x - w, m.y + h), vec2(m.x + w, m.y + h), vec2(m.x + w, m.y - h), vec2(m.x - w, m.y - h))
bodies[i].gravityScale = 0.1
end
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(0, 148, 255, 255)
-- This sets the line thickness
strokeWidth(2)
-- Do your drawing here
light:draw()
for i,body in ipairs(bodies) do
pushMatrix()
translate(body.x, body.y)
rotate(body.angle)
stroke(150,255,150,255)
strokeWidth(3.0)
local points = body.points
for j = 1,#points do
a = points[j]
b = points[(j % #points)+1]
line(a.x, a.y, b.x, b.y)
end
popMatrix()
end
end
--# Lightbeam
Lightbeam = class()
function Lightbeam:init()
self.mesh = mesh()
self.color = color(220, 220, 155, 255)
self.minWidth = WIDTH / 30
self.maxWidth = WIDTH / 10
self.tpos, self.bpos, self.twidth, self.bwidth = 0,0,0,0
end
function Lightbeam:cast()
self.tpos, self.bpos = math.random(WIDTH), math.random(WIDTH)
self.twidth = math.random(self.minWidth, self.maxWidth)
self.bwidth = math.random(self.twidth, self.maxWidth)
self:update()
end
function Lightbeam:update()
local twidth, bwidth = vec2(self.twidth / 2, 0), vec2(self.bwidth / 2, 0)
local p1, p2 = vec2(self.tpos, HEIGHT), vec2(self.bpos, 0)
local points = {}
local n = 10
for p = 1, n do
points[p] = p1 - twidth + (((twidth*2) / (n - 1)) * (p - 1))
points[p + n] = p2 + bwidth - (((bwidth*2) / (n - 1)) * (p - 1))
end
for i = 1, n do
local ray = physics.raycast(points[i], points[#points - (i - 1)])
if ray ~= nil and ray.point ~= nil then
points[#points - (i - 1)] = ray.point
end
end
self.mesh.vertices = triangulate(points)
self.mesh:setColors(self.color)
end
function Lightbeam:draw()
self:update()
self.mesh:draw()
end