So I'm making an RTS on a 2d isometric map. There is a 'fog of war' where units have line of sight and can reveal tiles around them. Making them simply reveal tiles in a box around their location is pretty easy, but I wanted a more realistic circular vision that can be obstructed by objects in the way.
I've already coded this, and the basic code is here:
resetFog();
for(unsigned int i = 0; i<calcUnits->size(); i++)
{
unsigned int tx = calcUnits->at(i).tileX;
unsigned int ty = calcUnits->at(i).tileY;
tmpExplored[tx+(ty*size)] = true;
for(double vi = 0; vi<360; vi++)
{
double xDir = sin(vi);
double yDir = cos(vi);
for(unsigned int m = 0; m<calcUnits->at(i).type->sightDistance; m++)
{
double xPos = (xDir*m*15)+calcUnits->at(i).x;
double yPos = (yDir*m*15)+calcUnits->at(i).y;
point in(xPos,yPos);
point aq(calcMap->getTileFromPos(in).toArray());
if(aq.x > 0 && aq.x < size && aq.y > 0 && aq.y < size)
{
tmpExplored[(int)aq.x+(size*(int)aq.y)] = true;
if(calcMap->treeGrid[(int)aq.x+((int)aq.y*size)])
break;
}
else
break;
}
}
}
updateHalfExplored();
resetFog() resets the buffer and updateHalfExplored() basically flips the buffer to the main array. As you can see for each unit 360 raycasts are made and each one is checked up to 100 times along the way stopping if an obstacle is found. This means there's up to 36,000 checks per unit which is a ton of processing, and is also far more checks than there are tiles in any units line of sight.
Is there anyway I can do this more efficiently?
The end result can be seen here: