Page 1 of 1

[SOLVED] Love2d raytracer weird output?

Posted: Thu Jun 20, 2019 3:00 pm
by Joelrodiel
Hi! I've been working on a Raytracer on love2d. It is by no means meant to be a very optimized renderer, more like a learning project. But I cannot, for the love of everything holy, get it working. I've successfully made them in C++ using .bmp output, but when I ported it to love2d, the output is not what it should be! Im fairly new to this too so I'm stumped.

Here is the code for the intersection of the ray & the sphere (which I suspect is the culprit to my dilemma):

Code: Select all

-- Function to check if ray hits sphere
-- @param r - Ray object
-- @param s - Sphere object
-- @return 0 for no hit, 1 for hit
function rayHitSphere(r, s)

	oc = subVec3(r.origin, s.origin)
	
	b = 2 * dotVec3(oc, r.direction)
	c = dotVec3(oc, oc) - (s.radius*s.radius)
	
	disc = b*b - 4*c
	
	if disc < 1e-4 then
		return 0
	end
	
	return 1
end
And I've attached the full main.lua, and I suspect that if the intersection function isn't at fault, it might be something with the pixel rendering... Thanks in advance!

Re: Love2d raytracer weird output?

Posted: Mon Jun 24, 2019 12:00 am
by pgimeno
There's a typo in your dotVec3 function. The first term is v1.x*v2.y, should be v1.x*v2.x.

Re: Love2d raytracer weird output?

Posted: Mon Jun 24, 2019 3:36 pm
by Joelrodiel
pgimeno wrote: Mon Jun 24, 2019 12:00 am There's a typo in your dotVec3 function. The first term is v1.x*v2.y, should be v1.x*v2.x.
Wow... I've been going crazy because of a typo, thank you so much!