0
\$\begingroup\$

I want to check the horizontal and vertical intersections on my raycasting view, and I can't exactly understand what is ALPHA using this drawing enter image description here

source : https://www.permadi.com/tutorial/raycast/rayc7.html

I wrote something that makes the walls straights and with less stairs on top and bottom, but i have some columns which go out of the view row, i can't figure out where i failed my operations (rounds)

this is the code of the vertical and the horizontal intersections :

static inline void      inter_hor(t_wolf3d *w, int i, double angle){
t_vec2f a; // first intersection point
t_vec2f o; // offset
double  tangent;

tangent = ttan(angle) + 0.001;
a.y = (angle <= 180) ? floor(w->cam->position.y / 32) *
32 - 1 : ceil(w->cam->position.y / 32) * 32 + 32;
o.y = (angle < 180) ? -32 : 32;
o.x = (angle < 90 || angle > 270) ? 32 / tangent : -32 / tangent;
a.x = (angle != 180.0 && angle < 359.97) ? w->cam->position.x + (w->cam->position.y - a.y) / tangent :
w->cam->position.x + (w->cam->position.y - a.y) / -tangent; //w->mini_h is the w->map->h  * 32 and so on for w->mini_w
a.x = (angle >= 179.97 && angle <= 180) ? w->cam->position.x + (w->cam->position.y - a.y) / -tangent : a.x;
if (ceil(a.x / 32) < 0 || ceil(a.x / 32) >= w->map->w)
    a.x = (ceil(a.x / 32) < 0) ? 0 : w->mini_w - 1;
while (w->map->board[(int)a.y / 32][(int)a.x / 32] == 0)
{
    a.x += ((angle >= 90 && angle < 180) || angle >= 270) ? -o.x : o.x; 
    a.y += o.y;
if (ceil(a.x / 32) < 0 || ceil(a.x / 32) >= w->map->w)
    a.x = (ceil(a.x / 32) < 0) ? 0 : w->mini_w - 1;
    if (a.y < 0 || a.y > w->mini_h - 1)
        break;
}
w->cam->interh[i].x = a.x;
w->cam->interh[i].y = a.y;}
static inline void      inter_ver(t_wolf3d *w, int i, double angle){
t_vec2f a; // intersection point
t_vec2f o; // offset
double  tangent;

tangent = ttan(angle) + 0.001;
a.x = (angle >= 90 && angle < 270) ? ceil(w->cam->position.x / 32) *
32 - 1 : floor(w->cam->position.x / 32) * 32 + 32;
o.x = (angle > 90 && angle < 270) ? -32 : 32;
o.y = (angle > 0 &&  angle < 180) ? -32 * tangent : 32 * tangent;
a.y = (angle != 270 && angle != 90) ? w->cam->position.y + (w->cam->position.x - a.x) * tangent :
w->cam->position.y + (w->cam->position.x - a.x) * -tangent;
if (ceil(a.y / 32) < 0 || ceil(a.y / 32) >= w->map->h)
    a.y = (ceil(a.y / 32) < 0) ? 0 : w->mini_h - 1;
while (w->map->board[(int)ceil(a.y / 32)][(int)a.x / 32] == 0)
{
    a.y += ((angle > 90 && angle < 180) || angle > 270) ? -o.y : o.y;
    a.x += o.x;
    if (ceil(a.y / 32) < 0 || ceil(a.y / 32) >=  w->map->h)
        a.y = (ceil(a.y / 32) < 0) ? 0 : w->mini_h - 1;
    if (a.x < 0 || a.x > w->mini_w - 1)
        break;
}
w->cam->interv[i].x = a.x;
w->cam->interv[i].y = a.y;}

I could use the dir.y / dir.x computed before for my tangent, but i didn't cause i had some divisions by zero when my rays was facing at 180, 360, 0 degrees, angles are complexes and i dont understand completely the vectors method to do that way; these are some screeshots :

enter image description here enter image description here

\$\endgroup\$
  • \$\begingroup\$ In this answer, I show how to march a ray through a 2D grid, rasterization-style. It looks like that might be relevant to the problem you're solving? \$\endgroup\$ – DMGregory Apr 2 at 18:27
  • \$\begingroup\$ I am looking at it, can you recommand me some good books to learn mathematics for computer graphics from beginners to masters? Cause I feel like when you give me codes you use some tricky advanced mathematics and I want to learn the way you find that! \$\endgroup\$ – Num Lock Apr 3 at 15:03
  • \$\begingroup\$ It's mostly high school linear algebra. You can find lots of resources about this online, or any standard linear algebra textbook will do. \$\endgroup\$ – DMGregory Apr 3 at 15:51
  • \$\begingroup\$ Well! Feels like the fact that i am a french native speaker has something to do with it! Notions and notations are quite differents \$\endgroup\$ – Num Lock Apr 3 at 16:33
1
\$\begingroup\$

\$\alpha\$ is the angle between the X-axis (horizontal) and the ray you're casting.

\$Tan(\alpha)\$ is also called the slope, which is \$opposite/adjacent\$ so in the triangle on the right it's \$Ya/Xa = Ya/64\$, thus, if you know the slope and one intersection you can get the next intersection in the grid by adding \$64\times Tan(\alpha)\$ to they coordinate and \$64\$ to the x coordinate.

\$\endgroup\$
  • \$\begingroup\$ I did something which was instead of computing each time the angle between ray and X-axis, i just had to re-use my dir->x and dir->y which was the vector direction of each ray to get my tan(ALPHA), i don't know if this make sense \$\endgroup\$ – Num Lock Apr 2 at 17:42
  • \$\begingroup\$ Basically, tan(alpha)=Ya/Xa=dir->y/dir->x, computing the angle and then its "tan" is the long way to achieve the same thing. in other words, alpha is not sought after, tan(alpha) is what we need, we can get it by dividing dir->x by dir->y, or by calculating the angle and then the tangent, but why would we add complexity? \$\endgroup\$ – Esam Bustaty Apr 2 at 18:34
  • \$\begingroup\$ (the difference in Y between first and second intersections) = (the difference in Y between second and third intersections) = (the difference in Y between third and fourth intersections) = width * tan(alpha) = width * dir->y / dir->x ...taking advantage of this equation you arrive at the solution described in the reference. \$\endgroup\$ – Esam Bustaty Apr 2 at 18:39
-1
\$\begingroup\$

I actually got it by myself, it was a mistake inside the minimap raycasting! First of all, i decided to print my rays part by part (example : ray 0 to 200), i noticed that the rendering in my minimap was not exactly what i had on my screen by moving left or right (rotating); i figured out that the mistake was not the rendering itself but the fact that i used different widths of screen to do the minimap and the 3d rendering raycast

\$\endgroup\$
  • \$\begingroup\$ Remember that the goal of answers on this site is to help future users who experience similar problems. "I figured it out" doesn't help them, but "here are the steps I took to find the problem, and here is how I solved it" does, so try to focus on explaining your solution process. \$\endgroup\$ – DMGregory Apr 15 at 18:13

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.