local function V(V,q,d,g)local P=q:Cross(g)local M=d.x-V.x local b=d.y-V.y local l=d.z-V.z local t=Det3x3(q.x,-g.x,P.x,q.y,-g.y,P.y,q.z,-g.z,P.z)if t==0 then return ZERO_VECTOR3 end local N=Det3x3(M,-g.x,P.x,b,-g.y,P.y,l,-g.z,P.z)/t local k=Det3x3(q.x,M,P.x,q.y,b,P.y,q.z,l,P.z)/t local Z=V+N*q local E=d+k*g local u=Z+.5*(E-Z)if(E-Z).Magnitude<.25 then return u 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


