Raycasting texture

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
lenlenlL6
Prole
Posts: 9
Joined: Tue Jul 04, 2023 3:54 pm

Raycasting texture

Post by lenlenlL6 »

I'm trying to add texture to my raycasting, but this is what I get, can anyone fix it :(((
Image
Here is the texture (40x40)
Image

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
User avatar
dusoft
Party member
Posts: 539
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Raycasting texture

Post by dusoft »

Maybe try:

Code: Select all

love.graphics.setDefaultFilter("nearest", "nearest");
lenlenlL6
Prole
Posts: 9
Joined: Tue Jul 04, 2023 3:54 pm

Re: Raycasting texture

Post by lenlenlL6 »

dusoft wrote: Sun Aug 27, 2023 10:03 am Maybe try:

Code: Select all

love.graphics.setDefaultFilter("nearest", "nearest");
It's still the same, it's just sharper
User avatar
darkfrei
Party member
Posts: 1186
Joined: Sat Feb 08, 2020 11:09 pm

Re: Raycasting texture

Post by darkfrei »

You are need to get just one pixel column, or scale the texture to show the texture just from covered delta angle.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
darkfrei
Party member
Posts: 1186
Joined: Sat Feb 08, 2020 11:09 pm

Re: Raycasting texture

Post by darkfrei »

I see the way to draw it with quads: just make 40 quads for image of 40 pixels wide and find the nearest quad to draw it with various distance.
Also the fish-eye problem can be solved with the factor 1/cos(phi) for horizontal angle.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests