Hello all,

I'm working on a top down 2D tile based game. I'm working on implementing lighting of a sort, but I'm only lighting entire tiles (either lit or not).

So, say I have a light source that should light all tiles within a radius of 1. That would end up lighting 5 tiles (it's own and those adjacent to it).

That's great and all, but the question arose to me, just how many tiles are going to be lit by using a specific light source (without iteration or implementing it).

Of course I started with just doing a typical calculation of circle surface area, but as an example, a radius of 1 would just result in Pi typically rather than 5.

Any suggestions on how I could implement a function that simply returns the number of tiles that would be lit when inputting a single number for radius? This can definitely ignore walls.

Thanks in advance,

Mythics

**0**

# Surface Area of Circle... sort of

Started by Mythics, Mar 09 2012 10:47 AM

5 replies to this topic

Sponsor:

###
#2
Crossbones+ - Reputation: **6991**

Posted 09 March 2012 - 11:18 AM

It becomes complicated by what distance method you're using. Without knowing this, it's impossible to even begin speculating on some function that does what you want.

To picture the complication I'm talking about, imagine doing a flood fill, starting at the where the light source is. If you want to do a flood fill, filling a region with a radius of 10, it all depends on how you calculate the radius. Do you do a 4-way flood fill, with each step costing 1? Do you do an 8-way flood fill, with each step costing 1? Do you do an 8-way flood fill with horizontal/vertical movements costing 1 and diagonal movements costing ~1.41 (sqrt(2))? Do you do a more "mathematically correct" flood fill and calculate each tile's distance to be the euclidian distance from the light source? The final result of how many tiles are lit all depends on what you mean by radius.

To picture the complication I'm talking about, imagine doing a flood fill, starting at the where the light source is. If you want to do a flood fill, filling a region with a radius of 10, it all depends on how you calculate the radius. Do you do a 4-way flood fill, with each step costing 1? Do you do an 8-way flood fill, with each step costing 1? Do you do an 8-way flood fill with horizontal/vertical movements costing 1 and diagonal movements costing ~1.41 (sqrt(2))? Do you do a more "mathematically correct" flood fill and calculate each tile's distance to be the euclidian distance from the light source? The final result of how many tiles are lit all depends on what you mean by radius.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

###
#3
Members - Reputation: **135**

Posted 09 March 2012 - 11:36 AM

It becomes complicated by what distance method you're using. Without knowing this, it's impossible to even begin speculating on some function that does what you want.

Ah, understood. Well, the function I typically use for distance is just Sqr((X2 - X1) ^ 2 + (Y2 - Y1) ^ 2) which I believe is euclidean distance.

The kind of answers I would expect would be like these, where the first number is the radius used and the second number is how many tiles would be considered to be lit.

1 5

2 13

3 29

4 49

5 81

6 113

7 149

8 197

9 253

10 317

I already have a pretty solid process for lighting them and all, but I'd greatly appreciate a method of calculating how many COULD be lit prior to running the lighting code.

###
#4
Crossbones+ - Reputation: **6991**

Posted 09 March 2012 - 12:22 PM

As far as I know, there isn't a simple mathematical formula you can use to obtain the precise answer. The only ways I know are a) approximating the result with the area of the circle, b) precomputing a table and looking it up in the table (which means you'd have an upper limit on your light's radius), or c) performing a special type of flood fill and counting the number of tiles in the circle.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]