Page 1 of 1

Ray reflection

Posted: Wed Jan 04, 2023 11:28 am
by darkfrei
Hi all!

Here is an example how to implement the ray reflection in lua.
collision-01.png
collision-01.png (12.1 KiB) Viewed 1613 times

Code: Select all

local function segmentVsSegment(x1,y1, x2,y2, x3,y3, x4,y4)
-- from https://2dengine.com/?p=intersections#Segment_vs_segment
	local dx1, dy1, dx2, dy2, dx3, dy3 = x2-x1, y2-y1, x4-x3, y4-y3, x1-x3, y1-y3
	local d = dx1*dy2 - dy1*dx2
	if d == 0 then return false end
	local t1 = (dx2*dy3 - dy2*dx3)/d
	if t1 < 0 or t1 > 1 then return false end
	local t2 = (dx1*dy3 - dy1*dx3)/d
	if t2 < 0 or t2 > 1 then return false end
	return x1 + t1*dx1, y1 + t1*dy1 -- point of intersection
end

local function reflectRay (x1,y1, x2,y2, x3,y3, x4,y4)
-- from https://stackoverflow.com/questions/30970103/2d-line-reflection-on-a-mirror
	-- x1,y1, x2,y2 is a mirror line
	-- x3,y3, x4,y4 is a ray
-- intersection:
	local x5, y5 = segmentVsSegment(x1, y1, x2, y2, x3, y3, x4, y4)
	if not x5 then return end -- no crossing

-- ray vector:
	local rayX, rayY = x4-x5, y4-y5

-- normal:
	local nx, ny = y2-y1, x1-x2
	local nlength = math.sqrt(nx*nx + ny*ny)
	if nlength == 0 then return end 
	nx, ny = nx/nlength, ny/nlength

-- dot product:
	local dotProduct = (rayX*nx)+(rayY*ny)
	local x6 = x4-2*dotProduct*nx
	local y6 = y4-2*dotProduct*ny
	return x3,y3, x5,y5, x6,y6 -- three points of reflected ray
end
2023-01-04.png
2023-01-04.png (3.43 KiB) Viewed 1602 times

Re: Ray reflection

Posted: Wed Jan 04, 2023 1:30 pm
by BrotSagtMist
Now include the refraction index and abbe number and we can start simulating lenses :D

Re: Ray reflection

Posted: Wed Jan 04, 2023 4:10 pm
by ivan
Good and thank you for providing credit to 2dengine.com
Please note that one of the conditional checks could be removed.
You first check for an intersection between two segments:

Code: Select all

	local x5, y5 = segmentVsSegment(x1, y1, x2, y2, x3, y3, x4, y4)
	if not x5 then return end -- no crossing
Then you check if one of the segment products is degenerate

Code: Select all

if nlength == 0 then return end
If the first condition is true (x5 is not nil) then nlength will always be > 0.