I have in place a cell-based lighting system that works basically like this: (Not my actual code)
//Light moves up
for (int x = 0; x < width; x += 1)
{
for (int y = 0; y < height; y += 1)
{
light[x,y] = light[x,y+1] - amount;
}
}
//Light moves down
for (int x = width; x < 0; x -= 1)
{
for (int y = height; y < 0; y -= 1)
{
light[x,y] = light[x,y-1] - amount;
}
}
//Light moves left
for (int x = 0; x < width; x += 1)
{
for (int y = 0; y < height; y += 1)
{
light[x,y] = light[x+1,y] - amount;
}
}
//Light moves right
for (int x = width; x < 0; x -= 1)
{
for (int y = height; y < 0; y -= 1)
{
light[x,y] = light[x-1,y] - amount;
}
}
The variable amount
is determined in each for() loop by if there is a block at x,y.
This should make it so that light darkens faster if there is a block at the position, but the light shows up looking like this:
Light shine out sides oddly
Light is in a tunnel, as you can see the top and bottom blocks are solid but they are not being lit up
Verical tunnel, but it seems to work correctly
Up and down dont seem to work quite right
I should also mention that changing the order of the passes changes in which direction the light propagates incorrectly. For example, if I change the order from up,down,left,right
as shown above to left,right,up,down
, the light is spread correctly in a horizontal tunnel and incorrectly in a vertical tunnel.
SO MY QUESTION IS: What is wrong with my code that creates this obvious miscalculation?
If you need to see more code or explanation or screenshots, I'll be happy to provide.