100 lines
2.7 KiB
Markdown
100 lines
2.7 KiB
Markdown
# cube3D
|
|
---
|
|
|
|
This project is a 3D mini game, that uses raycasting to produce a FPP (first person perspective), just like in Wolfenstein3D or Doom.
|
|
|
|

|
|
|
|
It creates a 3D view from a map in a text file, and move the view as if we were walking and looking around.
|
|
|
|
An example of a map :
|
|
|
|
```
|
|
$> cat -e map.cub
|
|
111111111111111111111111$
|
|
1......................1$
|
|
1......................1$
|
|
1......................1$
|
|
1.....11111....1.1.1...1$
|
|
1.....1...1............1$
|
|
1.....1...1....1...1...1$
|
|
1.....1...1....E.......1$
|
|
1.....11.11....1.1.1...1$
|
|
1......................1$
|
|
1......................1$
|
|
1......................1$
|
|
1......................1$
|
|
1......................1$
|
|
1.......11.............1$
|
|
1.......11.............1$
|
|
111111..11............11$
|
|
11.1....11.............1$
|
|
11....1.11............11$
|
|
11.1....11.............1$
|
|
11.1111111............11$
|
|
11...................111$
|
|
1111111111..........1111$
|
|
111111111111111111111111$
|
|
$>
|
|
```
|
|
|
|
This map will produce this 3D view :
|
|
|
|

|
|
|
|
You can change the images on the wall :
|
|
|
|

|
|
|
|
And change the map itself :
|
|
|
|

|
|
|
|
|
|
# raycasting
|
|
---
|
|
|
|
Raycasting is a technic that creates the illusion of 3D with very low computational power.
|
|
|
|
This technic has the hability to not show hidden area by design, which is a great simplification for rendering.
|
|
|
|
How it works :
|
|
|
|
1. first you determine the position and orientation of the viewer in a plan,
|
|
|
|

|
|
|
|
2. then you cast a ray until it reaches a wall
|
|
|
|

|
|
|
|
3. you can now trace the vertical line on the 3D view
|
|
|
|

|
|
|
|
4. the height of the wall is inversely proportional to the length of the ray : the closer the wall, the heigher it is
|
|
|
|

|
|
|
|
5. finally, if the rays scan the whole picture, you see the perspective appears
|
|
|
|

|
|
|
|
6. but you can also see that walls are rounded, like a fish eye effect. It's because rays change length while scanning the wall, so we must apply a correction to the angle
|
|
|
|

|
|
|
|
example of raycasting in action while construction of the project :
|
|
|
|

|
|
|
|
|
|
|
|
# ressources
|
|
---
|
|
|
|
- [tuto mlx](https://harm-smits.github.io/42docs/libs/minilibx/getting_started.html)
|
|
- [tuto raycasting js](http://www.playfuljs.com/a-first-person-engine-in-265-lines)
|
|
- [course about matrices and transformations in space (chapter 1 to 5)](https://www.youtube.com/watch?v=fNk_zzaMoSs&list=PLZHQObOWTQDPD3MizzM2xVFitgF8hE_ab)
|
|
|