Skip to content

Instantly share code, notes, and snippets.

@Mulperi
Last active April 17, 2023 08:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mulperi/269418ba0f77de93a8930878e3b9d0d5 to your computer and use it in GitHub Desktop.
Save Mulperi/269418ba0f77de93a8930878e3b9d0d5 to your computer and use it in GitHub Desktop.
PICO-8 raycast test
function _init()
tile = 8
gridlength = 8
player = {
x = 8,
y = 8
}
map = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, },
{ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, },
{ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, },
{ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }
}
rays = {}
end
getcellbypixelcoordinates = function(px, py, grid, tile)
local x = flr(px / tile)
local y = flr(py / tile)
if x > 0 and x <= #grid and y > 0 and y <= #grid then
return grid[x][y]
else
return nil
end
end
outofbounds = function(px, py)
if px < 0 or px > 128 + tile or
py < 0 or py > 128 + tile then
return true
end
return false
end
raycast = function(grid, tile, cellx, celly)
local points = {}
for angle = 0, 1, 0.05 do
local radius = 1
local targetx = cellx * tile
local targety = celly * tile
for i = 1, 128, 1 do
if not outofbounds(targetx, targety) and
getcellbypixelcoordinates(targetx, targety, grid, tile) == 0 then
radius = radius + 1
targetx = player.x * tile + cos(angle) * radius
targety = player.y * tile + sin(angle) * radius
end
end
add(points, { x = targetx, y = targety })
end
return points
end
function _update()
rays = raycast(map,
tile, player.x, player.y)
if btn(2) then
player.y = player.y - 0.1
end
if btn(3) then
player.y = player.y + 0.1
end
if btn(0) then
player.x = player.x - 0.1
end
if btn(1) then
player.x = player.x + 0.1
end
end
function _draw()
cls()
-- draw tiles.
for x = 1, #map do
for y = 1, #map do
if map[x][y] == 0 then
rect(
x * tile - tile,
y * tile - tile,
x * tile + tile - tile,
y * tile + tile - tile,
1
)
else
-- draw walls.
rectfill(
x * tile - tile,
y * tile - tile,
x * tile + tile - tile,
y * tile + tile - tile,
8
)
end
end
end
if rays then
for i = 1, #rays do
line(
player.x * tile - tile,
player.y * tile - tile,
rays[i].x - tile,
rays[i].y - tile,
12
)
circ(
rays[i].x - tile,
rays[i].y - tile,
3, 8)
end
end
-- player
circ(
player.x * tile - tile,
player.y * tile - tile,
3, 12)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment