Jump to content
  • Advertisement
Sign in to follow this  
Mythics

Surface Area of Circle... sort of

This topic is 2470 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!