3
\$\begingroup\$

I'm currently working on a ray-casting engine in Java and I can't figure out what's wrong with my code.

For the learning and theory I use:

The permadi tutorial: ( https://permadi.com/1996/05/ray-casting-tutorial-12/ ) When I started having problems with floor-casting I found this: ( https://www.allegro.cc/forums/thread/374305 )

I was happy becaues my implementation was 90% the same as the second code snippet on the linked Allegro.cc page. I added the x and y calculation to my code and I tried, but it didn't work. I tried exactly the same solution from the page but it didn't work.

The interesting fact that my code seems to "work" sometimes and sometimes not.

Furthermore, if I start walking then the floor moves to the wrong direction, sometimes backward for example.

In the beginning of implementing the engine I used the example code from the permadi tutorial too to correct my misunderstandings and so and it worked out well so far.

In this implementation 60° = planewidth. Every other degree is based upon this. Even the toRad function uses ANGLE180 (planeWidth*3) for the calculation.

I think I may not understand something in the math or I don't know, I just couldn't find where the error is.

I looked for the solution on other sites as well but it didn't help.

(Sorry if my english was bad.)

int x,y;
double floorPointDistance;
for(int i = floorCastingStartPixel; i < planeHeight; i++) {
floorPointDistance = ( ( PLAYERHEIGHT * playerPaneDist ) / ( (planeHeight>>1) - i ) ) / (Math.sin(((double)toRad(angle+rayAngle))));

// almost good
x = (int) Math.abs( (-floorPointDistance * sinTable[rayAngle]) + (playerX) ) % 64;
y = (int) Math.abs( (floorPointDistance * cosTable[rayAngle]) + (playerY) ) % 64;

screenBuffer.setRGB(raysCasted, i, Texture.floor.texture.getRGB(x,y));
}

Something very strange is happenning here:

enter image description here

But now it seems to be fine...mostly:

enter image description here

UPDATE

Success!!!!

I just "ruin" the equations a little bit and it's got better. I corrected the wrong floor-move direction by removing the "-" from the x equation. And I noticed the floor was very rotated, rotated into each other (you can see that "anomaly" in the previous pictures), I removed the cos part from the distance formula and it's got better.

int floorCastingStartPixel = planeHeight>>1;
int x,y;
for (int i=floorCastingStartPixel; i<=planeHeight-1; i++) {
distance = (float) (((float)PLAYERHEIGHT / (i-(planeHeight>>1)) )* playerPaneDist );

x = ( (int) (distance * (cosTable[rayAngle])) ) + playerX;
y = ( (int) (distance * (sinTable[rayAngle])) ) + playerY;

screenBuffer.setRGB(raysCasted, i, addFogEffect(Texture.floor.texture.getRGB(x&63,y&63),distance));
}

The current state: enter image description here

I did the ceil-casting by "reversing" the for loop (loop variable = 0 and incrementing till it's planeHeight>>1).

UPDATE #2

I just realized that my ceiling don't move correctly...not always.

UPDATE #3

Tried other texture, now seems to move ok, so it could have been just seom optical effect.

And...I noticed thath the floor and ceiling has a little hyperbolic(?) curve. And I just realized that it's like the fisheye-effect.

You have to do fish-eye correction on the floor and ceil distance too!!!!

\$\endgroup\$
  • \$\begingroup\$ I also have a problem with drawing a floor / ceiling. At the moment, using your formula in the last comments I have that: 1) floor rotates correctly relative to the player, 2) the floor does not move correctly. He follows the player and creates the effect that the floor goes under the walls and moves on. And in the correct version player must move on the floor. Сould you show the final code that you used for the calculation of collisions with the floor? Thank you. \$\endgroup\$ – Alex Jun 20 '18 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.