How we made the line of sight algorithm


The TIC-80 Fantasy Console is an awesome game making platform, but unfortunately we could not find a way to raycast in it, to check visibilty. We wanted to check whether the line of sight was obscured by map tiles or not, so we had to roll our own algorithm.

We came up with the following algorithm, written in pseudocode:

algorithm isClearLine(posFrom, posTo, mapData):
    // One of the exit conditions - if we're standing on it, we can definitely see it.
    if posFrom == posTo:
        return true
    eightClosest = getEightTilesAround(posFrom) // left out of here, not really related to the algorithm.
    closestTile = nil
    closestDist = +Infinity
    // Find the closest tile to the posTo position
    for tile in eightClosest:
        if distance(tile, posTo) < closestDist:
            closestDist = distance(tile, posTo)
            closestTile = til
    // Check if it's obsturcted
    if mapData[tile.x][tile.y].obstructed:
        return false
    else:
        // if the tile is not obstructed, repeat the check starting from it
        return isClearLine(tile, posTo, mapData)

Get FAKE'S-COM

Download NowName your own price

Leave a comment

Log in with itch.io to leave a comment.