The Ray Casting wiki last edited by Jagged85 on 03/21/14 09:49PM View full history

## How it works

### 2D Ray Casting Basics

2D Ray Casting involves, as the name suggests, emitting 'rays' from each column of pixels on-screen (e.g. 1024 columns for a 1024x768 pixel window or screen) at incrementally greater angles, relative to the camera's horizontal axis, until the entire Field of View has been calculated. The most basic ray casters, such as Wolfenstein 3D, work on a simple grid, where each cell takes a binary value, 0 or 1 i.e. a wall or not a wall. When a ray, emitted from the camera's position, hits a cell designated as a wall, that column of pixels to which that ray related is colored the same as the wall. Pythagoras' theorem is then used to calculate the distance between the camera and the cell (i.e. the length of the ray), and the number of pixels resized accordingly (the further away the cell, the shorter the wall appears. Naturally, the limitations of this method include, but are not limited to:

• Walls can be of only one height
• All cells are of the same width and height
• Walls must be one color per face
• The camera is unable to rotate on the x-axis (look up or down)
• There can be no cells above cells (e.g. the first and second floor of a building)
• Walls must be either perfectly horizontal or perfectly vertical

### Heightmap Ray Casting Basics

Some games from the mid 90's used an extension of 2d ray casting to render the terrain for their games, most notably Outcast and the Comanche series. The terrain is rendered in a similar way to the walls in a 2d ray cast game but instead of emitting a ray for each column of pixels on screen a ray is emitted for every single pixel. This ray is then checked against a 2d gray-scale image that represents the height of the world at a given point. If the ray is lower than the height of the world at that point the pixel on screen from which it was emitted is filled in.

### 3D Ray Casting Basics

Ray casting has also been extended into a full 3d rendering solution. It is similar to ray tracing in that a ray is emitted for each pixel on screen and continues until it intersects an object. The difference between ray tracing and ray casting at this point is that ray tracing will generate secondary rays that bounce off the object whereas ray casting stops at the first intersection. 3d ray casting has been put to a variety of uses including rending the pool balls for Hustle Kings and can be used to render volumetric objects such as smoke. Due to it's rather heavy processing cost it is rarely used.

Advanced ray casters utilize a number of methods to provide a more realistic experience:

• Firstly, walls may be given a height. This can be done by simply setting a height attribute to a cell, and offsetting the colored pixels on screen accordingly. A cell may also be designated two values, a wall minimum and maximum, and the top and bottom of the pixels column offset in both directions.
• Textures may be applied to each wall, which, when the x-offset of the wall is found (how far across the ray intersects the wall), replace the uniformly colored pixels with those of the texture.
• Wolfenstein 3D introduced a simple shading method, where all vertical walls on the cell grid are made slightly darker than horizontal walls.
• Some ray casters allow the player to look up or down by deforming the world in an unrealistic way, giving the impression of rotation on the player x-axis.
• Doom, for example, does not use cells to designate walls. Instead, they are defined as lines, which could create walls of any length and angle.

### Other uses for ray casting

Ray casting can be used for purposes other than rendering. It is often used to determine collision detection in games or for hit detection in first person shooters.

The ray casting technique is an advanced form of sprite-scaling, a pseudo-3D (2.5D) method used in earlier arcade and console games. See, for example, Sega's Super Scaler arcade engine and the SNES console's Mode 7.

This edit will also create new pages on Giant Bomb for:

Beware, you are proposing to add brand new pages to the wiki along with your edits. Make sure this is what you intended. This will likely increase the time it takes for your changes to go live.

### Comment and Save

Until you earn 1000 points all your submissions need to be vetted by other Giant Bomb users. This process takes no more than a few hours and we'll send you an email once approved.