local function i(i,l,T,S)local D=l:Cross(S)local H=T.x-i.x local g=T.y-i.y local v=T.z-i.z local C=Det3x3(l.x,-S.x,D.x,l.y,-S.y,D.y,l.z,-S.z,D.z)if C==0 then return ZERO_VECTOR3 end local r=Det3x3(H,-S.x,D.x,g,-S.y,D.y,v,-S.z,D.z)/C local R=Det3x3(l.x,H,D.x,l.y,g,D.y,l.z,v,D.z)/C local c=i+r*l local V=T+R*S local n=c+.5*(V-c)if(V-c).Magnitude<.25 then return n else return ZERO_VECTOR3 end end

local function RayIntersection(p0, v0, p1, v1)
	local v2 = v0:Cross(v1)
	local d1 = p1.x - p0.x
	local d2 = p1.y - p0.y
	local d3 = p1.z - p0.z
	local denom = Det3x3(v0.x,-v1.x,v2.x,v0.y,-v1.y,v2.y,v0.z,-v1.z,v2.z)
	
	if (denom == 0) then
		return ZERO_VECTOR3 -- No solution (rays are parallel)
	end
	
	local t0 = Det3x3(d1,-v1.x,v2.x,d2,-v1.y,v2.y,d3,-v1.z,v2.z) / denom
	local t1 = Det3x3(v0.x,d1,v2.x,v0.y,d2,v2.y,v0.z,d3,v2.z) / denom
	local s0 = p0 + t0 * v0
	local s1 = p1 + t1 * v1
	local s = s0 + 0.5 * ( s1 - s0 )
	
	-- 0.25 studs is a threshold for deciding if the rays are
	-- close enough to be considered intersecting, found through testing 
	if (s1-s0).Magnitude < 0.25 then
		return s
	else
		return ZERO_VECTOR3
	end
end


