0
\$\begingroup\$

I am trying to reproduce the raycasting of the game Wolf3D. I read this tutorial, but I am struggling to understand how to code the intersection between the rays and the walls by using angles and pixels.

I have coded something but my approach to the concept seems quite wrong:

void    ray_cast(t_wolf3d *w)
{
    int         wall_hit; // tell us if a ray hits a wall so we can stop the algorithm
    t_ray       ray;    // add the ray if we don't hit a wall
    double      angle_s; // will be a part of the FOV (subsequent column)
    int         rayAdvance = 0;

    int         x; // for each pixels ont the screen

    angle_s = 0.0;
    x = -1;
    while (++x < W_M) //W_M represents the width of the map
    {
        while (angle_s < FOV / 2) // cast rays
        {
            rayAdvance = 0;
            while (++rayAdvance <= (277 / cos(angle_s * M_PI / 180.0))) //comparing distance for every pixels in the ray to the ray distance
            {
                ray.x = w->posx + rayAdvance * cos(angle_s * M_PI / 180.0); // converting degrees to radians inside cosine and sine
                ray.y = w->posy + rayAdvance * sin(angle_s * M_PI / 180.0);
                if (ray.x < w->map->h && ray.y < w->map->h && w->map->board[(int)ray.y][(int)ray.x] != 0)
                {
                    printf("ray.x = %f, ray.y = %f\n, rayAdvance = %d\n", ray.x, ray.y, rayAdvance);
                    wall_hit = 1;
                    break;
                }
            }
            if (wall_hit == 1)
                draw_wall(w, rayAdvance, x);
            angle_s += 30.0 / W_M; // i devided the field of view so i can do the recursion for the other part of FOV
        }
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ What is it about your current approach that "seems quite wrong"? Precisely describing or showing the symptoms of the problem and how they differ from what you expect can help us narrow down the root cause and find solutions faster. \$\endgroup\$ – DMGregory Mar 24 '19 at 18:00
  • \$\begingroup\$ for me the ray is just a pixels suite, so if i know the ray length, i can check for its all pixels if there is a wall or no, but i can't know exactly where the intersection occurs neither when to find it inside my code although i am good at maths, i can't just implement it that's why i need some tips. \$\endgroup\$ – Num Lock Mar 24 '19 at 18:42
  • \$\begingroup\$ Need some explanations on how to check where my intersections occur \$\endgroup\$ – Num Lock Mar 24 '19 at 21:02
0
\$\begingroup\$

I actually got it by myself, first of all, i separated the problems into functions where i started by drawing a minimap which helps me to display the intersections between rays and walls, hence it was simple to do the calculations. My problem was the muddle between the map which is made of (0,1..., depending of the walls) and the 2D maps which is the one that i got from projecting the first map.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.