When casting rays at different angles, walls appear curved instead of straight because we measure the actual distance to each point, not the perpendicular distance to the screen plane.
Calculate delta vectors: Find the difference between hit point and player position
Find ray angle: Use atan2() to get the angle from player to hit point
Calculate angle difference: Subtract player's facing angle from ray angle
Apply cosine correction: Multiply distance by cos(angle_difference)
❌ Without Correction
Walls appear curved and distorted
Distance varies with viewing angle
Creates unrealistic "fishbowl" effect
✅ With Correction
Walls appear straight and natural
Consistent perpendicular distances
Realistic 3D perspective
🧮 Mathematical Insight
The cosine correction works because we're projecting the actual ray distance onto the viewing plane. This is essentially converting from radial distance (distance along the ray) to perpendicular distance (distance to the projection screen).
Think of it like shining a flashlight at an angle on a wall - the light travels further along the beam than the perpendicular distance to the wall.