1
\$\begingroup\$

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 (it’s not easy to tell, but the scrolling is much faster than it should be and scrolls in the wrong direction most of the time): enter image description here

\$\endgroup\$

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.