Problem with generating shadows
Posted: Mon Feb 13, 2012 7:24 pm
I'm messing around making a prototype for a game, and I have come across a problem that is aggravating me. So far, I have a bit of code which can place a block with left-click, and a light source with right-click. The program doesn't generate lights, just shadows, lights will come later.
My problem is, I only am generating one shadow for each box, I can't have two or more (one for each light source). I'm pretty new to using tables in lua, so I'm guessing my problem is in the implementation of tables, but I've been struggling with this for a little while now to no avail.
Here is my code:
The main parts that are preventing it from working are all of love.update() and the:
part of love.draw().
What I have managed to figure out is that it is overlaying all of the shadows over itself. If that doesn't make sense- just run it. Look how the shadows get darker the more you right click. It is keeping one shape for all of them, for reasons I don't know.
My problem is, I only am generating one shadow for each box, I can't have two or more (one for each light source). I'm pretty new to using tables in lua, so I'm guessing my problem is in the implementation of tables, but I've been struggling with this for a little while now to no avail.
Here is my code:
Code: Select all
function love.load()
boxes = {}
shadows = {}
lightImage = love.graphics.newImage("light.png")
lights = {}
light = {}
length = 0
end
function createBox(x,y)
box = {}
box.size = math.random(20,65)
box.xVelocity = math.random(-5,5)
box.yVelocity = math.random(-5,5)
box.color = {math.random(40,255),math.random(40,255),math.random(40,255)}
box.x = x
box.y = y
box.shadow = {}
box.vertices = {box.x, box.y, box.x +box.size, box.y, box.x + box.size, box.y + box.size, box.x, box.y + box.size}
box.walls = {}
box.walls = {}
box.walls[2] = {box.vertices[1],box.vertices[2],box.vertices[3],box.vertices[4]}
box.walls[3] = {box.vertices[3],box.vertices[4],box.vertices[5],box.vertices[6]}
box.walls[4] = {box.vertices[5],box.vertices[6],box.vertices[7],box.vertices[8]}
box.walls[1] = {box.vertices[7],box.vertices[8],box.vertices[1],box.vertices[2]}
table.insert(boxes, box)
end
function createLight(x,y)
light = {}
light.size = math.random(20,65)
light.color = {math.random(40,255),math.random(40,255),math.random(40,255)}
light.x = x
light.y = y
light.shadows = {}
table.insert(lights, light)
light = {}
end
function love.update(dt)
if #lights > 0 then
for i, v in ipairs (boxes) do
v.shadow = {}
shadow = {}
for i=1, 4 do
for k, j in ipairs(lights) do
number = i+((k-1)*4)
v.shadow[number] = (getShadow(v.walls[i], j.x, j.y))
end
end
end
end
end
function love.mousepressed(x, y, button)
if button == 'l' then
createBox(x, y)
else
createLight(x,y)
end
end
function getShadow(vertices, lightX, lightY)
x1 = vertices[1]
y1 = vertices[2]
ydist = y1-lightY
xdist = x1-lightX
x2 = vertices[3]
y2 = vertices[4]
ydist2 = y2-lightY
xdist2 = x2-lightX
vertices[7] = x1+xdist*200
vertices[8] = y1+ydist*200
vertices[5] = x2+xdist2*200
vertices[6] = y2+ydist2*200
return(vertices)
end
function love.draw()
love.graphics.setBackgroundColor(30,30,30)
--for i, v in ipairs (lights) do
--love.graphics.setColor(255,255,255,100)
--love.graphics.draw(lightImage, v.x-300, v.y-300)
--end
for i, v in ipairs (boxes) do
for i=1, #v.shadow do
love.graphics.setColor(10,10,10,20)
love.graphics.polygon('fill', v.shadow[i])
end
love.graphics.setColor(250,10,100)
love.graphics.print(#v.shadow, 400,400)
end
for i, v in ipairs(lights) do
love.graphics.circle('fill', v.x, v.y, 10)
end
love.graphics.setColor(200,50,50)
for i, v in ipairs (boxes) do
love.graphics.setColor(v.color)
love.graphics.rectangle('fill', v.x, v.y, v.size, v.size)
end
end
Code: Select all
for i, v in ipairs (boxes) do
for i=1, #v.shadow do
love.graphics.setColor(10,10,10,20)
love.graphics.polygon('fill', v.shadow[i])
end
love.graphics.setColor(250,10,100)
love.graphics.print(#v.shadow, 400,400)
end
What I have managed to figure out is that it is overlaying all of the shadows over itself. If that doesn't make sense- just run it. Look how the shadows get darker the more you right click. It is keeping one shape for all of them, for reasons I don't know.