0
\$\begingroup\$

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()) {
            blocks.add(t);
        }
    };

    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.

\$\endgroup\$
  • \$\begingroup\$ 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), \$\endgroup\$ – Clockwork-Muse Jul 10 '14 at 11:26
4
\$\begingroup\$

If you use an algorithm like Bresenham, where the two lines can be different, depending on their start- and end-position, you then have to either:

  • Plot both lines and use the result of both plots for your LoS calculation.
  • Plot only one line (for example always from Player to Enemy) and use this one LoS calculation for both entities.
\$\endgroup\$
  • \$\begingroup\$ I thought of the second option, but the first one didn't occur to my mind. It works just fine, thanks! \$\endgroup\$ – user2499946 Jul 10 '14 at 12:20
  • \$\begingroup\$ +1 this is actually a really simple and effective approach that works when you have asymmetric lines. \$\endgroup\$ – ashes999 Jul 10 '14 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.