itch.io is community of indie game creators and players

Devlogs

How we made the line of sight algorithm

FAKE'S-COM
A browser game made in HTML5

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)
Download FAKE'S-COM
Leave a comment