THE MAZE PROJECT
This is my Final project for ALX Software Engineering’s inaugural academic year. The purpose of this project is to develop a 3D ray-casting game utilizing a third party SDL library. The major objective of this project was to create a fully functional game that would push the player to think creatively and take an alternative path. Another objective was to make the game more engaging by including foes (such as obstacles) and weaponry.
So why did I pick this particular project?
This project’s primary goal is:
learning the physics and math that go into it.
how games were created in the past.
studying ray tracing and casting.
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.written in C.
Generally, I used the following technology and architecture.
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
Follow Hasen ebrahim to stay updated on their latest posts!
0 comments
Be the first to comment!
This post is waiting for your feedback.
Share your thoughts and join the conversation.