[Prev] [Home] [Next]

RAY-CASTING STEP 4: FINDING DISTANCE TO WALLS

There are several ways to find the distance from the viewpoint (player) to the wall slice. They are illustrated below.


Figure 17: Finding distance to a wall slice.
  


The sine or cosine functions are cheaper to implement because they can be pre-computed and put into tables. This can be done because ALPHA (player's POV) has to be between 0 to 360 degrees, so the number of possibilities are limited (the square root method has a virtually unlimited possible values for the x's and y's).


Before drawing the wall, there is one problem that must be taken care of. This problem is known as the "fishbowl effect." Fishbowl effect happens because ray-casting implementation mixes polar coordinate and Cartesian coordinate together. Therefore, using the above formula on wall slices that are not directly in front of the viewer will gives a longer distance. This is not what we want because it will cause a viewing distortion such as illustrated below.

Figure 18

Figure 19

Thus to remove the viewing distortion, the resulting distance obtained from equations in Figure 17 must be multiplied by cos(BETA); where BETA is the angle of the ray that is being cast relative to the viewing angle. On the figure above, the viewing angle (ALPHA) is 90 degrees because the player is facing straight upward. Because we have 60 degrees field of view, BETA is 30 degrees for the leftmost ray and it is -30 degrees for the rightmost ray.

[Prev] [Home] [Next]