Ray reflection

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Ray reflection

Post 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 2260 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 2249 times
Attachments
ray-reflection-01.love
CC0
(1.09 KiB) Downloaded 97 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
BrotSagtMist
Party member
Posts: 659
Joined: Fri Aug 06, 2021 10:30 pm

Re: Ray reflection

Post by BrotSagtMist »

Now include the refraction index and abbe number and we can start simulating lenses :D
obey
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Ray reflection

Post 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.
Post Reply

Who is online

Users browsing this forum: alexjgriffith and 2 guests