What is raycasting?
- Andrew Kim
- Oct 17, 2025
- 4 min read
Updated: Oct 24, 2025
Raycasting is a 3D rendering technique that is used in several video games that incorporate 3D objects and gameplay. A famous example of a game that uses raycasting is the game "Doom". So what is raycasting really? Before we get into that, we need to see when and where raycasting can actually be used.
Let's say that you are building a 3D game, and have no idea how to actually make something 3D. How would you start then? This is a problem that frequently rises in the minds of many coders that are new to 3D rendering, as it sounds very difficult. However, if you think more intuitively and flexibly, you realize that the game's layout doesn't necessarily have to be 3D! That's right, you start off with a 2D layout instead.
In this video, we can see that the red dot is the player, and the yellow blocks around it is the level. This seems MUCH more doable than building a 3D level straight off the bat, and its simple to make. Now, this setup is a monumental stepping stone for 3D rendering because you now have something to actually render! But how do we convert this 2D level into a 3D one?
The answer is.... Raycasting! But, what is raycasting?
Going back to our 2D level, you'll notice that something has changed. A red line has appeared, and it seems to stem from the player and point in where the player is pointing. The SUPER IMPORTANT purpose of this line is to detect collision with the 2D level. This is incredibly important because if we were to interpret the collision between the line and the level in a different way, we could say that this line could be our line of site, and it colliding with the level could represent our line of site being blocked by obstacles! That is the core idea of raycasting. By drawing hundreds of lines, we can simulate vision!
Now, the coding explanation to how this works is when the line detects collision with the level, the code essentially tells the line to cut off its length until the point of contact in order to prevent multiple collisions per line, as you can't see through an object in real life. The detection of collision is also a signal for the code to create a block on your screen. Now what purpose does this block serve you may ask? Well, its to create the visual illusion that the game you are playing is 3D, as you can see in the video directly above! Basically, when the line collides with the 2D level, the distance between the player, in the form of x and y coordinates, and the point of collision between the line and level, also in the form of x and y coordinates is calculated through the distance formula, which states that on a coordinate plane consisting of x and y values, the distance between any two points of coordinates (x2, y2) and (x1, y1) is equivalent to the square root of (x2^2 - x1^2) + (y2^2 - y1^2). Now with the distance that we achieved through this formula, the code interprets it as how big the block generated by the line should be. This simulates distance in the eyes of the player, which adds more to the illusion of 3D.

However, distance is not the only thing that is required to create a 3D point of view, as the offset of each line also needs to be calculated. What does "offset of each line" mean in this context? Well, its a fancy way of saying the difference in angles between any line and the line that represents what the player sees when looking directly forward. An easy way to interpret this is your peripheral vision, which stands for what you see in your field of vision that you aren't directly looking at. The offset of any line and the direct line is the same thing as the distance between what you are looking at directly and what you can look at without looking at it directly.
Anyways, why do we need to calculate the offset? We need to calculate the offset in order to determine where each block generated by their respective line should be placed on the screen. For example, the left-most line stemming from the player should generate the left-most block on the screen, as that is how vision works.
Now, by generating hundreds of lines, hundreds of blocks are also generated, which finally gives us an almost perfect 3D vision.
In order to make it perfect, however, is through color. If each and every block was the exact same color, it would be hard to make out shapes because they blend in with the other walls, so by simply increasing the darkness of the block according to distance, we can make the illusion of 3D even more believable and elegant.
In short, Raycasting is where:
Hundreds of lines are drawn from the player to the first wall it collides with,
Hundreds of blocks are then generated by the line through the calculation of distance between the collision of the line and the level to the player,
All blocks are then moved to their respective places according to the offset of the line they were produced from.
Note: the process of creating a 2D level is NOT a part of raycasting, as that is merely the setup required to perform raycasting.
