Here we are! After a one-day full-immersion, here is the first version of what I called Dog3D: a pseudo-3D raycasting engine inspired by Wolfenstein 3D’s engine.
Dog3D is based on the same principles I described in the previous article. I’m not gonna repeat myself, so if you haven’t read the theory, go read it and then come back here.
In this article I’m gonna explain how you can implement the raycasting theory we saw previously to build a real world raycasting engine.
Since there is a lot of code to show, I will just show you the most important, so I can explain it to you. To see the complete work, just check out my github.
The basic idea and data structures
Design, in software development, is probably the most important thing. It allows you to program faster and better and a good design makes possible to optimize your work with less effort.
So, let’s decide what we want to make!
We said we can represent our levels with a 2D matrix, so we want our engine to read levels from external files (no one likes to create integer matrices manually) in a form like this:
The numbers defines different types of objects, the white spaces represent walkable area and the P character is the player.
The player will move in the walkable area and the engine will render every block in his field of view respecting the proportions based on the distance from the player.
This matrix is obviously a 2D characters matrix, so we can implement it as a pointer to string:
It’s useful to store also matrix’s width and height, so we can work more easily, in case we need those informations. It’s probably a good idea to store those informations in the same structure… and now that we are on this, it would be nice to save also player’s position in it, since we can only have one player in the level and the player is an object of the level.