## VARIABLE HEIGHT WALLS

So far, all the walls in our world have the same height. With some innovations, we can actually use walls of different height. This makes the world more interesting as illustrated in the next figure.

Figure 28: Variable height walls

The easiest way to conceptualize variable height walls is to consider walls as floors. That is, imagine walls as floors that are raised. We will need an array to hold the height of each floor grid to make this work. The basic method to render the scene is this:

1. Start from the leftmost column of the projection plane.
2. Find the height of the floor that the player is currently standing on. (Call this CURRENT_HEIGHT.)
3. Cast a ray and check intersections as before.
4. If the ray hits a floor that has different height than the CURRENT_HEIGHT, then that floor is either raised/sunk. (A raised floor is just a wall.)
5. If it is raised, then it will be visible. Project it, and render it. (Figure 30 below illustrates the math behind this.) 6. If it is sunk, then we don't need to project it because it will not be visible.
7. Draw the floor from the point of where the height changes occurs until the point where the top of the last wall slice is projected onto.
(Initially, the top of last wall slice will be the bottom of the projection plane.)
8. Repeat until the ray extends pass the limit of the world map.
9. Repeat step 2 to 8 for all subsequent columns.
To clarify this process, consider the process of rendering the scene in Figure 29 below.

Figure 29
Start by tracing the ray that hits the point A (the bottommost row of the projection plane). As the ray is moved along the projection plane in the upward direction, the wall at point B is hit, so slice BC is drawn. Knowing that there is no height change from A to B, the floor from point A to B is drawn. The ray is then extended. It detects a height change at point D. Therefore, slice DE is drawn. Knowing that there is no height change from point C to D, the floor from point C to D is drawn. The ray is then extended again. At point F, the edge of the map is reached. Since there can be no more height changes, the floor from point F to E is drawn and the process is repeated until the whole screen is rendered.

The main drawback of using variable height walls is that the rendering process will be considerably slower. This is because the rays no longer stops when the closest wall is hit. One way to speed this up is to set a visibility distance and simply ignore anything beyond that distance.

Figure 30: The math behind variable height walls.