Tile by tile lighting how would i do this

Hi :),
You can also make use of a 2D-array for the “shadow”-tiles that you will draw over the scene.
In this array you can store the alpha-value for each tile (by default the alpha-value should be 1f).
Then you calculate (with the help of the light’s position) in which part of the array your light is.
After that you can decrease the alpha-value of the tiles next to your light and the tiles next to these tiles and so on…
Maybe like this:


if(tiles[x][y].getPosition() == light.getPosition()) {
    tiles[x][y].setAlpha(0f);
    tiles[x-1][y].setAlpha(0.5f);
    tiles[x+1][y].setAlpha(0.5f);
    tiles[x][y-1].setAlpha(0.5f);
    tiles[x][y+1].setAlpha(0.5f);
    // and so on..
}

When this works, you can try to create a recursive or even iterative algorithm for “the tiles next to the tiles next to the tiles and so on”.
Another possibility is to make the background-colour black and then you can decrease the alpha-value of your normal tiles (cf. this)

Sorry, had to appreciate that post. Made me laugh. :smiley:

I have and I am still getting the same thing where it is dark in the light instead of being light

You said have a look on the graphics (take it out)
which i did and the results were positive

[quote]The inside-out lighting is because of the missing ‘1 - …’ in 1 - distance / MAX_DISTANCE

Try taking it out of the graphs I linked and see what happens
[/quote]

Let’s not over-complicate things before tommohawk has a working naive solution.
Besides, the next step is using a light texture and additive blending instead of faking it with an array.
EDIT: actually I’m not sure what that method is trying to fake, but it sure isn’t as robust as the simple way.

… ??? ??? ??? ??? ??? ??? ??? ??? ???

Hello thanks for the help from people but its day 4 now and i haven’t figured it out yet

if (ENABLED) {
			int camX = (int) (camera.position.x + (MasterTile.TILE_WIDTH / 2) - camera.viewportWidth / 2) / MasterTile.TILE_WIDTH;
			int camY = (int) (camera.position.y + (MasterTile.TILE_HEIGHT / 2) - camera.viewportHeight / 2) / MasterTile.TILE_HEIGHT;
			int camZoomFixX = (int) (((camera.zoom - 1) * 100) / (MasterTile.TILE_WIDTH / 10));
			int camZoomFixY = (int) (((camera.zoom - 1) * 100) / (MasterTile.TILE_WIDTH / 10));
			int offset = 1;

			if (camZoomFixX < 0)
				camZoomFixX = 0;
			if (camZoomFixY < 0)
				camZoomFixY = 0;

			int startX = camX - (camZoomFixX + offset);
			int toX = (camX + TileRenderer.AMOUNT_WIDTH) + (camZoomFixX + offset);
			int startY = camY - (camZoomFixY + offset);
			int toY = (camY + TileRenderer.AMOUNT_HEIGHT) + (camZoomFixY + offset);

			// Lights
			spriteBatch.begin();
			{

				for (int x = startX; x < toX; x++) {
					for (int y = startY; y < toY; y++) {
						if (x < 0 || x > TileRenderer.map.getWidth() - 1 || y < 0 || y > TileRenderer.map.getHeight() - 1)
							continue;

						float tileBrightness = sunlight.a;

						for (int i = 0; i < lights.size; i++) {

							MasterTile tile = lights.get(i);
							if (tile == null)
								continue;
							float distance = Vector2.dst(tile.getX() / MasterTile.TILE_WIDTH, tile.getY() / MasterTile.TILE_HEIGHT, x, y);
							tileBrightness += Math.max(0, 1 - distance / MAX_DISTANCE);
							

						}
						tileBrightness -= 2;
						tileBrightness = Math.min(1, Math.abs(tileBrightness));
						System.out.println(tileBrightness);
						
						Color tileColor = Color.WHITE;
		                tileColor = Utils.mixColours(tileColor, Color.WHITE, tileBrightness);
		                tileColor = Utils.mixColours(tileColor, sunlight, sunlight.a);
		                
		                

						spriteBatch.setColor(tileColor);
						spriteBatch.draw(pixelTexture, x * MasterTile.TILE_WIDTH, y * MasterTile.TILE_HEIGHT, MasterTile.TILE_WIDTH, MasterTile.TILE_HEIGHT);
						spriteBatch.setColor(Color.WHITE);

					}
				}
			}

			spriteBatch.end();
		}

OMFG been doing tests and I thought to myself as i was getting darker colors that i would change the + to - in this equation and it works perfect

tileBrightness -= Math.max(0, 1 - distance / MAX_DISTANCE);

Thanks guys… sorry for the hassle

Question: what is the code for Utils.mixColours?

It is quite a simple code but here you go

	private static Color color = new Color(1,1,1,1);
	
	public static Color mixColours(Color color1,Color color2,float amount){
		color.set((color1.r * color2.r) * amount, (color1.g * color2.g) * amount, (color1.b * color2.b) * amount, (color1.a * color2.a)*amount);
		return color;
	}

Maybe you could try using the built-in libgdx Color lerp method. In my lighting engine I use lerp with 0.5 strength so it’s a 50% interpolation from X colour to Y colour.

Where did you get this code? :slight_smile:
You need to multiply color2 with amount and color1 with (1.0 - amount), component-wise, if you want to interpolate between color1 and color2.

So, in other words:


color.set(color1.r * (1.0f - amount) + color2.r * amount,
          color1.g * (1.0f - amount) + color2.g * amount,
          color1.b * (1.0f - amount) + color2.b * amount,
          color1.a * (1.0f - amount) + color2.a * amount);

This will ensure that the result is color1 if amount equals 0.0, and color2 if amount equals 1.0 (which is what you want).

I do have one problem and that is that i am having lines go in between my tiles… how would i fix such a thing I did look at padding but I don’t know if that would work?

Yeah, well. That is not my color mix method, that is standard old-school linear interpolation, and is quite intuitive. Just think about it for a second. So, did it work then now? All I see is purple text, which is hopefully not what you intended the end result to look like. :slight_smile: