From 9f19df48db5b1f4b9db9e4431a855c105a7daff1 Mon Sep 17 00:00:00 2001 From: kisekinopureya Date: Wed, 23 Jul 2025 17:33:23 +0300 Subject: [PATCH] feat(main): floor and ceiling cast --- main.go | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 3dbc808..0e3434d 100644 --- a/main.go +++ b/main.go @@ -110,8 +110,48 @@ func main() { } } - for i := range pixels { - pixels[i] = 0x00000000 // Set background to black + // Floorcasting logic + for y := 0; y < screenHeight; y += screenScale { + rayDirX0 := dirX - planeX + rayDirY0 := dirY - planeY + rayDirX1 := dirX + planeX + rayDirY1 := dirY + planeY + + p := y - screenHeight/2 + if p == 0 { + continue // avoid division by zero + } + + posZ := 0.5 * float64(screenHeight) + rowDistance := posZ / float64(p) + + floorStepX := rowDistance * (rayDirX1 - rayDirX0) / float64(screenWidth) + floorStepY := rowDistance * (rayDirY1 - rayDirY0) / float64(screenWidth) + + floorX := posX + rowDistance*rayDirX0 + floorY := posY + rowDistance*rayDirY0 + + for x := 0; x < screenWidth; x++ { + cellX := int(floorX) + cellY := int(floorY) + + tx := int(float64(texWidth)*(floorX-float64(cellX))) & (texWidth - 1) + ty := int(float64(texHeight)*(floorY-float64(cellY))) & (texHeight - 1) + + floorX += floorStepX + floorY += floorStepY + + floorTexture := 3 + ceilingTexture := 6 + + var color uint32 = textures[floorTexture][texWidth*ty+tx] + color = (color >> 1) & 0x7F7F7F // darken + pixels[y*screenWidth+x] = color + + color = textures[ceilingTexture][texWidth*ty+tx] + color = (color >> 1) & 0x7F7F7F + pixels[(screenHeight-y-1)*screenWidth+x] = color + } } // Raycasting logic