This project provided by 42 school aims to explore the functioning of raycasting by creating a simulated "3D" environment in which the user can move freely within a map.
The project must respect certain constraints :
- Using minilibx
- Display different wall textures for north, south, east and west
- Set colors for the floor and ceiling
- The left and right arrow keys must allows to rotate the perspective
WASD
must allow to move the point of viewESC
and the cross on the window's frame must close the program cleanly- The program take a map only with the
.cub
extension
The map can be freely created by the user, must be closed, and composed by the following characters to be accepted :
0
for the ground1
for a wallN
S
E
W
for the spwaning orientation
Here is an example of a valid map provided by the subject, with the path for textures and the RGB colors for the floor and ceiling :
NO ./path_to_the_north_texture
SO ./path_to_the_south_texture
WE ./path_to_the_west_texture
EA ./path_to_the_east_texture
F 220,100,0
C 225,30,0
1111111111111111111111111
1000000000110000000000001
1011000001110000000000001
1001000000000000000000001
111111111011000001110000000000001
100000000011000001110111111111111
11110111111111011100000010001
11110111111111011101010010001
11000000110101011100000010001
10000000000000001100000010001
10000000000000001101010010001
11000001110101011111011110N0111
11110111 1110101 101111010001
11111111 1111111 111111111111
According to the constraints imposed by the subject, the parsing was carried out in the following way :
-
Take data : first pass on the .cub file, for example with a static variable, to get the 6 first required data. RGB colors and paths of the images can be placed in an array, and check the validity of each path and colors. In this implementation, the order of declaration of textures or colors does not matter, but must be declared correctly and without gaps or duplication.
-
Turn map into rectangle : Once the data is retrieved, check the longest line of the map, and enlarge the size of each shorter line with spaces to form a rectangular. This will facilitate the vertical check of the map.
-
Horizontal check : check for each line if the characters are valid characters
"01NSEW "
. In case of space, check if the character preceding the space is a1
, and if the next character following the space string is a1
or the end of line. So it is possible to have space holes in a map provided that this hole is surrounded by walls. -
Vertical check : The vertical check follows the same logic, constant verification that a wall is present before and after a space string. The combination of the vertical and horizontal check will allow to create any type of map, rounded or with islands, while checking that there is no opening, in example a
0
not surrounded by1
.
The simulation of the 3D perspective of the raycasting is operated by size of the central pixel columns, more or less long depending on the distance between the player and the wall. This distance is measured by rays crossing the 2D map, and checking if a wall is encountered. These measurements use a digital differential analysis algorithm to check only the useful areas, in this case the intersections between the cells of the map. This measurement is performed as many times as there are x-pixels on the user's screen, allowing a resource saving without having to calculate the entire screen. Here is an example of the rendering of this project (commit ee1de0ef385d78e352bfb22914ec44ae6142da0f) in its state before the addition of textures :
Here is an example of the final graphic rendering of the project, using the textures of the original doom game, as well as a minimap and a RGB color set :
git clone git@github.com:Sleleu/Cub3d.git
cd Cub3d
In the ./Cub3d folder :
git clone https://github.com/42Paris/minilibx-linux.git mlx_linux
make
When launching the program, it is necessary to use as argument a map located in the /maps folder
./cub3D maps/map1.cub
WASD
to move->
,<-
or move the mouse on either side of the screen to execute a rotationESC
to quit the program