Search code examples
javagame-developmentraycasting

Floorcasting not scrolling in raycasting engine?


I'm currently working on a raycaster in Java, and so far, I have the floor correctly textured. The problem, however, is that the floor doesn't scroll. In other words, when I move the camera in the projection, the floor stays the same, yet the walls move as expected. I'm really not sure what I'm doing wrong. I took almost all the code from this reference. Note that I took some liberties when pasting the code in that I used some pseudocode.

I tried applying a player offset to the tileX and tileY variables, e.g., tileX += player.x, and all I got was a floor that scrolls far too quickly and incorrectly.

for every ray:
   ... // other stuff relating to the walls above here.
   int start = (int)(wallY + wallHeight + 1);
   double directionCos = cos(rad(ray.getAngle()));
   double directionSin = sin(rad(ray.getAngle()));
   int textureDim = 16;
   for (int y = start; y < screenHeight; y++) {
       double distance = screenHeight / (2.f * y - screenHeight);
       distance /= cos(rad(player.getAngle()) - rad(ray.getAngle()));
       // The source I grabbed the code from actually appends the player's x and y to the tileX and tileY variables, but this completely messes up the textures when I try to.
       double tileX = distance * directionCos;
       double tileY = distance * directionSin;

       int textureX = Math.floorMod((int)(tileX * textureDim), textureDim);
       int textureY = Math.floorMod((int)(tileY * textureDim), textureDim);
       int rgb = floorTexture.getRGB(textureX, textureY);
       projectionFloor.setRGB((int)wallX, y, rgb);
  }

Below is an image of the floor. This is what the floor currently looks like

Below is an animation visualizing the problem. Animation of what's wrong

Below is an animation visualizing what happens if I try to apply a player position offset: enter image description here


Solution

  • Fixed it on my own. Turns out that, yes, you do have to account for the player's position (shocker!); the source I got the code from just didn't do it correctly.

    DTPP = distance to projection plane.

    for every pixel y from wallY + wallHeight + 1 to projectionHeight:
        double r = y - this.getPreferredSize().height / 2.f;
        double d = (CAMERA_HEIGHT * DTPP / r) / ANGLE;
        double tileX = CAMERA_X + d * RAY_COSANGLE;
        double tileY = CAMERA_Y + d * RAY_SINANGLE;
        int textureX = Math.floorMod((int) (tileX * TEXTURE_SIZE / 
        TEXTURE_SCALE), TEXTURE_SIZE);
        int textureY = Math.floorMod((int) (tileY * TEXTURE_SIZE / 
        TEXTURE_SCALE), TEXTURE_SIZE);
        ... (drawing occurs here)