float use_fixed_dist(float x2, float y2, t_cub *game)
{
float delta_x, delta_y, angle, fix_dist;
delta_x = x2 - game->player.x;
delta_y = y2 - game->player.y;
angle = atan2(delta_y, delta_x) - game->player.angle;
fix_dist = use_distance(delta_x, delta_y) * cos(angle);
return (fix_dist);
}
delta_x = x2 - game->player.x
delta_y = y2 - game->player.y
These are the horizontal and vertical distances from player to target point.
angle = atan2(delta_y, delta_x) - game->player.angle
atan2(delta_y, delta_x) = angle from player to target
- game->player.angle = subtract player's current facing direction
Result: angle difference between where player is looking and where target is
use_distance(delta_x, delta_y) = sqrt(delta_x * delta_x + delta_y * delta_y)
This is the straight-line distance from player to target (Pythagorean theorem).
fix_dist = use_distance(delta_x, delta_y) * cos(angle)
cos(angle) corrects for the viewing angle.
If target is directly in front (angle=0°), cos(0°)=1, so full distance counts.
If target is to the side (angle=90°), cos(90°)=0, so no distance counts.
This is for raycasting in 3D games! It prevents the "fisheye effect":
This makes the 3D world look realistic instead of curved like a fisheye lens!