Janiczek / elm-raycasting

Raycasting in 2D space

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

elm-raycasting

This package currently hosts a single function: Raycast2D.touchedTiles.

tileSize = 50
from = (25, 75) -- center of tile (0,1)
to = (275, 175) -- center of tile (5,3)

touchedTiles tileSize from to
--> Set.fromList [(0,1),(1,1),(1,2),(2,2),(3,2),(4,2),(4,3),(5,3)]

It is an Elm implementation of the DDA (Digital Differential Analysis) algorithm popularized by OneLoneCoder (Javidx9).

The main idea is that instead of walking the ray by small steps and checking which tile we're in, we instead go all the way to the nearest cell boundary. This uses less CPU and doesn't suffer from inaccuracy (overshooting the finishline, as it were).

Screenshot

You can play with a modified version of this code (such that we can visualize the line segments and seen tiles) in this Ellie.

About

Raycasting in 2D space

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:Elm 100.0%