Raycasting algorithm image rendering problem C with MinilibX - Stack Overflow

admin2025-05-02  2

I am doing a small game with raycasting, the walls and the images now are rendered in the right place and size but the images are "spaghettified" vertically. I am using xml images 64x64 and this is the function:

void draw_line(t_player *player, t_game *game, float start_x, int i)
{
    float ray_dir_x = cos(start_x);
    float ray_dir_y = sin(start_x);
    float map_x = player->p_x;
    float map_y = player->p_y;
    double delta_dist_x = fabs(1 / ray_dir_x);
    double delta_dist_y = fabs(1 / ray_dir_y);
    double side_dist_x;
    double side_dist_y;

    int step_x;
    int step_y;
    if (ray_dir_x < 0) {
        step_x = -1;
        side_dist_x = (player->p_x - map_x) * delta_dist_x;
    } else {
        step_x = 1;
        side_dist_x = (map_x + 1.0 - player->p_x) * delta_dist_x;
    }
    if (ray_dir_y < 0) {
        step_y = -1;
        side_dist_y = (player->p_y - map_y) * delta_dist_y;
    } else {
        step_y = 1;
        side_dist_y = (map_y + 1.0 - player->p_y) * delta_dist_y;
    }

    int side;
    while (!touch(map_x, map_y, game)) {
        if (side_dist_x < side_dist_y) {
            side_dist_x += delta_dist_x;
            map_x += step_x;
            side = 0;
        } else {
            side_dist_y += delta_dist_y;
            map_y += step_y;
            side = 1;
        }
    }

    double wall_dist;
    double wall_x;
    if (side == 0)
        wall_dist = (map_x - player->p_x + (1 - step_x) / 2) / ray_dir_x;
    else
        wall_dist = (map_y - player->p_y + (1 - step_y) / 2) / ray_dir_y;

  //clear
   float line_height = (S_H / wall_dist * 64);  // Correct line height calculation

    // Adjust line height based on distance and projection plane
    //float proj_plane_dist = (float)S_W / (2 * tan(FOV / 2)); // Projection plane distance
    //float line_height = (proj_plane_dist / wall_dist); // Use consistent scaling
    int draw_start = -(int)line_height / 2 + S_H / 2;
    if (draw_start < 0)
        draw_start = 0;
    int draw_end = line_height / 2 + S_H / 2;
    if (draw_end >= S_H)
        draw_end = S_H - 1;

    if (side == 0)
        wall_x = (player->p_y) + wall_dist * ray_dir_y;
    else
        wall_x = (player->p_x) + wall_dist * ray_dir_x;
  //  wall_x -= floor(wall_x);  // Ensure it's a fractional part between 0 and 1

    //printf("wall_x: %f\n", wall_x);
    // Select the correct texture based on ray direction
    int *texture;
    if (side == 0) {
        if (ray_dir_x > 0)
            texture = game->texture_e.text_value;  // East
        else
            texture = game->texture_w.text_value;  // West
    } else {
        if (ray_dir_y > 0)
            texture = game->texture_s.text_value;  // South
        else
            texture = game->texture_n.text_value;  // North
    }

    printf("p_x: %f, p_y: %f, wall_x: %f\n", player->p_x,player->p_y, wall_x);


    int tex_x = (int)(wall_x * 64);  // Adjust texture X
    tex_x = tex_x % 64;  // Ensure tex_x is within bounds (0-63)
    // Draw the texture column
    for (int y = draw_start; y < draw_end; y++) {
        int tex_y = (int)((y - draw_start) * 64.0 / line_height) % 64;  // Adjust texture Y

        // Debug output
       // printf("tex_x: %d, tex_y: %d, wall_x: %f\n", tex_x, tex_y, wall_x);

        int color = texture[tex_x + tex_y * 64];  // Access the texture pixel
        put_pixel(i, y, color, game);  // Draw the pixel on the screen
    }
    //i++;
}

debug output:

p_x: 760.554504, p_y: 268.357147, wall_x: 280.284412
p_x: 760.554504, p_y: 268.357147, wall_x: 280.252147
p_x: 760.554504, p_y: 268.357147, wall_x: 280.219890
p_x: 760.554504, p_y: 268.357147, wall_x: 280.187640
p_x: 760.554504, p_y: 268.357147, wall_x: 280.155396
Window Closed

enter image description here

the put pixel function simply display the pixel columns one by one

this problem is fixed by removing the upscaling here:

 float line_height = (S_H / wall_dist * 64); to:
 float line_height = (S_H / wall_dist);

but then the images are rendered too far away from the player

转载请注明原文地址:http://www.anycun.com/QandA/1746117927a91916.html