[Solved] Raycasting Issue

I have a raytracing engine (or somewhat a raytracer) and raycast for “every” pixel. It should show the color of the instance that was hit. However, when the raycast doesn’t hit anything, it should show black.

If you look at the video, you can see the black part (sky) is inverted and should be on the top. But when I look at the spawn location, it shows up normal.

When I tried flipping the canvas upside down it showed the spawn upside down. So I don’t know what the problem is. Here’s my script by the way:

Script
function raytracer:cast(_start:Vector2, sep:number,res:number)
	self.RAYS = _{}
	sep = sep or 10
	local cam:Camera = self.cam
	local VS = cam.ViewportSize
	local width, height = VS.X, VS.Y
	
	for y = 1, res do
		for x = 1, res do
			local posX, posY = x*sep + _start.X - sep/2, y*sep + _start.Y - sep/2
			local RayRes = self:getRay(posX, posY)
			self.RAYS.append(RayRes)
		end
	end
	return self.RAYS
end

function raytracer:getColor(RayRes)
	--return BrickColor.random().Color
	if RayRes then
		return RayRes.Instance.Color
	end
	return Color3.new(0,0,0)
end

function raytracer:draw(res:number)	
	local i = 1
	for y = 1, res do
		for x = 1,res do
			self.canvas:DrawPixel(Vector2.new(x, y), self:getColor(self.RAYS[i]))
			i += 1
		end
		game["Run Service"].RenderStepped:Wait()
	end
end

(I’m using my QuickList module for the self.RAYS list that’s why it says “append()” which is adding to the end of the list.)

I somehow fixed it, I think my list didn’t like when I was adding “nil” to it. Or something like that.

I just replaced the append(nil) to append(0) when the RayRes was nil.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.