Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am working on a top-down, 2d cell-based RPG game. I would like to implement a cell based lighting system, something like this and this.

I basically have several light sources and light deteriorates on every cell in every direction. Something like the below (numbers represent the level of light in each cell):

1 1 1 1 1 0 0 0 0 0 0
1 2 2 2 1 0 0 1 1 1 0
1 2 3 2 1 0 0 1 2 1 0
1 2 2 2 1 0 0 1 1 1 0
1 1 1 1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 1 0 0 0 0
0 0 0 0 1 2 1 0 0 0 0
0 0 0 0 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0

So basically I insert a source of light of "strength" x at a given spot. I then set the surrounding cells' light strength to x-1 and so on until I reach zero (or a set global minimum value). I am looking for an efficient algorithm to implement and generate such a lightning grid based on my light sources and any tips regarding such implementation.

I am currently not looking into field of view algorithms, just an efficient way of generating the above. Any help will be much appreciated.

EDIT: Just to clarify, this should handle movement of light sources (e.g. player walking with a torch).

EDIT2: Ok, I've solved my problem myself based on the simple distance suggestion by Jari (thus I'll accept his answer although I'm still looking for a more efficient way of generating such array). If anyone's interested here is a demo of what I came up with (multiple light sources with alpha compositing)

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

I'd probably just create a 256x256 (or some such) pre-calculated array and scaled that as needed; that way you can do all sorts of special lights if you want as well.

Going procedurally, the easiest way would probably be to just calculate the distance.

xd = posx-centerx
yd = posy-centery
dist = sqrt(xd*xd+yd*yd)
pow = maxpow - dist
if (pow < 0) pow = 0

and then just draw a (maxpow*2)-1 square around the light center..

Something like that. Doesn't give exactly the results you mentioned, because this one generates a more round light (yours are rather square).

share|improve this answer
1  
Well a round light is good (as seen here). Your algorithm works well but only for one light source, any suggestions for handling multiple light sources? Let's say I have a list of light sources, should I, for each cell, calculate the distance from those light sources and sum up illumination - is that efficient? How should I add up illumination? –  MMM Feb 6 '12 at 11:21
    
Just a thought, should it be max(distance_a, distance_b, ...)? –  MMM Feb 6 '12 at 11:26
1  
I said, "just draw a (maxpow*2)-1 square around the light center" - do this for each light source, adding them together, capping (with max function) to some sane maximum value. –  Jari Komppa Feb 6 '12 at 12:33
1  
What do you mean by drawing a square? All I need is this lightning array, I can take it from there. –  MMM Feb 6 '12 at 12:49
1  
Ok I've created a prototype using your simple distance suggestion and my max() approach and it seems to work fine, I'm just wondering if there's a more efficient way of doing it. Here's a jsFiddle with the prototype. –  MMM Feb 6 '12 at 13:13
add comment

You could treat the light as a fluid that flows outwards from the light source and is blocked by certain blocks. You could then model this using cellular automata

share|improve this answer
    
Thanks but as I've said I'm not looking for a point of view solution, so I don't need to treat the light as fluid and block it using blocks, plus I can use libraries like libfov. –  MMM Feb 6 '12 at 12:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.