[Prev] [Home] [Next]

RAY-CASTING STEP 3: FINDING WALLS

Notice from the previous image (Figure 11), that the wall can be viewed as collection of 320 vertical lines (or 320 wall slices).


Figure 12


This is precisely a form of geometrical constraints that will be suitable for ray-casting. 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.

Figure 13: Rays looking for walls.


Therefore, to render such scene, we can simply trace 320 rays starting from left to right. This can be done in a loop. The following illustrates these steps:
    1. Based on the viewing angle, subtract 30 degrees (half of the FOV).
    2. Starting from column 0:
      A. Cast a ray. (The term "cast" is a bit confusing. Imagine the player as a wizard who can "cast" rays instead of spells. The ray is just an "imaginary" line extending from the player.)
      B. Trace the ray until it hits a wall.
      C. Record the distance to the wall (the distance is equal to the length of the ray).
    3. Add the angle increment so that the ray moves to the right (we know from Figure 10 that the value of the angle increment is 60/320 degrees).
    4. Repeat step 2 and 3 for each subsequent column until all 320 rays are cast.
The trick to step 2A is that instead of checking each pixels, we only have to check each grid. This is because a wall can only appear on a grid boundary. Consider a ray being traced as in Figure 14. To check whether this ray has hit a wall or not, it is sufficient to check the grid intersection points at A, B, C, D, E, and F.

Figure 14: This ray intersects the grids at points A,B,C,D,E, and F.

 

[Prev] [Home] [Next]