Created
August 27, 2023 18:41
-
-
Save Achie72/5147232732bd2b0c557091f5fe6949cc to your computer and use it in GitHub Desktop.
Lodev PICO-8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| player = { | |
| posX=1, | |
| posY=1, | |
| dirX = -1, | |
| dirY = 0, | |
| planeX=0, | |
| planeY=0.66 | |
| } | |
| function raycast() | |
| for i=0,128 do | |
| -- x coord in camera space | |
| local cameraX = 2 * i / 128 - 1 | |
| local rayDirX = player.dirX + player.planeX * cameraX | |
| local rayDirY = player.dirY + player.planeY * cameraX | |
| local mapX = player.posX | |
| local mapY = player.posY | |
| local sideDistX | |
| local sideDistY | |
| -- correction to avoud 0 div | |
| if rayDirX == 0 then rayDirX = 32000 end | |
| if rayDirY == 0 then rayDirX = 32000 end | |
| local deltaDistX = abs(1 / rayDirX) | |
| local deltaDistY = abs(1 / rayDirY) | |
| local stepX, stepY | |
| local perpWallDist | |
| local hit = 0 | |
| local side | |
| -- inital step and sidedist calc | |
| if (rayDirX < 0) then | |
| stepX = -1 | |
| sideDistX = (player.posX - mapX) * deltaDistX | |
| else | |
| stepX = 1 | |
| sideDistX = (mapX + 1 - player.posX) * deltaDistX | |
| end | |
| if (rayDirY < 0) then | |
| stepY = -1 | |
| sideDistY = (player.posY - mapY) * deltaDistY | |
| else | |
| stepY = 1 | |
| sideDistY = (mapY + 1 - player.posY) * deltaDistY | |
| end | |
| -- perform DDA | |
| while (hit == 0) do | |
| --printh(sideDistX) | |
| --printh(sideDistY) | |
| if sideDistX < sideDistY then | |
| -- step in x | |
| sideDistX += deltaDistX | |
| mapX += stepX | |
| side = 0 | |
| else | |
| sideDistY += deltaDistY | |
| mapY+=stepY | |
| side=1 | |
| end | |
| if wall_check_in_dda(mapX, mapY) then hit = 1 end | |
| end | |
| -- distance calculation from camera plane (this abvoids fisheye problem) | |
| if side == 0 then | |
| perpWallDist = sideDistX - deltaDistX | |
| else | |
| perpWallDist = sideDistY - deltaDistY | |
| end | |
| -- calc line height | |
| lineHeight = 128/perpWallDist | |
| -- calc lowesr and highest pixel to fill | |
| drawStart = -lineHeight/2 + 64 | |
| if drawStart < 0 then drawStart = 0 end | |
| drawEnd = lineHeight/2 + 64 | |
| if drawEnd >=127 then drawEnd = 127 end | |
| local colorData = { {5,1},{11,3},{12,13},{8,2},{10,9}} | |
| local mapTile = mget(mapX, mapY) | |
| printh(mapTile) | |
| local colorToUse = colorData[mapTile-63][side+1] | |
| line(i, drawStart, i, drawEnd, colorToUse) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment