I have a misunderstanding of the raycasting formulas on flat screen because my rendering is not what I expected, this is some parts of the code to show my projection formulas that I wrote by myself
This is the code for the function intersection
int intersection(t_wolf3d *w, int i, t_vec2f *dir, int iter){
int mapx;
int mapy;
unsigned int length;
length = -1;
while (++length < w->cam->raylength)
{
mapx = (int)(w->cam->position.x + (length * dir->x));
mapy = (int)(w->cam->position.y + (length * dir->y));
if ((mapy / BLOC_SIZE < w->map->h && w->map->board[mapy / BLOC_SIZE]
[mapx / BLOC_SIZE]) || (i == 1 && mapy >= HEIGHT_MM))
{
w->cam->intersection.x = mapx;
w->cam->intersection.y = mapy;
return (1);
}
}
return (0);
}
Code used for the raycasting algorithm:
void raycasting(t_wolf3d *w, int limit){
int i;
int hit;
t_vec2f dir;
t_vec2f forward;
t_vec2f right;
double halfwidth;
double offset;
i = -1;
forward.x = tcos(w->cam->angle);
forward.y = tsin(w->cam->angle);
right.x = forward.y;
right.y = -forward.x;
halfwidth = ttan(w->cam->fov / 2.0);
while (++i < limit && !(hit = 0))
{
w->cam->rays[i].startpoint = w->cam->position;
w->cam->rays[i].startpoint.color = 0xff0000;
offset = ((i * 2.0 / (limit - 1.0)) - 1.0) * halfwidth;
dir.x = forward.x + offset * right.x;
dir.y = forward.y + offset * right.y;
if ((hit = intersection(w, limit, &dir)))
w->cam->rays[i].endpoint = w->cam->intersection;
else
{
w->cam->rays[i].endpoint.x = w->cam->position.x + w->cam->raylength
* dir.x;
w->cam->rays[i].endpoint.y = w->cam->position.y + w->cam->raylength
* dir.y;
}
limit == w->width ? render(w, i, hit) : 0;
}
}
For non-straight walls, I thought it was my variable types, it seems like not, I changed them from integers to double and i still got the same result, this is a look of my structures :
typedef struct s_point{
int x;
int y;
int color;} t_point;
typedef struct s_vec2f{
double x;
double y;} t_vec2f;
typedef struct s_camera{
double radius;
double angle;
double fov;
double raylength;
double speedmove;
double speedangle;
t_point position;
t_point intersection;
t_point rays[WIDTH];} t_camera;
typedef struct s_map{
int **board;
int w;
int h;} t_map;
typedef struct s_wolf3d{
t_map *map;
t_camera *cam;
int hit;
t_image *img;
void *mlx_ptr;
void *win_ptr;
char texture;
t_image *textures[3];
int colors[3];
int mini_h;
int mini_w;
int width;
int height;} t_wolf3d;