Advertisement

Fog of war

Started by October 14, 2010 12:03 PM
8 comments, last by SuperVGA 14 years ago
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
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. :)
Advertisement
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).
ROFLMAO-GG-HF-GL-LOL-TTYL-BRB-GTG
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.
Quote: Original post by willh
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));
}

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.

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.
Advertisement
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.
Quote: Original post by Lord_Danny
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.


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.
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
Quote: Original post by Lord_Danny
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


-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?

This topic is closed to new replies.

Advertisement