The problematic was : re-create a Wolfenstein-3D like, using the MLX library. Only C was allowed.
git clone [email protected]:Bima42/cub3d.git
make
./cub3d map.cub
Note : Use W, A, S, D key to move. Use left and right arrow to rotate camera. Esc quit the game.
At the beginning, we decided to treat parsing part before talk about raycasting etc Then, we split works in two parts : initialisation part (graphics, mlx ...) and raycasting part
There is some important point to note :
- Map file is .cub only
- This map file is strict :
- Begin by texture path (N, S, E, W)
- Then some rgb data for ceiling and floor
- Empty lines can be found between those datas
- Map architecture
- Map :
- Must be surrounded by '1'
- Only one player spawn (N, S, E or W indicate player orientation)
Strategy use for map checking : for every rows and colums (x and y)
- Start with skip white spaces
- Then we have to hit a wall
- We skip all char except wall (1) until white space or \0
- If the char in pos - 1 is not a wall, it's an error
- The fact is if we hit a wall we have to found another one, walls works by pair. If the second wall was not hit, row (or column) is false
During this check, we extract the higher x and y to reformat_map, using white_spaces to reformat and put every lines ar the same length.
Last line is filled with \0.
Transforming a limited form of data into a three-dimensional projection with the help of tracing rays from the view point into the viewing volume. Main principle : rays can be cast and traced in groups based on certain geometric constraints. A ray from the pixel through the camera is obtained and the intersection of all objects in the picture is computed.
We have to create a maze world that has the following geometric constraints:
- Walls are always at 90° angle with the floor.
- Walls are made of cubes that have the same size.
- Floor is always flat. Need to define some attributes :
- Player/viewer’s height, player’s field of view (FOV), and player’s position.
- Projection plane’s dimension.
- Relationship between player and projection plane. FOV of 60 is generally a good choice. Player height reprensent half of window height.
The following illustrates steps:
- Based on the viewing angle, subtract 30 degrees (half of the FOV).
- Starting from column 0:
- Cast a ray.
- Trace the ray until it hits a wall.
- Record the distance to the wall (the distance = length of the ray).
- Add the angle increment so that the ray moves to the right
- Repeat step 2 and 3 for each subsequent column until all rays are cast.
This figure show a really good method to understand how checking horizontal and vertical intersections.
When horizontal and vertical intersections are merged, you have to check the lowest distance between player and wall using Pythagore theorem. Now you know which side of the wall you have to drawn.
This way will show distortion called "fish-eye".
To remove the viewing distortion : correct_dist = distorted_dist * cos(angle);