🎯 Understanding side_dist_y: Distance to Next Grid Line

Interactive Demo

🤔 What is side_dist_y?

Simple answer: It's the distance the ray must travel to reach the next horizontal grid line.

Why do we need it?
In raycasting, we want to find walls efficiently. Instead of checking every pixel, we jump from grid line to grid line!

The Two Cases:

Case 1: Ray going DOWN (ray_dir_y > 0)
side_dist_y = distance to BOTTOM edge of current cell
= ((map_y + 1) * BLOCK - pos_y) * delta_dist_y / BLOCK

Case 2: Ray going UP (ray_dir_y < 0)
side_dist_y = distance to TOP edge of current cell
= (pos_y - map_y * BLOCK) * delta_dist_y / BLOCK

📝 Step-by-Step Calculation Example

🔍 Breaking Down the Formula

Key variables:

Why multiply by delta_dist_y?

We calculate the fraction of a grid cell to the boundary,
then convert it to ray distance using delta_dist_y.

Think: "If crossing 1 full grid cell = delta_dist_y distance,
then crossing 0.3 of a cell = 0.3 * delta_dist_y distance"

🎮 Real-World Usage

After calculating side_dist_y, the DDA algorithm compares it with side_dist_x:

This is how games like Wolfenstein 3D determined which wall to render!