private bool LOSBetween2Nodes( int2 start , int2 end )
{
int2 dif = new int2(
math.abs( end.x - start.x ) ,
math.abs( end.y - start.y ) );
int2 pos = new int2(
start.x ,
start.y );
int2 inc = new int2(
( end.x > start.x ) ? 1 : -1 ,
( end.y > start.y ) ? 1 : -1 );
int numIterations = 1 + dif.x + dif.y;
int error = dif.x - dif.y;
int walkable = 0;
bool prevY = false;
int numSameDir = 0;
if ( error <= 0 )
prevY = true;
dif.x *= 2;
dif.y *= 2;
for ( ; numIterations > 0; --numIterations )
{
walkable += graphNodeWalkables[ pos.x + pos.y * graphCellLength ];
if ( numSameDir < 1 )
{
if ( prevY )
walkable += graphNodeWalkables[ ( pos.x - inc.x ) + pos.y * graphCellLength ];
else
walkable += graphNodeWalkables[ pos.x + ( pos.y - inc.y ) * graphCellLength ];
if ( error > 0 ) // more steps in x
{
pos.x += inc.x;
if ( !prevY )
numSameDir++;
else
numSameDir = 0;
prevY = false;
error -= dif.y;
}
else // more steps in y
{
pos.y += inc.y;
if ( prevY )
numSameDir++;
else
numSameDir = 0;
prevY = true;
error += dif.x;
}
}
}
return walkable == 0;
}
This currently checks all grid cells along a line between a certain width and returns telling you if any are unwalkable. This can run thousands-tens of thousands of times a frame.