fix raycasting algorithm