🎯 Raycasting Hit Point Calculation

Interactive Visualization

🔍 Function Breakdown

Step 1: Determine Hit Side

The function first checks params->side:

  • side == 0: Ray hit a vertical wall (North/South face)
  • side == 1: Ray hit a horizontal wall (East/West face)

Step 2: Calculate Distance to Wall

distance = (wall_position - player_position + offset) / ray_direction

Where offset adjusts for which side of the grid cell we hit.

Step 3: Find Exact Hit Point

hit_x = player_x + distance * ray_dir_x
hit_y = player_y + distance * ray_dir_y

This gives us the precise coordinates where the ray intersects the wall.

🎮 Key Concepts

Raycasting: A technique used in 3D games to determine what the player can see by casting rays from their position.

Grid-based World: The world is divided into blocks (BLOCK size), where walls exist at grid boundaries.

Step Direction: Determines whether we're moving in the positive or negative direction along each axis.

Hit Point: The exact pixel coordinate where a ray intersects with a wall surface.