Do you remember 'Wolfenstein 3D'? Here's some math behind the raycasting technique.

Do you remember 'Wolfenstein 3D'? Here's some math behind the raycasting technique.

"Wolfenstein 3D" and its Ingenious 3D Illusion:


In the early '90s, typical PCs weren't built for 3D graphics. Yet, "Wolfenstein 3D" offered an immersive 3D experience. How?


Raycasting: Rather than rendering true 3D, the game cast rays from the player's view, each for a screen slice. The distance to walls determined the wall slice's height, crafting a 3D illusion.


2D Map, 3D View: The game's world was a 2D grid. Raycasting transformed this grid into what felt like a 3D maze for players.


Uniform Environments: With consistent wall heights and 90-degree angles, raycasting calculations remained efficient and quick, ensuring fluid gameplay on then-limited hardware.


Sprite-based Objects: While walls seemed 3D, enemies and objects were 2D sprites, scaled based on their distance to the player.


In essence, "Wolfenstein 3D" masterfully sidestepped hardware limitations, creating an immersive 3D feel on platforms designed for 2D.


Exploring Raycasting in Pygame: A Dive into the Mathematics and Functionality


Raycasting is a rendering technique historically used in 2D games to simulate a 3D perspective. Instead of computing every 3D object, raycasting "casts" rays from the player's viewpoint and calculates intersections with walls in the game world. This method was popularized by early FPS games like "Wolfenstein 3D" by id Software.


In this article, we'll explore the mathematical underpinnings and the implementation of raycasting in a simple Pygame setup.


#### Mathematics Behind Raycasting:


1. **Vector Math and the Player**: 

The player's direction is represented as a vector. If the player is facing 90 degrees (or π/2 radians), their direction vector is represented using sine and cosine functions:

x = cos(π/2)

y = sin(π/2)


2. **Ray Angles**:

For a field of view (FOV) of 120 degrees (or whatever fits your game), we start casting rays from the leftmost angle, incrementing by a small factor until we cover the entire FOV. The leftmost angle can be calculated by subtracting half of the FOV from the player's current angle.


3. **Distance Calculation**:

When a ray intersects a wall, the distance between the player and the intersection point is crucial. We calculate it using the Pythagorean theorem:

distance = \sqrt{(x2 - x1)^2 + (y2 - y1)^2}


#### The Raycasting Function:


The `cast_rays2` function in our Pygame setup performs raycasting:


- It initializes the leftmost angle based on the FOV and player's current angle.

- For each ray within the FOV:

 - Cast the ray and look for wall intersections.

 - If an intersection is found, calculate the distance from the player to the wall.

 - Store this distance for later rendering.


The key snippet of the function is:

```python

left_ray_angle = self.player.rotation_angle - (self.FOV / 2 * math.pi)

for rays in range(self.FOV):

  ...

  wall_x_pos=self.player.x + math.cos(left_ray_angle)*depth

  wall_y_pos=self.player.y + math.sin(left_ray_angle)*depth

  ...

  left_ray_angle += math.pi/360

```


Rendering Walls with Ray Data:


Using the distances obtained from raycasting, we render walls on the screen. The further a wall is, the smaller its on-screen height. This perspective is achieved by scaling the wall's on-screen height inversely to its distance.


The `draw_walls` function handles this:

- It calculates the height of each wall slice using the formula:

wall\_height = constant / distance

- It then draws each slice on the screen, giving the 3D perspective illusion.


#### Conclusion:


Raycasting is a powerful and efficient technique that offers 3D perspectives with minimal computational requirements. While it has been mostly replaced by more advanced rendering techniques in modern games, understanding its foundational mathematics and logic can provide valuable insights into game development and computational geometry.


Next time you see a retro-styled FPS, appreciate the mathematics and algorithms at play behind those pixelated corridors and walls! 


To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics