how to not make my raycaster draw walls with curves

# define GRID_SIZE 64

// TODO: this comment
# define EPSILON 0.0001

// one degree in radians
# define ONE_DEGREE 0.0174532925

// Screen width
# define SCREEN_WIDTH 720

// Screen Height
# define SCREEN_HEIGHT 480

// Scale Factor for height of walls
# define SCALE_FACTOR 0.05

typedef struct s_ray_data
{
    int     mx;
    int     my;
    int     mp;
    int     dof;
    double  rx;
    double  ry;
    double  ra;
    double  xo;
    double  yo;
    double  a_tan;
    double  n_tan;
    double  hx;
    double  hy;
    double  dist_h;
    double  vx;
    double  vy;
    double  dist_v;
}           t_ray_data;


typedef struct s_ray_result
{
    double  dist;
    double  x;
    double  y;
    int object;
}           t_ray_result;

typedef struct s_ray
{
    int color;
    int direction;
    int object;
    double   distance;
}           t_ray;


double  mes_dist(double ax, double ay, double bx, double by)
{
    return (sqrt((bx - ax) * (bx - ax) + (by - ay) * (by - ay)));
}

void    calculate_horizontal_intercept(t_player_data *p, t_ray_data *rd)
{
    rd->dof = 0;
    rd->a_tan = -1 / tan(rd->ra);
    if (rd->ra > M_PI)
    {
        rd->ry = (((int)p->posy / GRID_SIZE) * GRID_SIZE) - 0.0001;
        rd->rx = (p->posy - rd->ry) * rd->a_tan + p->posx;
        rd->yo = -GRID_SIZE;
        rd->xo = -rd->yo * rd->a_tan;
    }
    if (rd->ra < M_PI)
    {
        rd->ry = (((int)p->posy / GRID_SIZE) * GRID_SIZE) + GRID_SIZE;
        rd->rx = (p->posy - rd->ry) * rd->a_tan + p->posx;
        rd->yo = GRID_SIZE;
        rd->xo = -rd->yo * rd->a_tan;
    }
    if (fabs(rd->ra) < EPSILON || fabs(rd->ra - M_PI) < EPSILON)
    {
        rd->rx = p->posx;
        rd->ry = p->posy;
        rd->dof = 8;
    }
}

void    calculate_vertical_intercept(t_player_data *p, t_ray_data *rd)
{
    rd->dof = 0;
    rd->n_tan = -tan(rd->ra);
    if (rd->ra > M_PI_2 && rd->ra < 3 * M_PI_2)
    {
        rd->rx = (((int)p->posx / GRID_SIZE) * GRID_SIZE) - 0.0001;
        rd->ry = (p->posx - rd->rx) * rd->n_tan + p->posy;
        rd->xo = -GRID_SIZE;
        rd->yo = -rd->xo * rd->n_tan;
    }
    if (rd->ra < M_PI_2 || rd->ra > 3 * M_PI_2)
    {
        rd->rx = (((int)p->posx / GRID_SIZE) * GRID_SIZE) + GRID_SIZE;
        rd->ry = (p->posx - rd->rx) * rd->n_tan + p->posy;
        rd->xo = GRID_SIZE;
        rd->yo = -rd->xo * rd->n_tan;
    }
    if (fabs(rd->ra - M_PI_2) < EPSILON || fabs(rd->ra - 3 * M_PI_2) < EPSILON)
    {
        rd->rx = p->posx;
        rd->ry = p->posy;
        rd->dof = 8;
    }
}

static void calculate_ray_data(t_player_data *p, t_ray_data *rd, t_minimap *m,
        t_ray_result *res)
{
    res->dist = 0.;
    res->x = p->posx;
    res->y = p->posy;
    while (rd->dof < 8)
    {
        rd->mx = (int)(rd->rx) / GRID_SIZE;
        rd->my = (int)(rd->ry) / GRID_SIZE;
        rd->mp = rd->my * m->map_x + rd->mx;
        if (rd->mp > 0 && rd->mx >= 0 && rd->mx < m->map_x && rd->my >= 0
            && rd->my < m->map_y && object_finder(m, rd, res))
        {
            res->x = rd->rx;
            res->y = rd->ry;
            res->dist = mes_dist(p->posx, p->posy, res->x, res->y);
            rd->dof = 8;
        }
        else
        {
            rd->rx += rd->xo;
            rd->ry += rd->yo;
            rd->dof++;
        }
    }
    if (res->dist == 0.)
        res->dist = INFINITY;
}

void    draw_rays(t_player_data *player, t_minimap *map, double angle, t_ray *rs)
{
    t_ray_data      ray_data;
    t_ray_result    result_h;
    t_ray_result    result_v;
    double          ca;

    ray_data.ra = angle;
    ca = player->playerangle - ray_data.ra;
    if (ca < 0)
        ca += 2 * M_PI;
    if (ca > 2 * M_PI)
        ca -= 2 * M_PI;
    calculate_horizontal_intercept(player, &ray_data);
    calculate_ray_data(player, &ray_data, map, &result_h);
    calculate_vertical_intercept(player, &ray_data);
    calculate_ray_data(player, &ray_data, map, &result_v);
    if (result_h.dist < result_v.dist)
    {
        ray_data.rx = result_h.x;
        ray_data.ry = result_h.y;
        rs->object = result_h.object;
        rs->color = 0xFF0000;
        direction_setter(rs, true, &ray_data, player);
    }
    else
    {
        ray_data.rx = result_v.x;
        ray_data.ry = result_v.y;
        rs->object = result_v.object;
        rs->color = 0x800000;
        direction_setter(rs, false, &ray_data, player);
    }
    rs->distance = mes_dist(player->posx, player->posy, ray_data.rx,
            ray_data.ry);
    rs->distance = rs->distance * cos(ca);
}

void    draw_ceiling(int x, int segment_width, int y_start, t_mlx *mlx)
{
    int y;
    int x_current;

    y = 0;
    while (y < y_start)
    {
        x_current = x;
        while (x_current < x + segment_width)
        {
            my_mlx_pixel_put(mlx, x_current, y, rgb_to_hex(mlx->scene.c_clr));
            x_current++;
        }
        y++;
    }
}

void    draw_walls(int x, int y_start, int y_end, t_mlx *mlx)
{
    int y;
    int x_current;
    int segment_width;

    segment_width = SCREEN_WIDTH / mlx->map->player->fov;
    x_current = x;
    while (x_current < x + segment_width)
    {
        y = y_start;
        while (y < y_end)
        {
            my_mlx_pixel_put(mlx, x_current, y, mlx->wall_color);
            y++;
        }
        x_current++;
    }
}

void    draw_floor(int x, int segment_width, int y_end, t_mlx *mlx)
{
    int y;
    int x_current;

    y = y_end;
    while (y < SCREEN_HEIGHT)
    {
        x_current = x;
        while (x_current < x + segment_width)
        {
            my_mlx_pixel_put(mlx, x_current, y, rgb_to_hex(mlx->scene.f_clr));
            x_current++;
        }
        y++;
    }
}


void    draw_wall_segment_to_image(int x, float distance, t_mlx *mlx, int color)
{
    int wall_height;
    int y_start;
    int y_end;
    int segment_width;

    segment_width = SCREEN_WIDTH / mlx->map->player->fov;
    wall_height = (int)(SCREEN_HEIGHT / (distance * SCALE_FACTOR));
    y_start = (SCREEN_HEIGHT - wall_height) / 2;
    y_end = y_start + wall_height;
    mlx->wall_color = color;
    draw_ceiling(x, segment_width, y_start, mlx);
    draw_walls(x, y_start, y_end, mlx);
    draw_floor(x, segment_width, y_end, mlx);
}


void    walls_3d(t_minimap *map, t_player_data *player, t_mlx *mlx)
{
    double  angle;
    int     i;
    int     segment_width;
    t_ray   ray_result;

    segment_width = SCREEN_WIDTH / player->fov;
    i = 0;
    angle = player->playerangle - ONE_DEGREE * (player->fov / 2);
    while (i <= player->fov)
    {
        if (angle < 0)
            angle += 2 * M_PI;
        if (angle > 2 * M_PI)
            angle -= 2 * M_PI;
        draw_rays(player, map, angle, &ray_result);
        draw_wall_segment_to_image(i * segment_width, ray_result.distance, mlx,
            ray_result.color);
        angle += ONE_DEGREE;
        i++;
    }
}


I still have some distortian when I am close to a wall that it curves towards me. If I stand next to a long wall it seems like it curves towards me at close distances and further away is curves a bit out. This only happens if I stand under an angle next to it. When I look straight towards it then its fine.picture of the scenario

I tried distance = distance * cos(ca) with ca being the player angle – ray angle. I also tried to work a little bit with a camera plane but couldn’t get this working. Also lowering the FOV seems to help a little but I want people to have a fov slider in the settings.

New contributor

Quinten Raymaekers is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật