So I'm a rather new hobbiest to programming and the main project I've been working on is a raycasting engine in C. However, I've been rather stumped when trying to implement a rendered floor. The best assesment I can give to whats going on is that the tiles being sampled are much further away than they should be, resulting in much of the rendered floor being indexed from places outside the map.
Some items for refrence, I've been following this tutorial and for the purpose of debugging I've held off on doing any textures but rather am rendering the floor tiles underneath the walls at the edge of the map (which under normal cercumstances should never be visible) in black, the rest of the floor tiles in green, and anything outside the map array in blue. Here's a sample of the code which I belive to be problematic:
void renderWallProjection(void) {
for (int x = 0; x < NUM_RAYS; x++) {
/*
Wall rendering code...
*/
// render floor
// ray angle relative to the player
float beta = abs(rays[x].rayAngle - player.rotationAngle);
int playerHeight = GAMEWINDOW_HEIGHT / 2;
for (int y = wallBottomY; y < WINDOW_HEIGHT; y++) {
// Distance from horizon line
float r = y - GAMEWINDOW_HEIGHT / 2;
// 2D distance to the floor point in world space (the bottom of the triangle, flat line distance)
float flatLnDist = (playerHeight / r) * DIST_PROJ_PLANE;
// the real "3D" distance from the player's head to the point of the floor
float truLnDist = flatLnDist / cos(beta);
// the real world cordinates of the floor
float flPointX = player.x + cos(rays[x].rayAngle) * truLnDist;
float flPointY = player.y + sin(rays[x].rayAngle) * truLnDist;
// the map tile which the points are located on
float flTileX = floor(flPointX / TILE_SIZE);
float flTileY = floor(flPointY / TILE_SIZE);
}
}
I hope that helps, let me know if theres any more information I should give. I've gone over the math mutliple times and to me, at leat, it seems correct.
So I discovered the main culpret and really it was quite simple, but was somthing that I completley overlooked. As was sugguested the problem did in face lie in the player height. While half the screen height is a way to represent the player height, that would be in screen space. FlatLnDist needed the player height in world space which is TILE_SIZE / 2. So now I've added the player's world space height to the player struct and the formula now looks like this:
flatLnDist = (player.height * DIST_PROJ_PLANE) / r;
I also added ceiling rendering like so:
ceilingY = (GAMEWINDOW_HEIGHT / 2) - r;
Because each vertical scanline, both in the floor and ceiling have the same pixel x cordinate and all the rest of the math for the ceiling is already done by the floor, I only have to modify the pixel y cordinate to esenitally mirror the floor projection over the horizon line. The result is so:[!Successful floor and ceiling projectionenter image description here](https://i.sstatic.net/3GYH8vnl.png)
Excellent. The only issue now I belive has to do with rounding and how the walls scale the closer and further away from them you are. In anycase I feel I've sufficently answered my original question and if I do get stumped on this new issue it seems it would be better to just create a new post.