0
\$\begingroup\$

I am making a raycaster, I Have currently got textures rendering, but with North and South faces it appears that the texture is scrolling infinitely. I have read Shimmering, scrolling/panning a texture but that appears to be a something different. Here is what it looks like here I have been following https://lodev.org/cgtutor/raycasting.html to make the raycaster.

DDA:

double cameraX = 2 * x / (double)w - 1;
            double rayDirX = camera.rotation.x + camera.plane.x * cameraX;
            double rayDirY = camera.rotation.y + camera.plane.y * cameraX;
            double deltaDistX = Math.abs(1 / (double) rayDirX) + 0.0000001;
            double deltaDistY = Math.abs(1 / (double) rayDirY) + 0.0000001;
            int mapX = (int)camera.position.x;
            int mapY = (int) camera.position.y;
            double sideDistX;
            double sideDistY;
            int stepX;
            int stepY;
            boolean hit = false;
            int side = 0;
            
            if(rayDirX < 0) {
                stepX = -1;
                sideDistX = (camera.position.x - mapX) * deltaDistX;
            } else {
                stepX = 1;
                sideDistX = (mapX + 1.0 - camera.position.x) * deltaDistX;
            }
            if(rayDirY > 0) {
                stepY = -1;
                sideDistY = (camera.position.y - mapY) * deltaDistY;
            } else {
                stepY = 1;
                sideDistY = (mapY + 1.0 - camera.position.y) * deltaDistY;
            }
            while(!hit) {
                if(sideDistX < sideDistY) {
                    sideDistX += deltaDistX;
                    mapX += stepX;
                    side = 0;
                } else {
                    sideDistY += deltaDistY;
                    mapY += stepY;
                    side = 1;
                }
                if(!(mapX >= 0 && mapX < width && mapY >= 0 && mapY < height)) {
                    break;
                }
                if(map[0][mapX][mapY] > 0) {
                    hit = true;
                }
            }

this following code is what I believe to be the issue:

double perpWallDist;
            if(side == 0)  perpWallDist = (sideDistX - deltaDistX);
            else           perpWallDist = (sideDistY - deltaDistY);
            int lineHeight = (int)(h / perpWallDist);
            int drawStart = -lineHeight / 2 + h / 2;
            if(drawStart < 0) drawStart = 0;
            int drawEnd = lineHeight /2 + h / 2;
            if(drawEnd >= h) drawEnd = h - 1;
            int textNum = map[0][mapX][mapY]-1;
            double wallX;
            if(side == 0)
                wallX = camera.position.y + perpWallDist * rayDirY;
            else 
                wallX = camera.position.x + perpWallDist * rayDirX;
            wallX -= Math.floor(wallX);
            Texture tex = textures.get(textNum);
            int texX = (int)(wallX * (double)tex.width);
            if(side == 0 && rayDirX > 0) texX = tex.width - texX - 1;
            if(side == 1 && rayDirY < 0) texX = tex.width - texX - 1;
            double step = 1.0 * tex.height / lineHeight;
            double texPos = (drawStart - h / 2 + lineHeight / 2) * step;
            for(int y = drawStart; y<drawEnd; y++)
              {
                // Cast the texture coordinate to integer, and mask with (texHeight - 1) in case of overflow
                int texY = (int)texPos & (tex.height - 1);
                texPos += step;
                int color = tex.getRGB(texX, texY);
                //make color darker for y-sides: R, G and B byte each divided through two with a "shift" and an "and"
                if(side == 1) color = (color >> 1) & 8355711;
                renderer.pixel[y * renderer.width + x] = color;
              }

Also, the "renderer" is just a class that holds an array of pixels (stored as RGB) that are later blitted onto the screen.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

I Found the issue. In the DDA there are these lines:

if(rayDirY > 0) {
                stepY = -1;
                sideDistY = (camera.position.y - mapY) * deltaDistY;
            } else {
                stepY = 1;
                sideDistY = (mapY + 1.0 - camera.position.y) * deltaDistY;
            }

it was this if statement: if(rayDirY > 0) needs to be changed to if(rayDirY < 0) . the problem was the greater than sign needed to be the less than sign.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .