Here is the texture (40x40)
Code: Select all
pi = math.pi
rad = math.rad
deg = math.deg
sin = math.sin
cos = math.cos
tan = math.tan
each = 1 / 40
function dist(x1, y1, x2, y2)
return math.sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2)
end
function love.load()
gw, gh = love.graphics.getWidth(), love.graphics.getHeight()
love.window.setMode(gw, gh)
gw, gh = love.graphics.getWidth(), love.graphics.getHeight()
love.graphics.setBackgroundColor(0.2, 0.2, 0.2)
mapw, maph = 7, 7
maps = 40
map = {
1,1,1,1,1,1,1,
1,0,0,0,0,0,1,
1,0,0,0,0,0,1,
1,0,0,0,0,0,1,
1,0,0,1,1,0,1,
1,0,0,0,0,0,1,
1,1,1,1,1,1,1
}
px, py, pa = 100, 100, rad(270)
wall1 = love.graphics.newImage("Wall1.png")
-- wall1:setWrap("clampzero")
end
function love.update(dt)
if love.keyboard.isDown("a") then
pa = pa - rad(100) * dt
if pa < 0 then
pa = pa + 2 * pi
end
end
if love.keyboard.isDown("d") then
pa = pa + rad(100) * dt
if pa > 2 * pi then
pa = pa - 2 * pi
end
end
if love.keyboard.isDown("w") then
px = px + cos(pa) * 60 * dt
py = py + sin(pa) * 60 * dt
end
if love.keyboard.isDown("s") then
px = px - cos(pa) * 60 * dt
py = py - sin(pa) * 60 * dt
end
end
function love.draw()
-- draw map
for i = 0, maph - 1 do
for s = 0, mapw - 1 do
if map[i * mapw + s + 1] ~= 0 then
love.graphics.rectangle("fill", s * maps, i * maps, maps, maps)
end
end
end
-- draw player
love.graphics.setColor(0, 0, 1)
love.graphics.circle("fill", px, py, 3)
love.graphics.setColor(1, 0, 0)
love.graphics.line(px, py, px + cos(pa) * 8, py + sin(pa) * 8)
love.graphics.setColor(1, 1, 1)
rayCast()
end
function rayCast()
local fov = 60
local ra, rx, ry
ra = pa - rad(fov / 2)
if ra < 0 then
ra = ra + 2 * pi
end
if ra > 2 * pi then
ra = ra - 2 * pi
end
for i = 0, fov - 1 do
local cDis, cRay = math.huge
local ya, xa
local run = true
local atan = -1 / tan(ra)
if ra > pi then
ry = math.floor(py / maps) * maps - 0.0001
ya = -maps
rx = (py - ry) * atan + px
xa = -ya * atan
end
if ra < pi then
ry = math.floor(py / maps) * maps + maps
ya = maps
rx = (py - ry) * atan + px
xa = -ya * atan
end
if ra == 0 or ra == pi then
rx, ry = px, py
run = false
end
while run do
local ix, iy = math.floor(rx / maps), math.floor(ry / maps)
if map[iy * mapw + ix + 1] ~= 0 then
local distance = dist(px, py, rx, ry)
if distance < cDis then
cDis = distance
cRay = {rx, ry, "hor"}
end
break
else
rx, ry = rx + xa, ry + ya
end
end
run = true
local ntan = -tan(ra)
if ra > 3 * pi / 2 or ra < pi / 2 then
rx = math.floor(px / maps) * maps + maps
xa = maps
ry = (px - rx) * ntan + py
ya = -xa * ntan
end
if ra < 3 * pi / 2 and ra > pi / 2 then
rx = math.floor(px / maps) * maps - 0.0001
xa = -maps
ry = (px - rx) * ntan + py
ya = -xa * ntan
end
if ra == pi / 2 or ra == 3 * pi / 2 then
rx, ry = px, py
run = false
end
while run do
local ix, iy = math.floor(rx / maps), math.floor(ry / maps)
if map[iy * mapw + ix + 1] ~= 0 then
local distance = dist(px, py, rx, ry)
if distance < cDis then
cDis = distance
cRay = {rx, ry, "ver"}
end
break
else
rx, ry = rx + xa, ry + ya
end
end
-- drawRay
love.graphics.setColor(1, 0, 0)
love.graphics.line(px, py, cRay[1], cRay[2])
love.graphics.setColor(1, 1, 1)
-- draw wall
cDis = cDis * cos(pa - ra)
local lineH = (maps * gh) / cDis
local center = gh / 2 - lineH / 2
--[[
love.graphics.setLineWidth(8)
love.graphics.line(i * 8 + 320, center, i * 8 + 320, lineH + center)
love.graphics.setLineWidth(1)
--]]
local offset
if cRay[3] == "ver" then
offset = cRay[2] % 40
end
if cRay[3] == "hor" then
offset = cRay[1] % 40
end
local mesh = love.graphics.newMesh({
{0, 0, offset * each, 0},
{8, 0, offset * each + each * 8, 0},
{8, lineH, offset * each + each * 8, 1},
{0, lineH, offset * each, 1}
}, "fan")
mesh:setTexture(wall1)
love.graphics.draw(mesh, i * 8 + 320, center)
ra = ra + rad(1)
if ra < 0 then
ra = ra + 2 * pi
end
if ra > 2 * pi then
ra = ra - 2 * pi
end
end
end