# Tilebased Light - kinda like Minecraft

### » Mon Sep 05, 2011 4:04 pm

Tilebased Light - kinda like Minecraft

Hi everyone! Im new to the forum but not so new to Construct.
Thanks for such a wonderful application! Free of Charge!

Im having a brain meltdown here, and this is my last resort.
I have searched the web for this but I can only find C++ examples and Im no good at interpret that. I have also searched the forums here but I can't find any example which explains this.

As the title say: Tilebased Light, how would you go about doing that?

I have tried to add a light variable to every tile and depending on the distance to the light source it gets a certain light value. Then I used that value to set the color filter using the RGB expression. And that worked well. But with multiple lightsources when the lighted area is overlapping and should get a addative effect, I just can't work it out.

I have also looked at the Bresenham Line algorithm, which some people suggested on the net, like this:

But I can't figure it out...

That would really get me going again.

/MJOneMJOne2011-09-05 16:36:59
B
5
S
1
G
2
Posts: 28
Reputation: 806

### » Mon Sep 05, 2011 4:27 pm

B
160
S
48
G
80
Posts: 7,242
Reputation: 61,512

B
5
S
1
G
2
Posts: 28
Reputation: 806

### » Mon Sep 05, 2011 4:56 pm

Well, I tried.

http://dl.dropbox.com/u/17634050/TileBasedLight1.cap
B
223
S
26
G
13
Posts: 1,768
Reputation: 17,862

### » Mon Sep 05, 2011 5:09 pm

Light goes out in all directions, so its reasonable to assume that the distance between a tile, and a light source would be just like a radius of a circle.
Given you know the xy of the light you can figure the adjacent tiles using cos, and sin. The formula would be lightx+cos(loop angle)*loop radius, and lighty+sin(loop angle)*loop radius, with two loops, loop angle, and loop radius.
Since this is tiles you don't need all 360 degrees, 45 degree increments should suffice. Like wise your radius would increase by the length of your tiles.
That will give you a circle around the light, to change that to an oval change your y offset radius to twice that of your x.
Keep in mind the longer the radius the more degrees you will need.newt2011-09-05 17:11:05
B
160
S
48
G
80
Posts: 7,242
Reputation: 61,512

### » Mon Sep 05, 2011 5:21 pm

aah I will try that newt. Thanks!

But how would you do the blending with multiple lights that illuminates the same tile?

If you also have a gradient drop in light strength in the color filter RGB channels. 255 to 0. White to black.

MJOne2011-09-05 17:23:02
B
5
S
1
G
2
Posts: 28
Reputation: 806

### » Mon Sep 05, 2011 5:23 pm

Nice example there, but that part I have figured out, what I can't figure out is how to blend the tiles when there are many light sources.
B
5
S
1
G
2
Posts: 28
Reputation: 806

### » Mon Sep 05, 2011 5:27 pm

Not sure I would think you would add that particular tiles distance from the light to rgb.
B
160
S
48
G
80
Posts: 7,242
Reputation: 61,512

### » Mon Sep 05, 2011 8:21 pm

I made an example based on Tokinson cap:
http://dl.dropbox.com/u/7871870/construct/tile-light-01.cap
Scirra Employee
B
125
S
45
G
15
Posts: 705
Reputation: 15,313

### » Mon Sep 05, 2011 8:35 pm

[QUOTE=MJOne] Nice example there, but that part I have figured out, what I can't figure out is how to blend the tiles when there are many light sources.[/QUOTE]

Here ya go

http://dl.dropbox.com/u/17634050/TileBasedLight1.cap

Just needed to add one more condition!

Also I recommend NOT using nuovofill or any pixel shaders for something like this; it can really slow your game down. You said something about color filters earlier, best just stick with that!

edit: actually animmaniac's example might be what you're looking for..the lights 'combine' becoming a lot stronger, where as mine just 'blends' the lights.Tokinsom2011-09-05 20:41:58
B
223
S
26
G
13
Posts: 1,768
Reputation: 17,862

Next