From f963cd0aca3254b8ebb8af17881a470707ab91c3 Mon Sep 17 00:00:00 2001 From: Matthew Ryan Dillon Date: Sun, 29 Dec 2024 21:36:03 -0500 Subject: [PATCH] collision --- main.lua | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/main.lua b/main.lua index 4dee6ac..85786a8 100644 --- a/main.lua +++ b/main.lua @@ -8,12 +8,13 @@ function love.load() love.window.setMode(1024, 512) player = {} - player["x"] = 300 - player["y"] = 300 + player["x"] = 100 + player["y"] = 110 player["size"] = 8 - player["angle"] = 0 + player["angle"] = -math.pi / 2 player["dx"] = math.cos(player.angle) * 5 player["dy"] = math.sin(player.angle) * 5 + player["offset"] = 20 map = {} map["x"] = 8 @@ -45,13 +46,41 @@ function love.update() player.dx = math.cos(player.angle) * 5 player.dy = math.sin(player.angle) * 5 end + + local xo = 0 + if player.dx < 0 then xo = -player.offset else xo = player.offset end + local yo = 0 + if player.dy < 0 then yo = -player.offset else yo = player.offset end + + local ipx = math.ceil(player.x / map.size) + local ipx_add_xo = math.ceil((player.x + xo) / map.size) + local ipx_sub_xo = math.ceil((player.x - xo) / map.size) + local ipy = math.ceil(player.y / map.size) + local ipy_add_yo = math.ceil((player.y + yo) / map.size) + local ipy_sub_yo = math.ceil((player.y - yo) / map.size) + + local pos1 = (ipy - 1)*map.x + ipx_add_xo + local pos2 = (ipy_add_yo - 1)*map.x + ipx + if love.keyboard.isDown("w") then - player.x = player.x + player.dx - player.y = player.y + player.dy + if map.map[pos1] == 0 then + player.x = player.x + player.dx + end + if map.map[pos2] == 0 then + player.y = player.y + player.dy + end end + + local pos3 = (ipy - 1)*map.x + ipx_sub_xo + local pos4 = (ipy_sub_yo - 1)*map.x + ipx + if love.keyboard.isDown("s") then - player.x = player.x - player.dx - player.y = player.y - player.dy + if map.map[pos3] == 0 then + player.x = player.x - player.dx + end + if map.map[pos4] == 0 then + player.y = player.y - player.dy + end end end @@ -210,6 +239,7 @@ function drawRays3D() love.graphics.setColor(0.7, 0, 0) end + love.graphics.setLineWidth(1) love.graphics.line( player.x, player.y, ray.x, ray.y