# Fog of war

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

## Recommended Posts

Hi guys,

I don't know if this is the forum for this question, but I'm planning to make a RTS game which allows 2 players to fight each other or 1 player vs AI.

However, I need to find out the name and algorithm where a unit or building can only see or have vision of a limited range of the field/map(no good if each player knows exactly what each other is doing).

So my question is: Does anyone know the name or method of that algorithm?

Thanks

##### Share on other sites
Here is a really easy way to do it. Hopefully not too easy for what you're looking for. :)

1. Take the position of theunit/building that is looking around.. lets call this X,Y;

2. Define a viewing distance for your unit/building.. Lets call this RANGE.

3. Write a function called 'GetDistance(X, Y, X1, Y1)' that will tell you how far apart two things are.
GetDistance(int x1, int y1, int x2, int y2)
{
int dx = x1 - x2;
int dy = y1 - y2;

return Math.Sqrt( ( dx * dx) + (dy * dy));
}

4. Write a function called 'GetObjectsInRange(X, Y, RANGE)' that returns a list (an array) of the items that are in range. Basically this function will call CheckDistance against each object on your map. If the distance is less than RANGE, then do something (set some flag?) so your code knows it is visible.

You're done! This is REALLY simple of course.. and won't help you at all with line-of-sight, and it's not terribly effecient if you've got a lot of pieces to deal with.

Good luck with your game. :)

##### Share on other sites
Ok, so I bet that you want at least a realtime algorithm, or at least something that runs quickly.
http://www.appsizematters.com/
Has a good tutorial. The only thing I can add would be to use Run-Length Encoding to store the fog bits (if you have a decent-sized map).

##### Share on other sites
If you know how to render to and from textures with OpenGL or DirectX, you can store the fog of war as a one channel texture and draw white circles where your troops move. Use 4 linear blur filters in 8 directions like when making bloom to get a diffuse fog map to apply on the world as a planar lightmap. Use the world position in the vertex shader as input to the fog map's UV in the pixel shader. If you can read pixels from a copy of a fog map, you can use it for the AI.

##### Share on other sites
Quote:
 Original post by willh3. Write a function called 'GetDistance(X, Y, X1, Y1)' that will tell you how far apart two things are.GetDistance(int x1, int y1, int x2, int y2){ int dx = x1 - x2; int dy = y1 - y2; return Math.Sqrt( ( dx * dx) + (dy * dy));}

A good optimization is to square RANGE and change GetDistance to return distance squared (dx*dx + dy*dy). Range only needs to be squared once, avoiding multiple (expensive) Sqrt calls.

##### Share on other sites
Yes, and if it needs to be less prone to cheating, you should perform that check on an independent server that knows the positions of all map entities, then send the list to the client.

Joking aside, remember to store your stuff in a 2d tree so your algorithm can easily look up distances to things that are actually within a sensible range relative to the view distance.

##### Share on other sites
wtf bbq?? All of this stuff seems too complicated for me :(

Sorry to say this, but the game will be in 3d... Should of said that earlier.

##### Share on other sites
Quote:
 Original post by Lord_Dannywtf bbq?? All of this stuff seems too complicated for me :(Sorry to say this, but the game will be in 3d... Should of said that earlier.

Hmm. Will the game take place in plane or in space? The discussed fog of war will work even though you render the game in 3D. If you need actual volume fog of war, it gets trickier.

##### Share on other sites
What I'm trying to make is something like C&C Generals. Not something like Homeworld 2 if that helps and answers your question? :S

##### Share on other sites
Quote:
 Original post by Lord_DannyWhat I'm trying to make is something like C&C Generals. Not something like Homeworld 2 if that helps and answers your question? :S

-Then the game won't really be 3D in relation to fog of war. So no need to mention that.
Do you know how to compute the distance between two points in 2D?

• 48
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631378
• Total Posts
2999665
×

## Important Information

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!