The maze project

Arafat Mamia
4 min readJun 1, 2022

This is my final project of the first year at ALX Software Engineering. The Goal of this project is to create a game by using a third party SDL library in 3D using ray casting. The main goal for this project was to make a fully functioning game that would challenge the player to think outside of the box and move in a different direction. Another goal included adding enemies(e.g. obstacle ) to make the game interesting and using weapons.

so, why did I chose such a project?, the main purpose of this project is:

  • learning the mathematics and physics behind it.
  • how in old days games were develop.
  • learning ray casting and ray tracing.

Why choosing the SDL2 library and the C language? SDL provide a low level access to audio, keyboard, mouse, joystick, and graphics hardware via OpenGL and Direct3D. It is used by video playback software, emulators, and popular games including Valve’s award winning catalog and many Humble Bundle games.it officially supports Windows, mac 0S X, Linux, iOS, and Android. it also written in C, that is way I used both.

Generally, I used the following technology and architecture.

  1. Language
  • c programming language

2.Technique

  • ray casting

3.Library

  • SDL2

4.basic trigonometry

  • Pythagoras theorem and trigonometric ratio

Trigonometry Review

the flow chart of the game

first stage: create the following

  • window
  • keyboard input
  • The player
  • 2d Map grid
  • player movement in delta time and detect collision with wall

In the below picture we can see that the player can go forward and backward when we give it the up and down key pressed and it can turn left or right at a given rotation angle with given speed when we use the turn left or right pressed key. The game can detect the wall and can take action when the collision occur And I will add the wall and its texture and its detailed workflow.

stage two: ray casting for width of projection plane

WHAT IS RAY-CASTING

  • is a technique that transform a limited form of data (a very simplified map or floor plan) into a 3D projection by tracing rays from the view point into the viewing volume.

Ray casting limitation

  • Walls are always perpendicular(90 angle) with the floor
  • Floor is always flat
  • walls are made of cubes that have the same size(square grid)

we need to define some attributes before we can project and render the world. Specifically, we need to know these attributes:

1. Player/viewer’s height, player’s field of view (FOV), and player’s position.

2. Projection plane’s dimension.

3. Relationship between player and projection plane.

  • the player is 32 unit half of the wall unit
  • The FOV determines how wide the player sees the world in front of him/her
  • To put the player inside the world, we need to define the player’s X coordinate, the player’s Y coordinate, and the angle that the player is facing to. These three attributes forms the “point of view” of the player.

So now we know:

  • Dimension of the Projection Plane = 320 x 200 units
  • Center of the Projection Plane = (160,100)
  • Distance to the Projection Plane = 277 units
  • Angle between subsequent rays = 60/320 degrees

the wall can be viewed as collection of 320 vertical lines (or 320 wall slices).

Instead of tracing a ray for every pixel on the screen, we can trace for only every vertical column of screen. The ray on the extreme left of the FOV will be projected onto column 0 of the projection plane, and the right most ray will be projected onto column 319 of the projection plane.

  • 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 is equal to the length of the ray).
  • Add the angle increment so that the ray moves to the right

To find walls, we need to check any grid intersection points that are encountered by the ray The best way is to check for horizontal and vertical intersections separately. When there is a wall on either a vertical or a horizontal intersection, the checking stops. The distance to both intersection points is then compared, and the closer distance is chosen.

stage three: drawing a wall

I am Arafat Mamia, first year at ALX School doing Full stack web development ,

Links:

--

--