Tuesday, December 27, 2011

Light map and line of sight

I implemented Bresenham's line algorithm and used it to calculate the light's path on the map, then added it to the light map. It needs a bits of smoothing to look good, light spilling over into unlighted tiles. But it works great.

The walls are not affected by the light yet, as they need to check the tile next to it and not it's own til to determine how much light it gets.

I might have to expand my light map to hold a light value for each corner of the tile. It should make it look less like blocks.

I found an easy description of Bresenham's line algorithm here wiki/Bresenhams_line_algorithm just scroll down to the simplification, My version looks like this:

    public static boolean isVisibleFrom(int startX, int startY, int endX, int endY, Map map){
        int dx = Math.abs(endX - startX);
        int dy = Math.abs(endY - startY);
        int sx;
        if (startX < endX)
            sx = 1;
            sx = -1;
        int sy;
        if (startY < endY)
            sy = 1;
            sy = -1;
        int err = dx-dy;
        int mapX = startX;
        int mapY = startY;
        int tempError;
            Tile tile = map.getTile(mapX, mapY);
            if (tile != null){
                if (tile.isBlocking()) return(false);
            if (mapX == endX && mapY == endY) break;
            tempError = 2*err;
            if (tempError > -dy){
                err = err - dy;
                mapX = mapX + sx;
            if (tempError <  dx){
                err = err + dx;
                mapY = mapY + sy;

I basically just runs through my map until i hit a tile that is blocking, then stop.

No comments:

Post a Comment