0

I´m trying to create a 2d map with a player (filled circle) and I´m starting to create a raycasting. Before doing the raycast I´m trying to draw a simple FOV (field of view) which should be an arc fillid with a solid red, like this:

enter image description here

(The image is just to give an idea of the filled solid red arc)

To calculate the arc for the drawing I get the first position of the arc with this:

rayAngle = rotationAngle - (FOV_ANGLE /2)

The rotationAngle is the start position of the player in radian:

90d - PI/2
270d = 3 * PI/2
180d = PI
360d = 2 * PI

The FOV_ANGLE is 60d in radian, which I did with this function:

60 * (PI /180)

And for the others rays I use these calc:

ray_angle += fov_angle / num_ray;

The num_ray is just the width of the windows, consider in this case that it is (11 * 32)

With this func. I calculate each ray and save in an array

t_point *rays

For the struct t_point I have this:

typedef struct s_point
{
    int x;
    int y;
    int color;
} t_point;

After that I get each index of the array and draw a line using an dda algorithm:

#define ROUND(a) ((int)(a + 0.5))

void ft_line_dda(t_data *img, t_point start, t_point end)
{
    int dx = end.x - start.x;
    int dy = end.y - start.y;
    int steps;
    int k;
    long double xincrement;
    long double yincrement;
    long double x = start.x;
    long double y = start.y;

    if (abs(dx) > abs(dy))
        steps = abs(dx);
    else
        steps = abs(dy);

    xincrement = abs(dx) / (long double)steps;
    yincrement = abs(dy) / (long double)steps;
    my_mlx_pixel_put(img, ROUND(x), ROUND(y), get_color(ft_create_point(ROUND(x), ROUND(y), 0), start, end));
    for (k = 0; k < steps; k++)
    {
        if (start.x <= end.x)
            x += xincrement;
        else
            x -= xincrement;
        if (start.y <= end.y)
            y += yincrement;
        else
            y -= yincrement;
        // my_mlx_pixel_put(img, ROUND(x), ROUND(y), start.color);
        my_mlx_pixel_put(img, ROUND(x), ROUND(y), get_color(ft_create_point(ROUND(x), ROUND(y), 0), start, end));
    }
}

But the problem is that I keep getting this image, with some dots and spaces:

enter image description here

(The size is ok. The problem are the empty space and dots)

Please find bellow the code related to the arc (pseudo-raycasting) I´m trying to do:

void ft_create_rays(t_vars *vars)
{
    long double fov_angle;
    long double ray_angle;
    long double num_ray;
    t_point *rays;
    int i;

    i = 0;
    fov_angle = ft_degtorad(FOV_ANGLE);
    num_ray = vars->win_width / WALL_STRIP_WIDTH;
    ray_angle = vars->player->rotation_angle - (fov_angle / 2);
    rays = malloc(num_ray * sizeof(t_point));
    while (i < num_ray)
    {
        rays[i] = ft_create_point(vars->player->x + cos(ray_angle) * 100,
                                  vars->player->y + sin(ray_angle) * 100, 0x00ff0000);
        ray_angle += fov_angle / num_ray;
        i++;
    }
    if (vars->rays != NULL)
        free(vars->rays);
    vars->rays = rays;
}

void ft_raycast_render(t_vars *vars, t_data *img)
{
    int i;
    t_point central;

    i = 0;
    central = ft_create_point(vars->player->x, vars->player->y, 0x00ff0000);
    while (i < (vars->win_width / WALL_STRIP_WIDTH))
    {
        ft_line_dda(img, central, vars->rays[i]);
        i++;
    }
}

void ft_raycast(t_vars *vars, t_data *img)
{
    ft_create_rays(vars);
    ft_raycast_render(vars, img);
}

I think I showed all the code that is need to help me debug this. Anyway I will let the link to the github repository:

https://github.com/wblech/cub3d

P.S. I think the problem might be that the end point is in the wrong place and the line go to a different position. But I don´t know if is this and if it is I don´t know how to fix.

1 Answers1

0

The spaces are there because your view really have holes...

You know if you cast n arrays on some FOV angle then you got only n points on the arc perimeter at all distances so the further you look the bigger the holes get. If you want to avoid this then limit your view distance ... and compute number of rays from perimeter arclength.

So lets make some example:


l=128 is view distance (or map size) in [pixels]
FOV=60*deg=1.0471975511965977461542144610932 rad is field of view in [rad]
n=? is min number of rays without holes in FOV

n >= FOV*l
n >= 134.04128655316451150773945101993
n = 135

If you want some inspiratiosn take alook at:

Spektre
  • 49,595
  • 11
  • 110
  • 380