I’ve been working on my per-tile lighting system, currently it is very simple and supports coloured lighting.
PROBLEM 1:
I started to notice that lighting wouldn’t work for random tiles, here is an example:
http://screensnapr.com/e/9JoXox.png
At first i thought it was a problem in my tiling system, however if i disabled lighting the tiles were fine.
Here is my lighting code:
public void updateLightValues() {
for (Tile t : Engine.getMap().getVisibleTiles()) {
Point p1 = new Point((int) x, (int) y);//Player coordinates
double tileDistance = p1.distance((double) t.getX(), (double) t.getY());//the distance from the player to the tile
tileDistance /= Tile.SIZE;//tiles are 16x16 therefore this is the amount of tiles between the player and the tile.
if (tileDistance > lightDistance) continue;//if the distance is further than the light source's max distance, skip this tile.
float light = lightDistance;//the initial light value (alpha value)
light -= tileDistance;
light /= 10;
light += Engine.getMap().getLightModifier();//currently getlightmodifier() returns 0.
if (light < 0) light = 0;
if (light > 1) light = 1;
float a = light + t.getLightValue() * (1 - light);
float r = ((red * a) + (t.getLightRed() * t.getLightValue())) / a;
float g = ((green * a) + (t.getLightGreen() * t.getLightValue())) / a;
float b = ((blue * a) + (t.getLightBlue() * t.getLightValue())) / a;
t.setLightValues(r, g, b, a);
}
}
That method is called every tick and will update light values around the light source.
Note that a light is represented by a LightSource, and the lightsource class holds all variables including this method.
PROBLEM 2:
My second problem is with the colour blending. It seems that a light source will blend into another light source, however the light source it’s blending into won’t be counter-blended, producing a weird blend like this:
http://screensnapr.com/e/diDBhS.png
Thankyou for your time.