Want more features on Pastebin? Sign Up, it's FREE!
Guest

Line of Sight

By: a guest on Feb 6th, 2013  |  syntax: C#  |  size: 1.02 KB  |  views: 53  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public bool LineOfSight(Vector2 origin, Vector2 destination)
  2.         {
  3.             float maxRange = 800.0f;
  4.             float distance = Vector2.Distance(origin,destination);
  5.             int precision = 25; // How many steps to check, must be smaller than pixel width/height of a tile
  6.                                 //
  7.            
  8.             if (distance > maxRange) // If target is out of range, do not check LOS
  9.                 return false;
  10.             else
  11.             {
  12.                 Vector2 stepDirection = destination - origin;
  13.                 stepDirection.Normalize();
  14.                 int iterations = (int)distance / precision;
  15.  
  16.                 for (int x = 1; x < iterations; x++)
  17.                 {
  18.                    
  19.                     if (TileMap.IsWallTileByPixel(origin + stepDirection * (x * 25)))
  20.                     {
  21.                         return false;
  22.                     }
  23.                 }
  24.                 return true; // If no wall tiles were found LOS is clear
  25.             }
  26.         }
clone this paste RAW Paste Data