# Line of Sight with Bresenham

I have a simple function to determine whether an entity can see another, in a top-down tile-based game (this is actually a tiny bit more complex in my game, to determine whether to see the first block, but I simplified it for this example):

public static boolean canSee(int x, int y, int x2, int y2, Area area) {
Array<Tile> blocks = new Array<>();
Utils.Bresenham.Plotter plotter = (x1, y1) -> {
Tile t = area.getTileAt(x1, y1);
if (t.blocksView()) {
}
};

Utils.Bresenham.line(x, y, x2, y2, plotter);

return blocks.size == 0;
}

// Example below:

P = player, E = enemy, B = block

.....
.....
.....
P.B..
B...E


Now the problem I have is that in this position, the enemy can see the player, but the player cannot see the enemy (because the most top 'B' blocks the Bresenham).. and I'm not sure what's a good way to fix this.

• For one thing, you could obviously early-exit if you found any brick that blocked a view. Otherwise, the critical parts of the code is missing - we don't know what criteria you're using to decide if a tile blocks views, or how you're deciding what tiles are on a line. You could certainly run the function twice (ie, sightlines should be mutual), – Clockwork-Muse Jul 10 '14 at 11:26