Skip to content

Instantly share code, notes, and snippets.

@Mulperi
Created April 13, 2023 08:35
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/74dc9c1d08aaf11838e7e5454a61203f to your computer and use it in GitHub Desktop.
Save Mulperi/74dc9c1d08aaf11838e7e5454a61203f to your computer and use it in GitHub Desktop.
Love2d raycasting test
-- 1. make zip and rename it to .love
-- 2. copy /b love.exe+mygame.love mygame.exe
-- or cmd /c copy /b love.exe+mygame.love mygame.exe
-- D:\repos\love-11.4-win64/lovec.exe .
function love.load()
TileSize = 32
GridLength = 16
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
function love.keypressed(key)
if key == "up" then
Player.y = Player.y - 1
end
if key == "down" then
Player.y = Player.y + 1
end
if key == "left" then
Player.x = Player.x - 1
end
if key == "right" then
Player.x = Player.x + 1
end
Rays = RayCast(Map, TileSize, Player.x, Player.y)
end
GetCellByPixelCoordinates = function(px, py, grid, tileSize)
local x = math.floor(px / tileSize)
local y = math.floor(py / tileSize)
if x > 0 and x <= #grid and y > 0 and y <= #grid then
return grid[x][y]
else
return nil
end
end
PixelOutOfBounds = function(px, py)
if px < 0 or px > love.graphics.getWidth() or
py < 0 or py > love.graphics.getHeight() then
return true
end
return false
end
DegreesToRadians = function(degrees)
return degrees * math.pi / 180
end
RadiansToDegrees = function(radians)
return radians * 180 / math.pi
end
RayCast = function(grid, tileSize, cellX, cellY)
local points = {}
for angle = 0, 360, 1 do
local radius = tileSize
local targetX = cellX * tileSize
local targetY = cellY * tileSize
for i = 1, love.graphics.getWidth(), 1 do
if not PixelOutOfBounds(targetX, targetY) and
GetCellByPixelCoordinates(targetX, targetY, grid, tileSize) == 0 then
radius = radius + 1
targetX = Player.x * tileSize + math.cos(DegreesToRadians(angle)) * radius
targetY = Player.y * tileSize + math.sin(DegreesToRadians(angle)) * radius
end
end
table.insert(points, { x = targetX, y = targetY })
end
return points
end
function love.update(dt)
love.timer.sleep(1 / 10 - dt)
end
function love.draw(dt)
-- draw tiles
for x = 1, #Map, 1 do
for y = 1, #Map, 1 do
if Map[x][y] == 0 then
love.graphics.setColor(0.5, 0.5, 0.5, 1)
love.graphics.rectangle("line",
x * TileSize,
y * TileSize,
TileSize,
TileSize)
else
love.graphics.setColor(0.75, 0.75, 0.75, 1)
love.graphics.rectangle("fill",
x * TileSize,
y * TileSize,
TileSize,
TileSize)
end
end
end
if Rays then
print(#Rays)
for i = 1, #Rays, 1 do
love.graphics.setColor(0, 0, 1, 1)
love.graphics.line(
Player.x * TileSize,
Player.y * TileSize,
Rays[i].x,
Rays[i].y)
love.graphics.setColor(1, 0, 0, 1)
love.graphics.circle("line",
Rays[i].x, Rays[i].y,
4)
end
end
-- player
love.graphics.setColor(0, 1, 0, 1)
love.graphics.rectangle("fill",
Player.x * TileSize, Player.y * TileSize,
16,
16)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment