I have an issue to find the correct height of walls to make them straight in raycasting rendering.
This is how I calculate the height of the wall which is h_seen
:
dist_w = sqrt(pow((w->cam->position.x - w->cam->rays[i].endPoint.x), 2) + pow((w->cam->position.y - w->cam->rays[i].endPoint.y), 2));
distortion = dist_w * tCos(w->cam->fov / 2 - (i * w->cam->fov / WIN_W));
if (distortion)
h_seen = dist_c * wall_h / distortion;
//printf("h_seen = %f\n", h_seen);
y = cam_h - (h_seen / 2) - 1;
This is a screenshot of the problem I have fisheye effect, if i use depth directly
if (l == 0)
{
raytravel.y = w->cam->rays[i].endPoint.y - w->cam->position.y;
raytravel.x = w->cam->rays[i].endPoint.x - w->cam->position.x;
depth = raytravel.x * dir.x + raytravel.y * dir.y;
h_seen = dist_c * wall_h / depth;
y = cam_h - (h_seen / 2) - 1;
if (hit == 1)
while (++y < (cam_h + (h_seen / 2)))
put_pixel_img(w, WIN_W - i + WIDTH_MM, y + 150, 0xff0000);
}
// Direction of each ray inside the while loop
if ((hit = intersection(w, l, &dir)))
w->cam->rays[i].endpoint = w->cam->intersection;
else
{
w->cam->rays[i].endpoint.x = w->cam->position.x + w->cam->raylength * tcos((w->cam->angle + w->cam->fov / 2) - (i * (w->cam->fov / limit)));
w->cam->rays[i].endpoint.y = w->cam->position.y + w->cam->raylength * tsin((w->cam->angle + w->cam->fov / 2) - (i * (w->cam->fov / limit))); // limit is just the screen width
}