RAY CASTING

C++Ray casting is one of the most basic methods for a simplified 3D like game. Ray casting uses a cone for the field of view and s simple array can  define the walls etc of the game world.

Raycasting can go very fast, because only a calculation has to be done for every vertical line of the screen. The most well known game that used this technique, is of course Wolfenstein 3D.

Wolfenstein 3D used textures to give the walls some visual cues. A more simplified sample can use basic colors to give a basic sense of 3D like imaging.

The map is a 2D square grid, and each square can either be 0 (= no wall), or a positive value (= a wall with a certain color or texture).

For every x of the screen (i.e. for every vertical stripe of the screen), send out a ray that starts at the player location and with a direction that depends on both the player’s looking direction, and the x-coordinate of the screen.

Let this ray move forward on the 2D map, until it hits a map square that is a wall. If it hit a wall, calculate the distance of this hit point to the player, and use this distance to calculate how high this wall has to be drawn on the screen: the further away the wall, the smaller it’s on screen, and the closer, the higher it appears to be.

These are all 2D calculations. Using a top down overview gives a better sense of how ray casting works,

An example is available on github.