Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Scavenger on Tue 01/03/2016 16:23:18

Title: [C++/AGS] How to simulate light dropoff in a 2d array?
Post by: Scavenger on Tue 01/03/2016 16:23:18
I have a 2D array that deals with lighting in my game, and I'd like to be able to just create a "seed" light and have the dropoff automatically happen, so I can have dynamic lighting and such. Like so:


Seed lights:
0,0,0,0,0,0,0,0,0,0
0,4,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0
0,2,0,0,0,0,0,0,8,0
0,0,0,0,0,0,0,0,0,0

With dropoff:
1,2,1,0,0,0,0,0,1,0
2,4,2,1,0,0,0,1,2,1
1,2,1,0,0,0,1,2,4,2
1,2,1,0,0,1,2,4,8,4
0,1,0,0,0,0,1,2,4,2


But I'm not sure how to do it in a way that wouldn't slow down the game too much if it's calculated often. Does anyone have any advice about how I should do this? I need to do it once every second, at least, since there are objects that move around that give off light, and daylight.
Title: Re: [C++/AGS] How to simulate light dropoff in a 2d array?
Post by: Gurok on Wed 02/03/2016 04:08:42
Presumably, you're iterating through this array to render the lighting.

My immediate thought is that the light reaching a particular coordinate is just a function of the coordinate's distance from the source and the total brightness of the source. Instead of maintaining an array, I would keep a list of light sources and their coordinates, then calculate the total light hitting a given x/y coordinate. Let's say (x1,y1) is your light source and (x2,y2) is the coordinate you're testing.

For a circle, it's just sqrt((x2 - x1)^2 + (y2 - y1)^2); repeat for all sources in the list, summing the results. Total value is the total light hitting x2,y2.

Yours seem like they could be asymptotes, but I can't really tell. Off the top of my head, if you want something asymptotic, it's more like: ((c / |(x2 - x1)|) + (c / |(y2 - y1)|)) / 2; where || denotes absolute value and c is total brightness.

Does that help? I couldn't tell if your question was asking about formulas or array optimisation. If it's the latter, I don't think you need to store things in arrays, but I guess more info would help clarify.