1

I'm in the process of making an RTS game and I've ran into a problem I could use help with. I want to create a fog of war system that reveals an area around a unit that belongs to you like how it's done in Starcraft 2. To do this, I'm using a 2 dimensional influence map that's stored in a 1 dimensional array to communicate with the FoW texture. Currently, every element in the array is a square tile and to find out whether or not it's revealed, I do a simple distance check but this generates a lot of performance demand on the CPU. Is there a clever way to check if a tile is revealed or not more efficiently? I've been thinking of using hexagons for the job, checking in 6 directions and interpolating the space in between but I'm not sure how to check in a single direction nor how to interpolate the space between.

Note: I'm using C# but pseudocode in any language works for me.

JPtheK9
  • 179
  • I think this might be a better question for stackexchange or one of the programming sister sites? You can access the list from the top of the screen on the left. – FundThmCalculus Mar 20 '15 at 00:29
  • Aren't you making the problem too mathy? Seems like an easy programming task. Is your game logic decoupled from your graphics? – Pedro Mar 20 '15 at 00:34
  • Why are you iterating over the tiles? Iterate over your units and draw what you need to draw for each unit. – Pedro Mar 20 '15 at 00:38
  • @FundThmCalculus I guess this question might be downvoted on stackoverflow – Pedro Mar 20 '15 at 00:40
  • for each unit in unitlist: unit->draw(); and make a subclass for drawing your units, then your system is even decoupled from the 3D engine you use, if you later want to switch your 3D engine you don't need to rewrite all your code. – Pedro Mar 20 '15 at 00:43
  • The more interesting question is how to securely render the fog of war. – Jonny Mar 20 '15 at 00:50
  • I'm using Unity3D for rendering and whatnot. Rendering the fog of war is just drawing a color to each pixel of a texture and applying that texture to a material. – JPtheK9 Mar 20 '15 at 03:35

2 Answers2

3

You could create a "delta list" that you apply to the current position to determine which square tiles are within the prescribed distance. For example:

OXXXO
XXXXX
XXOXX
XXXXX
OXXXO

There are twenty deltas: $\{(-1,2), (0,2), (1,2),(-2,1), (-1,1), (0,1), (1,1), (2,1), (-2,0),(-1,0),(1,0), (2,0), (-2,-1), (-1,-1), (0,-1), (1,-1), (2,-1), (-1,-2), (0,-2),(1,-2)\}$

Add each element in the list to the current $(x,y)$ position to give the twenty positions to reveal. You can even improve this somewhat by keeping track of movement -- if you move north then there is no need to re-reveal the southern part of the grid.

vadim123
  • 82,796
1

To give a brief suggestion. You know your location in $x$,$y$ coordinates. Compute your location in the 1D array. Now compute $(x+1,y)$ (your offset $+1$), $(x-1,y)$ (your offset $-1$), $(x,y-1)$ (your offset $-map_{width}$), and $(x,y+1)$ (your offset $+map_{width}$).

  • Thanks for this suggestion. I think this is what I'm going to do. – JPtheK9 Mar 20 '15 at 03:38
  • No problem. I think @vadim123 and I were thinking of nominally the same thing. He's recording the delta's, and I am recording the map between the 2D world grid and the 1D storage array. – FundThmCalculus Mar 21 '15 at 11:52