I know what it's used for, I know what it does.

What I am essentially asking is what is the math/code behind how it works?

I'm aware that any custom implementation would likely be slower than an engines official implementation, I just am interested in how it works.


I assume you're refering to how a game like Wolfenstein 3D uses raycasting?

Here is a nice explanation on what raycasting is, and how it's implemented:

Here is a summary of a raycasting implementation (taken from the above website, edited for clarity):

The basic idea of raycasting is as follows: the map is a 2D square grid,
and each square can either be 0 (no wall), or a positive value (some wall).

For every x-position of the screen (every vertical strip of the screen),
send out a ray that starts at the player location and points in the player's looking direction.
Let this ray move forward on the 2D map until it hits a map square that contains a wall.
If it collided with a wall, calculate the distance of the collision 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 is,
the smaller it's on screen; the closer it is, the higher it appears to be.

To find the first wall that a ray encounters on its way, start at the player's position, and each frame
check whether or not the ray collided with a wall. If it is colliding, then the loop can stop.
Calculate the distance, and draw the wall. If the ray position is not colliding with a wall, you have to trace it further. To do this, add a constant value to its position in the ray's direction. For this new position, check again if it's colliding with a wall or not. Keep doing this until a wall is hit.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.