Here is my raycaster function:
Code: Select all
function rays:rayCasting()
local rayAngle = Player.rotAngle - 30*math.pi/180
local width = love.graphics.getWidth() / 60
for i = 0, 60 do
--ray path incrementers
local segx1, segy1 = self.x1, self.y1
local rayCos, raySin = math.cos(rayAngle), math.sin(rayAngle)
local wall = 0
while (wall == 0) do
segx1 = segx1 + rayCos
segy1 = segy1 + raySin
wall = tileMap[math.floor((segy1 + 100)/100)][math.floor((segx1 + 100)/100)]
end
love.graphics.line(self.x1, self.y1, segx1, segy1)
local distance = math.sqrt((self.x1 - segx1) ^ 2 + (self.y2 - segy1) ^ 2)
--Angle trubles :/
local newAng = rayAngle - Player.rotAngle
if (newAng <= -2*math.pi) or (newAng >= 2*math.pi) then
newAng = 0
end
distance = distance * (math.cos(newAng))
--Shows rays being casted
love.graphics.line(self.x1, self.y1, distance*math.cos(newAng) + self.x1, distance*math.cos(newAng) + self.y1)
local wallHeight = math.floor(8000 / distance)
love.graphics.rectangle('line', i * width, love.graphics.getHeight() / 2 - wallHeight / 2, width, wallHeight)
rayAngle = rayAngle + math.pi/180
end
end
edit: I used Y2 instead of Y1, sorry it took me so long to figure out