Add castRay function