Ray Parameters Deep Analysis

📐 Why Sin/Cos? The Math Behind Direction Vectors

Unit Circle & Direction Vectors

An angle θ on the unit circle gives us:

  • cos(θ) = X component (horizontal displacement)
  • sin(θ) = Y component (vertical displacement)
  • Together they form a unit vector (length = 1)

This is exactly what we need for ray direction!

Screen Coordinates
Origin: Top-Left
X: Left → Right
Y: Top → Bottom
Math Coordinates
Origin: Center
X: Left → Right
Y: Bottom → Top
⚠️ Coordinate System Difference:
In computer graphics, Y increases downward. In math, Y increases upward. This affects how angles are interpreted but sin/cos still work correctly!

🎯 Interactive Ray Parameter Calculator

Click anywhere to set ray direction. Grid shows map blocks.

Click to see ray parameters...
void init_ray_params(...)
{
    params->ray_dir_x = cos(ray_angle);
    params->ray_dir_y = sin(ray_angle);
    params->map_x = (int)player.x / BLOCK;
    params->map_y = (int)player.y / BLOCK;
    params->delta_dist_x = fabs(1 / ray_dir_x);
    params->delta_dist_y = fabs(1 / ray_dir_y);
    params->pos_x = player.x;
    params->pos_y = player.y;
}

🔍 Parameter Breakdown:

  • ray_dir_x/y: Direction vector components (unit vector)
  • map_x/y: Current map grid cell coordinates
  • delta_dist_x/y: Distance ray travels to cross one grid line
  • pos_x/y: Exact player position in world coordinates

🧮 The Deep Math: Why Delta Distance = 1/ray_dir?

Delta Distance X

delta_dist_x = |1 / ray_dir_x|

This is the distance the ray travels to move exactly 1 unit in the X direction.

  • If ray_dir_x = 1 (horizontal ray): delta = 1
  • If ray_dir_x = 0.5 (shallow angle): delta = 2
  • If ray_dir_x = 0.1 (steep angle): delta = 10

Delta Distance Y

delta_dist_y = |1 / ray_dir_y|

This is the distance the ray travels to move exactly 1 unit in the Y direction.

  • If ray_dir_y = 1 (vertical ray): delta = 1
  • If ray_dir_y = 0.5 (shallow angle): delta = 2
  • If ray_dir_y = 0.1 (steep angle): delta = 10

Visualization of delta distances: How far ray travels to cross grid lines

💡 Why This Matters for DDA:
The DDA algorithm steps through grid cells efficiently. Delta distances tell us exactly how far to step when crossing each grid line, making collision detection fast and accurate.
🚨 Division by Zero Protection:
If ray_dir_x or ray_dir_y is 0 (perfectly horizontal/vertical ray), 1/0 = infinity. The fabs() handles this, and the DDA algorithm is designed to handle infinite delta distances.