This is an implementation of a simple raycaster which translates a 2D array map into a pseudo 3D scene inspired by early Wolfenstein Games.
In this raycaster, simple trigonometry is used to efficiently create a pseudo 3D scene.
By, this we can find the value of the first intercept with the grid.
Now to find the intercept with next grid, we can easily find the next intercept by adding the following values to the previous intercept.
To find the value of
The same logic used to find the vertical intercept can also be used to find the horizontal intercept.
For horizontal intercept, the subsequent intercept would look something like this.
With this, we can check when the ray intersects a block of the grid. If it intersects a block, we can calculate the distance of the block from the player and draw the wall accordingly.
To draw the pseudo 3D scene, we need to find the projected height for each wall. To find the projected height, we first need to find the distance of that projected wall from the player.
This would look something like this.
We have defined FOV as 60 degrees or PI/3 radians.
So,
Now, to find the projected height of the wall, we can use similar triangle properties.
By similarity of triangles,
We also need to define a scaling factor, which helps optimize the drawing of the wall.
Scale is defined as,
Now that we have the projected height, we can draw the walls using the following values,
x = ray * SCALE
y = HALF_HEIGHT - proj_height / 2
width = SCALE
height = proj_height
This can be better visualized as follows,