Overview
The raycasting system uses the Digital Differential Analyzer (DDA) algorithm to efficiently trace rays through a 2D grid and render a 3D perspective. It supports transparent entities and recursive raycasting for realistic rendering.Ray Structures
t_raycast
Result structure returned by the DDA algorithm.Perpendicular distance from camera to hit point (corrected for fisheye effect)
Angle of the ray in degrees
Pointer to the entity that was hit (
t_entity*). NULL if no hit.Horizontal position on the wall where ray hit (0.0 to 1.0). Used for texture mapping.
Cardinal direction of the wall face that was hit: NORTH, SOUTH, EAST, or WEST
Grid coordinates of the hit cell
t_dda_raycast_config
Configuration for initiating a raycast.2D array of entity pointers representing the game grid (
t_entity***)Dimensions of the grid (width and height)
Starting position and direction of the ray (x, y, yaw)
Entity to ignore during raycasting (typically the casting character). Can be NULL.
t_dda_raycast_data
Internal data structure for DDA algorithm execution.Ray direction vector
Distance the ray travels between grid lines
Distance to next vertical or horizontal grid line
Current grid cell being checked
Direction to step in grid (+1 or -1 for x and y)
Which side was hit (0 = vertical wall, 1 = horizontal wall)
Core Function
ft_dda_raycast()
Performs DDA raycasting to find the first wall intersection.Configuration containing grid, start position, and ignored entity
t_raycast structure with hit information
Algorithm:
- Initialize ray direction from yaw angle
- Calculate step direction and initial side distances
- Loop through grid cells:
- Step to next grid line (X or Y)
- Check if current cell contains an entity
- Skip if entity is the ignored object
- Return hit data if solid entity found
- Stop if max ray length exceeded (FT_MAX_RAY_LENGTH = 150.0)
- Calculate final distance and texture coordinate
ft_utils.h:253)
Transparency System
Entity Transparency Flags
Entity allows rays to pass through based on texture alpha. Uses
entity_x_is_transparent() check.Entity is always transparent to rays (e.g., open doors, triggers)
If true, this entity blocks billboards even if transparent to rays
Recursive Raycasting
Transparent walls require multiple raycasts from the same origin:src/utils/render/camera/walls/render.c:31
entity_x_is_transparent()
Checks if a specific point on an entity’s texture is transparent.Entity to check transparency for
Which face was hit (NORTH, SOUTH, EAST, WEST)
Horizontal position on the texture (0.0 to 1.0)
Ray Angle Calculation
Calculating the yaw angle for each screen column:src/utils/render/camera/walls/render.c:60
Explanation:
- Calculate distance to projection plane using FOV
- Find angle offset for current ray index
- Add offset to character’s viewing angle
- Normalize to 0-360 degrees
Raycasting Constants
Maximum distance a ray can travel before stopping. Defined in
ft_utils.h:30.Default distance used when ray doesn’t hit anything. Defined in
cub3d.h:57.Small value for floating-point comparisons. Defined in
ft_utils.h:31.Coordinate System
t_coords
Horizontal position in grid space
Vertical position in grid space
Rotation angle in degrees (0 = North, 90 = East, 180 = South, 270 = West)
t_direction
Cardinal directions for wall faces.Fisheye Correction
The DDA algorithm returns perpendicular distance, which is already corrected:Performance Optimization
Multi-Threading
Ray casting is parallelized across CAMERA_THREADS:src/utils/render/camera/walls/render.c:95