Lou's Pseudo 3d Page v0.8B
(C) 2009 Louis Gorenfeld, updated June. Is this information inaccurate? Am I way off? Don't hesitate to write me at louis.gorenfeld at gmail dot com! Table of ContentsIntroductionRoad Basics Curves and Steering Sprites and Data Hills Enhancements Related Effects Roads on Various Hardware Code Stuff Glossary The Gallery IntroductionWhy Pseudo 3d? But they have plenty of drawbacks as well. The depth of physics found in more simulation-like games tends to be lost, and so these engines aren't suited to every purpose. They are, however, easy to implement, run quickly, and are generally a lot of fun to play with! It is worth noting that not every older racing game used these techniques. In fact, the method outlined here is only one possible way to do a pseudo 3d road. Some used projected and scaled sprites, others seem to involve varying degrees of real projection for the road. How you blend real mathematics with trickery is up to you. I hope you have as much fun exploring this special effect as I did. Raster Effects - Some Background
Road BasicsIntroduction to Raster Roads The pseudo raster road effect actually works similarly to the Street Fighter II perspective effect in that it warps a static image to create the illusion of 3d. Here's how they do it: Most raster roads start off with an image of a flat road. This is essentially a graphic of two parallel lines on the ground retreating into the distance. As they get farther into the distance, the lines appear to the viewer to be closer together. This is a basic rule of perspective. Now, in order to give the illusion of motion, most arcade racing games have stripes on the road. Moving these stripes on the road forwards is generally either achieved by color cycling or by changing the palette every line. Curves and steering are done by scrolling each line independently of one another, just like in Street Fighter II. We will get into curves and steering in the next chapter. For now, let's put that aside and concentrate on making the road appear to scroll forwards. The Simplest Road Take the image of a road described above: Two parallel lines demarcating the left and right edges of the road
retreat into the distance. As they move into the distance, they appear to the viewer to be closer together.
Below is what this might look like:
What is missing from this picture are road markings to give a good impression of distance. Games use alternating light and dark strips, among other road markings, for this effect. To help accomplish this, let's define a "texture position" variable. This variable starts at zero at the bottom of the screen and increases each line going up the screen. When this is below a certain amount, the road is drawn in one shade. When it is above that amount, it is drawn in the other shade. The position variable then wraps back to zero when it exceeds the maximum amount, causing a repeating pattern. It is not enough to change this by a set amount each line though, because then you'll just see several strips of different colors which are not getting smaller as the road goes into the distance. That means you need another variable which will change by a set amount, add it to another variable each line, and then add the last one to the texture position change. Here's an example which shows, from the bottom of the screen, what the Z value will be for each line as it recedes into the distance. After the variables, I print what is added to get the values for the next line. I've named the values DDZ (delta delta Z), DZ (delta Z), and Z. DDZ remains constant, DZ changes in a linear fashion, and Z's value curves. Note that the value I chose, four, is arbitrary and was just convinient for this example.
Noice that DZ is modified first, and then that is used to modify Z. To sum it up, say you are moving through the texture at speed 4. That means that after line one, you are reading the texture at position 4. The next line will be 12. After that, 24. So, this way it reads through the texture faster and faster. This is why I like to refer to these variables as the texture position (where in the texture we are reading), the texture speed (how quickly we read through the texture), and the texture acceleration (how quickly the texture speed increases).
A similar method will also be used to draw curves and hills without too much number crunching. Now, to make the road appear to move, just change where the texture position starts at the bottom of the screen for each frame. Now, you may notice a shortcoming with this trick: the zooming rate is inaccurate. This causes a distortion that I will refer to as the "oatmeal effect". It is a warping effect present in early pseudo games such as OutRun in which objects, including the stripes on the road, appear to slow down as they move outwards from the center of the screen. A More Accurate Road - Using a Z Map In this case, the road is mapped onto the screen by reading through this buffer. For each distance, you must figure out what part of the road texture belongs there by noting how many units each stripe or pixel of the texture take up. As a side-note,
even though we now know the distance of each row of the screen, it may be useful to also cache the width of the road or scale factor for each line. This would be the
inverse of the distance, adjusted so that the value is 1 on the line which the player's car graphic spends the most time.
Making it Curve There are some problems with this method though. One is that S-curves are not very convinient. Another limitation that going into a turn looks the same as coming out of a turn: The road bends, and simply unbends. To improve the situation, we'll introduce the idea of road segments. A road segment is a partition which is invisible to the player. Think of it as an invisible horizontal divide which sets the curve of the road above that line. At any given time, one of these segment dividers is at the bottom of the screen and another is travelling down at a steady rate towards the bottom of the screen. Let's call the one at the bottom the base segment, because it sets the initial curve of the road. Here's how it works: When we start drawing the road, the first thing we do is look at the base point and set the parameters for drawing accordingly. As a turn approaches, the segment line for that would start in the distance and come towards the player kind of like any other road object, except it needs to drift down the screen at a steady rate-- otherwise the road swings too wildly. So, suppose the segment line for a left curve is halfway down the road and the base segment is just a straight road. As the road is drawn, it doesn't even start curving until it hits the "left curve" segment. Then the curve of the road begins to change at the rate specified by that point. As the moving segment hits the bottom of the screen, it becomes the new base segment and what was previously the base segment goes to the top of the road. Shown below are two roads: One is a straightaway followed by a left turn, and one is a left turn followed by a straightaway. In both these cases, the segment position is halfway down the Z Map. In other words, the road begins to curve or straighten halfway down the road, and the camera is entering the turn in the first picture and leaving the turn in the second.
And this is the same technique and same segment position applied to an S-curve: The best way to keep track of the segment position is in terms of where on the Z Map it is. That is, instead of tying the segment position to a screen y position, tie it to a position in the Z Map. This way, it will still start at the road's horizon, but will more gracefully be able to handle hills. Note that on a flat road that the two methods of tracking the segment position are equivalent. Let's illustrate this with some code:
One big advantage of doing the curves this way is that if you have a curve followed by a straightaway, you can see the straightaway as you come out of the curve. Likewise, if you have a curve followed by a curve in the opposite direction (or even a steeper curve the same direction), you can see that next piece of track coming around the bend before you hit it. To complete the illusion, you need to have a horizon graphic. As the curve approaches, the horizon doesn't change (or scrolls only slightly). Then when the curve is completely drawn, it is assumed that the car is going around it, and the horizon scrolls quickly in the opposite direction that the curve is pointing. As the curve straightens out again, the background continues to scroll until the curve is complete. If you are using segments, you can just scroll the horizon according to the settings on the base segment. Perspective-style Steering Placing Objects and Scaling The X position of the object should be kept track of relative to the center of the road. The easiest way then to position the sprite horizontally is just to multiply it by the scaling factor for the current line (inverse of Z) and add that to the road's center. Storing Track Data If, however, you are using a segmented system, you can just use a list of commands. The distance that each command takes is equivalent to how quickly the invisible segment line drifts down the screen. This also frees you up to create a track format which works on a tile map, for representing somewhat realistic track geography. That is, each tile could be one segment. A sharp turn could turn the track 90 degrees, while a more mild turn would come out at 45 degrees. Texturing the Road If you want the road to look more accurate, make the Z for each line correspond to a row number on a texture graphic. Voila! One textured road! However, if you only want strips of alternating color, the answer is even simpler-- especially when using fixed point. For each Z, make one of the bits
represent the shade of the road (dark or light). Then, just draw the appropriate road pattern or colors for that bit.
Varieties of Hills
Faked Hills
Here's how it's done: First of all, the drawing loop would start at the beginning of the Z Map (nearest) and stop when it gets to the end (farthest). If we are to decrement the drawing position each line by 1, the road will be drawn flat. However, if we decrement the drawing position each line by 2, doubling lines as we go, the road will be drawn twice as high. Finally, by varying the amount we decrement the drawing position each line, we can draw a hill which starts flat and curves upwards. If the next drawing position is more than one line from the current drawing position, the current Z Map line is repeated until we get there, producing a scaling effect. Downhills are similar: If the drawing position is incremented instead of decremented, it will move beneath the last line drawn. Of course, lines which are below the horizon will not be visible on-screen-- only lines which are 1 or more pixels above the last line should be drawn. However, we'll still want to keep track of objects which are beneath the horizon. To do this, note the screen Y position of each sprite as the Z Map is traversed. It may help to make the Z Map larger than needed for a flat road. This way, as the buffer stretches, it won't become too pixellated. Now, we have to move the horizon to convince the player. I like to use a Lotus-style background in which the horizon doesn't just consist of just a skyline, but also a distant ground graphic. When the hill curves upwards (elongating the view), the horizon should move downwards slightly relative to the top of the road. When the hill curves downwards as the camera crests the hill (shortening the view), the horizon should move upwards. This is what the effect looks like for a downhill and an uphill-- minus the horizon graphic of course:
Pros
Realistic Hills Using 3d-Projected Segments
To do this effect, you would first find the screen y location of each 3d segment by using the screen_y = world_y/z formula. Or, alternately, you could find the height off the ground of a given segment by multiplying the segment's height by the scaling factor for that line. Then, you would linearly interpolate the road widths and texture (if desired) between these heights. Deciding which 3d segments to draw and which not to can be determined easily: From the bottom of the screen, a 3d segment which is lower than the last-drawn 3d segment would not be shown on-screen. The sprites on that 3d segment would still need to be shown and properly cropped, however. We can actually draw the sprites as the last step: If a sprite is on a segment which is completely visible, it does not need to be cropped. But if a sprite is on a segment which is either not visible or partially visible, we can easily crop it. First, find the top of the sprite. Then, either every line of the sprite will be drawn, or the sprite will be drawn until it hits the last visible segment's screen Y position. If the top of the sprite is below the last segment's Y position, the sprite won't be visible at all and will be skipped. Since my mock-up of this specific variation is underdeveloped, I will have to save discussing curves for a later update. There are some games which can be observed though in the meantime. 4WD Road Riot by Atari is a likely candidate. Enduro Racer also uses a similar looking system, but one in which the 3d segments do not actually move forwards. Pros
A Third Take on Hills The
advantage of this method is not only that the distance to each point
on the hill is mathematically
correct, but the hills can be very detailed as well.
Multiple Roads And, even more dramatic, below is the freeway after the two roads are overlapped to form six lanes, both with and without the second road:
Endless Checkerboard
Pictured below is the Space Harrier checkerboard effect with and without the palette changes. To turn this
into a checkerboard, all that has to be done is to flip the color palette every few lines. Think of it as analogous to the
light and dark strips on a road.
So, how then does it scroll left and right? This is just a variation on perspective-style steering: As the
player moves to the left or right, the ground graphic is skewed. After a few pixels have scrolled past, the ground
"resets" or "wraps" its position. This is how it appears to scroll endlessly to the left and right.
Roads on the Commodore 64
First, some background: On many console systems, a pseudo-3d road can be done by drawing a straight road with tiles and scrolling per-line to make it appear to curve. However, this turned
out to be too slow for a full-framerate game on the Commodore 64.
Simon's engine instead uses C64's bitmap mode and uses a fast-fill algorithm. His fast-fill algorithm uses self-modifying code to speed up draws: Each line is a series of per-pixel
stores which specify an address in video memory.
At the point though that the color has to change, the code is altered. The store command is turned into a load command, and what was the address for the store is turned into
the literal number of the new color.
One major advantage of this technique is that sprite multiplexing techniques to show more than eight sprites on the screen can still be used. In Simon's words:
"Offsetting the horizontal scroll to get a stable raster effect would involve manipulating register $D011.
The raster IRQ at $D012 would flicker badly otherwise depending on the number of sprites
on that particular raster line. To have any sort of smooth display would involve locking up the processor to get the timing just right, or by not using the screen graphics and just changing the
border colour. This which would be solid and flicker free, but there wouldn't be road visible on the screen because it would have to be switched off.
These smooth, per-line border colour changes were used
chasing the raster down the screen, and it could also be used to 'hold off' where the top of the screen could be displayed. It was called $D011 hold-off or sometimes FLD for flexible line distancing
(the technique used to eliminate VIC's bad lines).
Dedicated Road Hardware First off, the chip has its own graphics memory. What is stored in this road ROM is nearly a perspective view of the road, given that it is flat,
centered, and not curving.
Then, for each line of the screen, the programmer specifies roughly which line of the perspective graphic to draw there.
Each line also has an X offset (for curving the road) and each line can also have a different color palette (to draw road markings and simulate movement).
To show an example, here are some images taken from Sega racing game road graphics side-by-side with the road as seen in-game
(special thanks to Charles MacDonald for his road viewing application):
The first thing you might notice is that the road graphics are much higher resolution than the in-game graphics. In these particular games, the road is up to 512x256 resolution while the game's display resolution is only 320x224. This gives the graphics engine plenty of graphic to play with, which cuts down on the amount of jaggies. Another thing which might pop out at you is that the perspective of the road stored in the ROM is completely different from the perspective shown in-game. This is because the graphic in the ROM merely stores what the road might look like for various road widths. It is the job of the program to select the correct lines out of this large graphic for each line of the screen. The hardware supports two roads at a time, and so you can assign priority to the left or right road. This is for the parts of the game in which the road branches, or when there is a center divider between lanes of traffic. For you ROM hackers out there, check out MAME's src/mame/video/segaic16.c and src/mame/video/taitoic.c files for examples of road chips. Note that the Sega road graphics are stored in a 2-bit planar format, with the center of the graphic able to sport a fourth color (this is the yellow line shown in the graphics above).
Other Engines
Power Drift
Racin' Force Formulas and Tips Fixed point multiplcation is trickier than addition. In this operation, you would multiply the two numbers and
then shift right by however many bits are reserved for fractions. Due to overflow, sometimes you may need to shift
before multiplication instead of after. See "Fast Linear Interpolation" for an example of fixed point multiplcation.
Usually, a scaling routine has parameters like x, y, and scaling factor.
But since a scaling factor is just 1/z, we can just reuse the Z value of that
sprite! We will still need the scaling factor though to determine the
boundaries of the sprite so that we can keep it centered as it scales.
Bad Line - In the C64's VIC II graphics chip, at the first pixel of every background tile, the VIC takes over the processor to pull in more data such as colors. Since there are fewer cycles left for a program to do computations, these are refered to as bad lines Height Map - A height map is an array of height values. In a polygon or voxel landscape engine, this might be a 2d array (think of a landscape viewed from the top). However, in a road engine, a height map would only need to be one-dimensional (think of a landscape viewed from the side). Indexed Color Mode - Older systems which feature few colors on the screen at a time are generally in indexed color modes. Some of the most common indexed color modes are the 256-color VGA modes. In these modes, each pixel is represented by a byte. Each byte stores an index value from 0 to 255. When the screen is drawn, the index number for each pixel is looked up in the palette. Each entry in the palette can be one of VGA's 262,144 possible colors. In summary, even though only 256 colors can be on screen at a time, the user can choose each color from a much larger palette. Linear Interpolation - The process of obtaining in-between values from a data set by drawing lines between the points Painter's Algorithm - The Painter's Algorithm is the practice of drawing overlapping objects from far to near. This ensures that closer objects always appear on top of farther ones. Planar Graphics Mode - A Planar graphics mode is one in which an N-bit image is made up of N 1-bit images which are combined to produce the final image. This is opposed to most graphics modes, sometimes referred to as chunky, in which an N-bit image is made up of N-bit pixel values. Raster Effect - A raster effect is a graphical trick which takes advantage of the scanline-based nature of most computer, or raster, displays. Scaling Factor - The reciprocal of Z. This gives you the amount by which to scale an object at a given Z distance. Segment (Road) - I am using the term segment to mean a position below which the road acts one way, and above a different way. For example, the segment could divide a left turn on the lower half of the screen from a right turn on the upper half. As the segment makes its way towards the player, the road will appear to snake left then right. Segment, 3d (Road) - I am using the term 3d segment to mean a horizontal line which has both a Z distance and a world Y height. Unlike a vertex, which could be a 3d point, a 3d segment would be a 3d line with the left and right X-axis endpoints at positive and negative infinity. Voxel - A 3d pixel. Raycast/landscape voxel engines were popularized in the game Commanche: Maximum Overkill. Z Map - A lookup table which associates each line of the screen with a Z distance.
Cisco Heat Pole Position Hydra Outrunners Road Rash
Turbo Spy Hunter II Pitstop II Enduro Enduro Racer Lotus Test Drive II Speed Buggy |