problem with my raycaster

I’m Creating a raycaster in SDL using my own little methods and i came across this distortion effect when i removed the fisheye, any idea how i can remove it without heavily altering my code too much?? here is the image followed by my Raycast3D() function:
enter image description here

const int MAXDIST(4000);
char prevwall{ '0' };
int prev_alpha(0);
SDL_Color WallColor{};
void Raycast3D()
    float Projection(FOV / 5);
    float FovInc(FOV / xRes);
    float rayAng(angle - (FOV / 2));
    float dist(0), line(0);
    int alpha(0);
    SDL_FPoint Ray{};
    for (int r = 0; r < xRes; ++r)
	    float rayDir = rayAng - angle;
	    Ray =;
	    for (int i = MAXDIST; i--;)
		    Ray.x += Cos(rayAng);
		    Ray.y += Sin(rayAng);
		    if (map[floor(Ray.y / cellsize)] [floor(Ray.x / cellsize)] != '0') break;		    
	        dist = SDL_GetDistanceF(, Ray);
	        line = (yRes * Projection / (dist * Cos(rayDir)));
	        alpha = 255 * (1 - dist / 900); if (alpha < 0) alpha = 0; if (alpha > 255) alpha = 255;
	        if (map[floor(Ray.y / cellsize)][floor(Ray.x / cellsize)] != prevwall)
		        switch (map[floor(Ray.y / cellsize)][floor(Ray.x / cellsize)])
		        case '1': WallColor = Red; break;
		        case '2': WallColor = Green; break;
		        case '3': WallColor = Blue; break;
		        case '4': WallColor = Yellow; break;
		        case '5': WallColor = Purple; break;
		        case '6': WallColor = Gray; break;
		        default: WallColor = SDL_GetRandomColor(); break;
		        prevwall = map[floor(Ray.y / cellsize)][floor(Ray.x / cellsize)];
	    if (alpha != prev_alpha) { DrawColor(WallColor, alpha); prev_alpha = alpha; }
	    SDL_RenderDrawLineF(REN, r, (yRes / 2) - line, r, (yRes / 2) + line);
	    rayAng += FovInc;

i forgot to mention that some functions in this code, like SDL_GetDistanceF() or SDL_GetRandomColor(), were created by me, but the results are how you’d expect expect

Been a long time since I messed with a raycaster, but almost looks like it’s overcorrecting?

You’ve got a lot of magic numbers in your code that might also be the source of the problem.

Maybe you could look at this raycaster that’s also using SDL.

I referred back to this series of articles a lot back when I was writing a raycaster. The raycaster it has you build sidesteps the fisheye problem entirely, by calculating the distance from the wall to that vertical slice’s spot on the camera plane instead of calculating the distance from the wall to the center of the player.


i’ve taken a look at every raycaster video/article/repository pretty much out there, i’m not exactly trying to take code from those, if i did my raycaster wouldnt have this issue, im turning my own spin on things to see if i can come up with any interesting algorithms or faster methods than what i can find, or simply just to understand how it works better by implementing it how i best understand and go from there. As for the magic numbers, the only reason i use magic numbers is because my code is already bulky with other functions related to other things in the game, like textboxes, player movement, etc., and i just end up using magic numbers for those quick one time things. my engine’s issue was fisheye, and i know the way to get rid of fisheye is to multiply the distance by cosine of ray direction, so i did the easy fix, which seems to get rid of the fisheye, but now the issue on the side of the window occurs, and its not just the left side, its also the right side, perhaps if you used my function to produce the same result, you might be able to help me out. Aside from the obvious way the function works, the map is just a vector of strings, SDL_GetDistanceF() returns a float equal to the euclidean distance of 2 SDL_FPoints, SDL_GetRandomColor() just returns an SDL_Color value of a random R, G, B, and A, and DrawColor() is just a short form of SDL_SetRenderDrawColor(), oh also Cos() just returns the cosine of the input parameter after converting it from degrees to radians. If you decide to 100% recreate the scene, the resolution is 1000x800 and the FOV is 110

I’m not suggesting turning the magic numbers into functions, but at least giving them descriptive names.

okay, but still, any idea how i fix the distortion on the edges??